2017-01-14 9 views
0

아래에 설명 된대로 통합 결과를 플로팅하고 싶지만 빈 시트로 판명 된 것 같습니다. 그 이유는 무엇입니까? 도와주세요!파이썬 과학 프로그래밍 올바른 결과를 그릴 때 왜 공백이 발생합니까?

# -*- coding: utf-8 -*- 
import matplotlib.pylab as plt 
import numpy as np 
import scipy as sp 
from scipy.integrate import quad, dblquad, tplquad 

x = np.arange(0,1,0.1) 
print ("x = ", x) 

def f(x): 
    return x 
print ("f(x) = ", f(x)) 

x_lower = 0 
for x_upper in x : 

    val, abserr = quad(f, x_lower, x_upper) 
    print ("integral value =", val, ", x_upper = ", x_upper ,", absolute error =", abserr) 
    plt.plot(x_upper, val, ' b--') 

plt.show() 

출력은 있지만 플롯이 비어 있습니다.

x = [ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9] 
f(x) = [ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9] 
integral value = 0.0 , x_upper = 0.0 , absolute error = 0.0 
integral value = 0.005000000000000001 , x_upper = 0.1 , absolute error = 5.551115123125784e-17 
integral value = 0.020000000000000004 , x_upper = 0.2 , absolute error = 2.2204460492503136e-16 
integral value = 0.04500000000000001 , x_upper = 0.3 , absolute error = 4.996003610813205e-16 
integral value = 0.08000000000000002 , x_upper = 0.4 , absolute error = 8.881784197001254e-16 
integral value = 0.125 , x_upper = 0.5 , absolute error = 1.3877787807814457e-15 
integral value = 0.18000000000000005 , x_upper = 0.6 , absolute error = 1.998401444325282e-15 
integral value = 0.24500000000000005 , x_upper = 0.7 , absolute error = 2.720046410331634e-15 
integral value = 0.32000000000000006 , x_upper = 0.8 , absolute error = 3.552713678800502e-15 
integral value = 0.40499999999999997 , x_upper = 0.9 , absolute error = 4.496403249731884e-15 

답변

2

플롯에 아무 것도 표시되지 않는 이유는 하나의 단일 점으로 이루어진 여러 개의 선 그래프를 플로팅하는 것입니다. 선은 시작과 끝 (적어도 두 점을 의미)을 필요로하기 때문에 그래프는 공백으로 남습니다.

당신의 점을 보여주는 가장 쉬운 방법은 marker="o"으로 plt.plot()로 호출 ' b--'을 대체하는 것입니다 :

plt.plot(x_upper, val, marker="o", color="b") 

다른 옵션은 첫 번째 목록에있는 모든 통합 결과를 수집하는 것입니다, 다음 줄거리 선 그림의 전체 목록은 :

import matplotlib.pylab as plt 
import numpy as np 
from scipy.integrate import quad 

x = np.arange(0,1,0.1) 

def f(x): 
    return x 

x_lower = 0 
vals = [] 
for x_upper in x : 
    val, abserr = quad(f, x_lower, x_upper) 
    vals.append(val) 

plt.plot(x, vals, "b--") 

plt.show() 
+0

멋지다! 나 같은 초보자에게 도움을 주셔서 감사합니다! 나를 위해 매우 유용한 답변! 많이 배웠다! 예수님은 당신을 축복합니다! –

+0

나는 벡터화 된 메소드'integrate.nquad()'를 사용하여 솔루션을 연구하고 있었지만, 더 빨랐다 ... ;-) – MaxU

0

나는 아마 답을 찾을 때마다 당신은 단지 하나의 점,하지만 선을 그릴. 'bs'를 설정하면 실제로는 공백이 아닌 점을 찾을 수 있습니다.