繁体   English   中英

“matplotlib 错误:x 和 y 的大小必须相同”但它们是

[英]"matplotlib error: x and y must be the same size" but they are

嘿,我对以下代码有疑问:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

x = np.array([100.0, 140.0, 180.0, 220.0, 260.0, 300.0, 340.0,  380.0, 420.0, 460.0, 500.0, 540.0])
y = np.array([2.6, 3.7, 5.2, 6.6, 8.0, 9.3, 10.9, 12.0, 13.3, 14.8, 16.3, 17.4])

# Graph 
plt.scatter(x, y ="blue") # the ERROR is in this line
plt.xlabel("Masse [m] in g")
plt.ylabel("Länge [s] in cm") 

plt.show()

错误如下:

Traceback (most recent call last):
  File "c:\Users\nicod\Desktop\Hausversuch\main.py", line 33, in <module>
    plt.scatter(x, y ="blue")
  File "C:\Users\nicod\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 3068, in scatter
    __ret = gca().scatter(
  File "C:\Users\nicod\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\__init__.py", line 1361, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "C:\Users\nicod\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\axes\_axes.py", line 4498, in scatter
    raise ValueError("x and y must be the same size")
ValueError: x and y must be the same size

也许我不理解错误,但数组的大小相同。 不仅仅是元件的数量,还有 arrays 的尺寸。 我希望有人可以帮助我::)

看起来你有一个错字,你在抛出错误的行中将“蓝色”分配给y

您可以使用以下方法修复它:

plt.scatter(x, y, c="blue")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM