【问题标题】:Matplotlib error "x and y must have same first dimension, but have shapes (1,) and (6,)"Matplotlib 错误“x 和 y 必须具有相同的第一维,但具有形状 (1,) 和 (6,)”
【发布时间】:2020-10-11 23:08:43
【问题描述】:

我正在尝试制作一个图形作为几何级数收敛的证明。这是我的代码:

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,)

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    graph.plot(konvergencia,y) 是问题所在。 konvergencia 只是一个浮点变量,而 y 是一个列表。他们必须匹配,因为 plt.plot() 只是在 (xi, yi) 处绘制点你想在那里绘制什么? 如果你想用 x=konvergencia 画一条线,那么这样做:

    graph.plot(len(y)*[konvergencia],y)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 2020-05-02
      • 1970-01-01
      • 2021-07-29
      • 2021-07-31
      • 1970-01-01
      相关资源
      最近更新 更多