python 科学编程为什么绘图正确时出现空白?
python scientific programming why blank occur when plotting with right results?
我想按如下所述绘制积分结果,但结果似乎是空白sheet,这是什么原因?请帮我!
# -*- 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
你在图中看不到任何东西的原因是你正在绘制只有一个点的几条线图。由于线条需要开始和结束(意味着至少有两个点),因此图表保持空白。
显示您的观点的最简单方法是将您对 plt.plot()
的调用中的 ' b--'
替换为 marker="o"
:
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()
也许我找到了答案,因为每次你只绘制一个点,而不是线条。如果你设置 'bs' ,你会发现它真的不是空白,而是一些点。
我想按如下所述绘制积分结果,但结果似乎是空白sheet,这是什么原因?请帮我!
# -*- 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
你在图中看不到任何东西的原因是你正在绘制只有一个点的几条线图。由于线条需要开始和结束(意味着至少有两个点),因此图表保持空白。
显示您的观点的最简单方法是将您对 plt.plot()
的调用中的 ' b--'
替换为 marker="o"
:
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()
也许我找到了答案,因为每次你只绘制一个点,而不是线条。如果你设置 'bs' ,你会发现它真的不是空白,而是一些点。