matplotlib错误:ValueError:x和y必须具有相同的第一个维度

时间:2013-06-09 11:05:15

标签: matplotlib dimension

我正在尝试使用matplotlib绘制两个列表,但是我收到有关x和y维度的错误。其中一个列表包含日期和其他数字,您可以看到列表的内容,我在下面打印过。

我尝试用len()检查列表的长度,它们似乎相等,所以我有点迷失。我没有太多运气就检查了这个错误。

注意:“query”包含我的SQL查询,为简单起见,我没有包含该查询。

#####我的代码
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()

我的剧本输出:

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 个答案:

答案 0 :(得分:0)

为您提供数据。

更改代码并将print语句放在函数中。

def plot2():
    print "date = ", date
    print "close = ", close
    print "date length = ", len(date)
    print "close length = ", len(close)
    plt.plot(date, close)
    plt.show()

您的代码必须有一些不会显示的内容。