Reputation: 109
I would like to plot a single row from a 2-dimensional numpy array against a 1d list in python. For example, I would like to plot using matplotlib row 'i' as below
|0 0 0 0 0|
|1 1 1 1 1|
i |2 2 2 2 2|
|. . . . .|
|n n n n n|
against
[0, 100, 200, 300, 400]
What I have currently is:
plt.plot(list1, 2dimArray[i])
but this is not working. I had this functionality working when I was plotting 1d lists against 1d lists but I had to go multidimensional and chose numpy.
Is there anyway to do this?
Upvotes: 3
Views: 9360
Reputation: 46530
Using the data from your comment below, this works for me:
In [1]: import numpy as np
In [2]: x = np.arange(0,1100,100)
In [3]: y = np.random.rand(6,11)
In [4]: i = 2
In [5]: plt.plot(x, y[i])
Out[5]: [<matplotlib.lines.Line2D at 0x1043cc790>]
The the thing is that the x
and y
arguments to plot
must have the same shape (or at least the same first entry to shape).
In [6]: x.shape
Out[6]: (11,)
In [7]: y.shape
Out[7]: (6, 11)
In [8]: y[i].shape
Out[8]: (11,)
Perhaps one of your items generated by your program doesn't actually have the shape you believe it does?
This should also work if you use a list together with a numpy array (plt.plot
will probably convert the list to an array):
In [9]: xl = range(0, 1100, 100)
In [10]: plt.plot(xl, y[i])
Out[10]: [<matplotlib.lines.Line2D at 0x10462aed0>]
Upvotes: 2