Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的绘图问题_Python_Matplotlib - Fatal编程技术网

python中的绘图问题

python中的绘图问题,python,matplotlib,Python,Matplotlib,当我运行代码时,绘图仅显示在点x=4处。我希望它能在整个间歇期内出现 from math import exp, pi, sin import matplotlib.pyplot as plt for x in range(-4,5): f = (exp(-1*(x)**2))*sin(3*pi*(x)) print(x,"\t", f) plt.plot(x,f, 'ro', markersize=1) plt.show() 必须在列表中累积值才能绘制它们: from mat

当我运行代码时,绘图仅显示在点x=4处。我希望它能在整个间歇期内出现

from math import exp, pi, sin
import matplotlib.pyplot as plt
for x in range(-4,5):
    f = (exp(-1*(x)**2))*sin(3*pi*(x))
    print(x,"\t", f)
plt.plot(x,f, 'ro', markersize=1)
plt.show()

必须在列表中累积值才能绘制它们:

from math import exp, pi, sin
import matplotlib.pyplot as plt

xs = []
ys = []
for x in range(-4,5):   # or range(-4, 6) if you need to plot a value for 5
    f = (exp(-1*(x)**2))*sin(3*pi*(x))
    print(x,"\t", f)
    xs.append(x)
    ys.append(f)
plt.plot(xs, ys, 'ro', markersize=1)
plt.show()

您只存储一个值。将值存储在列表中。范围不包括最后一个元素。