[英]Plotting: ValueError: x and y must have same first dimension
ValueError:x 和 y 必須具有相同的第一維,但具有形狀 (12,) 和 (6,)
我對 Python 還是很陌生,並試圖創建一個簡單的圖表。
這是我的代碼:
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()
這是完整的跟蹤:
---------------------------------------------------------------------------
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,)
plot
的x
和y
參數的長度必須相同。range(1, 13, 2)
)。對於此錯誤,您必須添加np.unique()
。 謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.