matplotlib error: ValueError: x and y must have same first dimension
我正在尝试使用 matplotlib 绘制两个列表,但我收到关于 x 和 y 维度的错误。其中一个列表包含日期和其他数字,您可以查看列表的内容,我已将它们打印在下面。
我尝试使用 len() 检查列表的长度,它们似乎相等,所以我有点迷茫。我已经检查了几个关于这个错误的问题,但运气不佳。
注意:"查询"包含我的 SQL 查询,为简单起见,我没有包括在内。
#####我的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | t = 0 for row in query: data = query[t] date.append(data[0]) close.append(data[1]) t = t + 1 print"date =", date print"close =", close print"date length =", len(date) print"close length =", len(close) def plot2(): plt.plot(date, close) plt.show() plot2() |
#
我的脚本的输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | date = [datetime.datetime(2010, 1, 31, 22, 0), datetime.datetime(2010, 1, 31, 22, 1), datetime.datetime(2010, 1, 31, 22, 2), datetime.datetime(2010, 1, 31, 22, 3), datetime.datetime(2010, 1, 31, 22, 4), datetime.datetime(2010, 1, 31, 22, 5), datetime.datetime(2010, 1, 31, 22, 6), datetime.datetime(2010, 1, 31, 22, 7), datetime.datetime(2010, 1, 31, 22, 8), datetime.datetime(2010, 1, 31, 22, 9), datetime.datetime(2010, 1, 31, 22, 10)] close = [1.5945, 1.5946, 1.59465, 1.59505, 1.59525, 1.59425, 1.5938, 1.59425, 1.59425, 1.5939, 1.5939] date length = 11 close length = 11 Traceback (most recent call last): File"script.py", line 234, in <module> plot2() File"script.py", line 231, in plot2 plt.plot(date, close) File"/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2467, in plot ret = ax.plot(*args, **kwargs) File"/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 3893, in plot for line in self._get_lines(*args, **kwargs): File"/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 322, in _grab_next_args for seg in self._plot_args(remaining, kwargs): File"/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 300, in _plot_args x, y = self._xy_from_xy(x, y) File"/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 240, in _xy_from_xy raise ValueError("x and y must have same first dimension") ValueError: x and y must have same first dimension |
提前致谢。
用你的数据为我工作。
更改代码并将打印语句放入函数中。
1 2 3 4 5 6 7 | def plot2(): print"date =", date print"close =", close print"date length =", len(date) print"close length =", len(close) plt.plot(date, close) plt.show() |
你的代码中一定有什么东西没有显示出来。