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 def plot2(): 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): |
提前致谢。
用你的数据为我工作。
更改代码并将打印语句放入函数中。
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() |
你的代码中一定有什么东西没有显示出来。
- 好点子!通过这样做,我发现 matplotlib 出于某种原因不喜欢关闭列表名称,我已经更改了它并且它工作得很好。非常感谢你的帮助。
来源:https://www.codenong.com/17008951/