我正在尝试制作一个图形作为几何级数收敛的证明。这是我的代码:
import matplotlib.pyplot as graph
def cv12(nula,r,n): # nula is A0 (first term), r is common ratio and n is how many terms we want
sucet=0 # sum of series
a=nula # assign A0 as current term
konvergencia=nula/(1-r)
x=[sucet]
y=[a]
for i in range(n):
sucet=sucet+a
x.append(sucet)
print(sucet)
a=a*r
y.append(a)
graph.plot(x,y,label="Geometrický rad")
graph.plot(konvergencia,y)
cv12(1,2,5)
但我收到这个错误
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 399, in _plot_args
raise ValueError(f"x and y must have same first dimension, but "
ValueError: x and y must have same first dimension, but have shapes (1,) and (6,)
graph.plot(konvergencia,y) 是问题所在。konvergencia 只是一个浮点变量,而 y 是一个列表。它们必须匹配,因为 plt.plot() 只是在 (xi, yi) 处绘制点,您想在那里绘制什么?如果您想用 x=konvergencia 画一条线,请执行以下操作:
graph.plot(len(y)*[konvergencia],y)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句