You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用matplotlib绘制股票交易量时间曲线时维度不匹配报错如何解决

问题分析与解决:matplotlib绘制GME交易量曲线维度不匹配报错

问题背景与复现代码

我是Python新手,正在学习matplotlib绘图库的使用方法。我持有游戏驿站(Gamestop)的股票交易数据,想要绘制交易量随时间变化的曲线,按照教程指导编写代码后运行报错,相关代码如下:

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

问题根源

报错核心原因很明确:matplotlib的plot函数要求传入的x、y两个数组长度必须完全一致,每个x值对应一个y值才能确定坐标点完成绘制。你当前的交易量x数组只有12个元素,代表天数的y数组有14个元素,二者维度不匹配因此触发报错。
另外补充一个优化提示:你当前的坐标轴设定和「交易量随时间变化」的需求是反过来的,常规习惯会把时间(天数)放在x轴,交易量放在y轴,可读性更强。

修复后代码

如果你的交易量确实只有12天的数据,删除y数组多余的后两个元素,同时调整坐标轴对应关系即可正常运行:

import matplotlib.pyplot as plt

# 12天的交易量数据作为y轴
y = [10022500, 4961500, 6056200, 6129300, 6482000, 37382200, 78183100, 42698500, 62427300, 81345000, 25687300, 26843100]
# 对应12天的日期序号作为x轴
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
plt.plot(x,y)
plt.xlabel('day')
plt.ylabel('volume')
plt.title('GME volume over time')
plt.show()

内容的提问来源于stack exchange,提问作者kdog

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 17:48:05