【问题标题】:how do I plot data on matplotlib? [duplicate]如何在 matplotlib 上绘制数据? [复制]
【发布时间】:2021-11-11 11:41:58
【问题描述】:

我是 python 新手,我正在尝试了解 matplotlib 的工作原理。我有 gamestop 股票的数据,并试图随着时间的推移绘制交易量,我在绘制数据时遵循了书中所说的内容,但我的代码不起作用。你能告诉我我做错了什么吗? 下面有人回答了我的问题。问题是我在 y 中的值比在 x 中的值多。感谢大家的帮助。 这是我的代码。

import matplotlib.pyplot as plt

x = [10022500, 4961500, 6056200, 6129300, 6482000, 37382200, 78183100, 42698500, 62427300, 81345000, 25687300, 26843100]
y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
plt.plot(x,y)
plt.xlabel('volume')
plt.ylabel('day')
plt.title('volume over time')
plt.show()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_34116/3774317828.py in <module>
      1 x = [10022500, 4961500, 6056200, 6129300, 6482000, 37382200, 78183100, 42698500, 62427300, 81345000, 25687300, 26843100]
      2 y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
----> 3 plt.plot(x,y)
      4 plt.xlabel('volume')
      5 plt.ylabel('day')

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

【问题讨论】:

  • 你能告诉我们你遇到什么样的错误吗?
  • 欢迎来到 Stack Overflow!对于调试帮助,您需要提供预期输出和实际输出(或者如果您遇到错误,请提供full error message with traceback)。如需参考,请参阅minimal reproducible example。有关更多提示,请参阅How to Ask。您可以使用 Ctrl+G 或单击图片图标添加图像;有关详细信息,请参阅Markdown help
  • 回家后我会提供错误信息,因为我不得不跑到杂货店。

标签: python matplotlib


【解决方案1】:

您的 Y 尺寸错误。 X 有 12 个元素,Y 有 14 个元素。当 matplot 步进到 (?,13) 和 (?,14) 时会发生什么?以下代码工作并生成给定的图像:

import matplotlib.pyplot as plt

x = [10022500, 4961500, 6056200, 6129300, 6482000, 37382200, 
     78183100, 42698500, 62427300, 81345000, 25687300, 26843100]
y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
plt.plot(x,y)
plt.xlabel('volume')
plt.ylabel('day')
plt.title('volume over time')
plt.show()

【讨论】:

  • 快速评论,我认为时间应该始终在 x 轴上
  • 我同意。如果我要优化它,我可能会做plt.plot(range(len(x)),x),这样我就不必处理这样的不匹配了
猜你喜欢
  • 1970-01-01
  • 2019-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-06
  • 2011-07-27
  • 2020-08-23
  • 1970-01-01
相关资源
最近更新 更多