开发者

ValueError while plotting in python

I want to use the plot method of the matplotlib and plot 2 arrays. The array to be plotted along the x-axis has 1 row and 128 columns [1,128]. The array to be plotted along the y-axis has 14 rows and 128 columns [14,128]. When I try to use the plot method, it returns this message:

ValueError: x and y must开发者_开发知识库 have same first dimension

This is the code that I am using to plot it. a and b are the 2 arrays.

line, = plt.plot(b, a, 'bs', markersize=4)


This error shows up when the size of a and b (taking from above example) is not the same - so, 128 x-values here should be plotted against 128 y-values.


You've just got your arrays the wrong way around. Transpose them and everything should work.

>>> from matplotlib import pyplot as plt
>>> import numpy as np
>>> x = np.array(range(1,129))
>>> y = np.random.rand(14,128)
>>> plt.plot(x, y)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 2286, in plot 
    ret = ax.plot(*args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 3783, in plot
    for line in self._get_lines(*args, **kwargs):
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 317, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 294, in _plot_args
    x, y = self._xy_from_xy(x, y)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 234, in _xy_from_xy
    raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension
>>> plt.plot(x.T, y.T)
# works
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜