4

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)

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.