简体   繁体   English

绘图:ValueError:x 和 y 必须具有相同的第一维

[英]Plotting: ValueError: x and y must have same first dimension

ValueError: x and y must have same first dimension, but have shapes (12,) and (6,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 (12,) 和 (6,)

I'm still pretty new to Python and trying to create a simple graph.我对 Python 还是很陌生,并试图创建一个简单的图表。

Here is my code:这是我的代码:

import matplotlib.pyplot as plt

months = range(1, 13)

nyc_temp_2000 = [20.0, 30.5, 80.1, 80.3, 56.5, 99.6]

nyc_temp_2006 = [44.9, 6.4, 92.4, 69.8, 25.5, 12.5]

nyc_temp_2012 = [60.5, 60.9, 66.2, 25.0, 10.0, 78.0]

plt.plot(months, nyc_temp_2000)

plt.plot(months, nyc_temp_2006)

plt.plot(months, nyc_temp_2012)
show()

Here is the full trace:这是完整的跟踪:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_34116/1667745297.py in <module>
     10 nyc_temp_2012 = [60.5, 60.9, 66.2, 25.0, 10.0, 78.0]
     11 
---> 12 plt.plot(months, nyc_temp_2000)
     13 
     14 plt.plot(months, nyc_temp_2006)

D:\anaconda3\envs\py39\lib\site-packages\matplotlib\pyplot.py in plot(scalex, scaley, data, *args, **kwargs)
   3017 @_copy_docstring_and_deprecators(Axes.plot)
   3018 def plot(*args, scalex=True, scaley=True, data=None, **kwargs):
-> 3019     return gca().plot(
   3020         *args, scalex=scalex, scaley=scaley,
   3021         **({"data": data} if data is not None else {}), **kwargs)

D:\anaconda3\envs\py39\lib\site-packages\matplotlib\axes\_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
   1603         """
   1604         kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1605         lines = [*self._get_lines(*args, data=data, **kwargs)]
   1606         for line in lines:
   1607             self.add_line(line)

D:\anaconda3\envs\py39\lib\site-packages\matplotlib\axes\_base.py in __call__(self, data, *args, **kwargs)
    313                 this += args[0],
    314                 args = args[1:]
--> 315             yield from self._plot_args(this, kwargs)
    316 
    317     def get_next_color(self):

D:\anaconda3\envs\py39\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs, return_kwargs)
    499 
    500         if x.shape[0] != y.shape[0]:
--> 501             raise ValueError(f"x and y must have same first dimension, but "
    502                              f"have shapes {x.shape} and {y.shape}")
    503         if x.ndim > 2 or y.ndim > 2:

ValueError: x and y must have same first dimension, but have shapes (12,) and (6,)
  • The length of the x and y arguments sent to plot , must be the same.发送到plotxy参数的长度必须相同。
  • You are plotting 6 temperature points versus 12 month points.您正在绘制 6 个温度点与 12 个月点。 You have to add 6 more temperature values, or only have 6 months (eg range(1, 13, 2) ).您必须再添加 6 个温度值,或者只有 6 个月(例如range(1, 13, 2) )。

For this error, you have to add np.unique() .对于此错误,您必须添加np.unique() Thanks谢谢

暂无
暂无

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

相关问题 ValueError:绘制时x和y的第一维必须相同 - ValueError: x and y must have same first dimension when plotting ValueError:在绘制 dataframe 整数列时,x 和 y 必须具有相同的第一维 - ValueError: x and y must have same first dimension when plotting dataframe column of integers 绘制序列会导致:“ValueError:x和y必须具有相同的第一维” - plotting a sequence results in: “ValueError: x and y must have same first dimension” 绘制逻辑回归:ValueError:x和y必须具有相同的第一维 - plotting a logistic regression :ValueError: x and y must have same first dimension ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (2,) - ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 - ValueError: x and y must have same first dimension, but have shapes ValueError:x 和 y 必须具有相同的第一维,但具有形状 (6,) 和 (8,) - ValueError: x and y must have same first dimension, but have shapes (6,) and (8,) 输入错误,然后输入值错误:x 和 y 必须具有相同的第一维 - Type error, and then ValueError: x and y must have same first dimension Python ValueError:x 和 y 必须具有相同的第一维 - Python ValueError: x and y must have same first dimension Matplotlib:ValueError:x和y必须具有相同的第一维错误 - Matplotlib: ValueError: x and y must have same first dimension Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM