Math Matplotlib的“Float”错误
我正试图写一个程序来绘制方程的图表 y=5*e^-t*cos2*pi*t 我必须使用导入数学,这就是我所拥有的:Math Matplotlib的“Float”错误,math,matplotlib,import,floating-point,Math,Matplotlib,Import,Floating Point,我正试图写一个程序来绘制方程的图表 y=5*e^-t*cos2*pi*t 我必须使用导入数学,这就是我所拥有的: import matplotlib.pyplot as plt import math x = range(10) y = [5 * math.exp(-t) * math.cos(math.pi * 2 * t) for t in x] plt.plot(x,y) plt.show() 我没有得到我想要的图表。我需要x增加0.1,但当我设置范围0,10,1时,它会给我一
import matplotlib.pyplot as plt
import math
x = range(10)
y = [5 * math.exp(-t) * math.cos(math.pi * 2 * t) for t in x]
plt.plot(x,y)
plt.show()
我没有得到我想要的图表。我需要x增加0.1,但当我设置范围0,10,1时,它会给我一个错误:
浮点不能解释为整数
如何调整代码,使绘图点之间的间隔为0.1?范围仅接受所有参数min、max和step的整数值-请参阅
要创建浮点范围,可以使用numpy.arange:
或者使用另一种理解手动执行此操作:
>>> x = range(0, 10, 1)
>>> x_float = [0.1 * i for i in x]
[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
用法:
y = [5 * math.exp(-t) * math.cos(math.pi * 2 * t) for t in x_float] # note difference
plt.plot(x_float,y) # and here
既然y已经被赋值了,那么如果我手工输入,我该如何包括需要绘制的等式呢?只要把变量y改成其他变量就行了?这只是一个占位符变量名。那么我将使用plt.plotx,x\u float,y?不,旧的x是未使用的。y使用x_浮点计算。调用的是plt.plotx\u float,y
y = [5 * math.exp(-t) * math.cos(math.pi * 2 * t) for t in x_float] # note difference
plt.plot(x_float,y) # and here