3

Consider a small numpy array:

array([[  0.,   1.,   0.,   1.,   0.,   0.,   0.,   0.,   0.,   1.],
   [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
   [  0.,   0.,   0.,   0.,  18.,  15.,  25.,   0.,   0.,   0.],
   [  0.,   0.,   0.,  23.,  19.,  20.,  20.,   0.,   0.,   0.],
   [  0.,   0.,  20.,  22.,  26.,  23.,  18.,   0.,   0.,   0.],
   [  0.,   0.,   0.,  23.,  16.,  20.,  13.,   0.,   0.,   0.],
   [  0.,   0.,   0.,   0.,  18.,  20.,  18.,   0.,   0.,   0.],
   [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
   [  0.,   1.,   0.,   0.,   1.,   0.,   0.,   0.,   0.,   0.]])

I would like to plot, let say from the row number 3 to the row number 6, i.e. a section of my numpy array(I am coming from matlab backgroud). How could I loop this? or How could I plot multiple rows of my numpy array in the same graph?

So far I have tried; I define an arbitrary x:

x = np.arange(0,10)

then If I use

plt.plot(x,data[3,:])

to plot the third row and It does fine. The problem arises if I try:

plt.plot(x,data[3:4,:])

I get the error "x and y must have same first dimension", which I understand because he stacks row number 3 and row number 4 together, so that x and y do not have the same dimension. How can I overcome that?

Thank you

2
  • you would like to plot row number 3 to row number 6 with what? I mean, do you need to plot row3 vs row6 or row3-row6 vs something else? Commented Oct 2, 2014 at 8:36
  • from row number 3 to row number 6 vs an arbitrary x contaning the same number elements (10) in a row Commented Oct 2, 2014 at 11:21

3 Answers 3

4

As the error implies, your data.shape = (1,10) is inconsistent with your input x.shape = (10,). To solve this problem you can just transpose your data using .T, i.e.

plt.plot(x, data[3:4,:].T)

Also, keep in mind that data[3:4,:] is the same as data[3,:], you will need to use data[3:5,:] to get the 3rd and 4th rows, for example.

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

Comments

2

Just a better application of psuedocubi's answer.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0,10,10)
y = np.array(YOUR DATA HERE)

plt.plot(x,a[3:4].T,'r--',label="x vs y1") #CONTAINS YOUR 3RD ROW
plt.plot(x,a[4:5].T,'g--',label="x vs y2") #CONTAINS YOUR 4TH ROW
plt.plot(x,a[5:6].T,'b--',label="x vs y3") #CONTAINS YOUR 5TH ROW
plt.legend(loc='best')
plt.xlabel("x")
plt.ylabel("y")
plt.show()

YOUR DATA PLOTTED WITH ARBITARY X

The x here as been plotted with your own data!

Comments

2

You can try:

for i in range(3):
    plt.plot( x , data[ i , : ] )

plt.show()

If you want a range of rows ,for example from 3 to 6 , you can use:

range(3,7,1) , where 1 is the step , 3 is the starting row and 7 is the last row we want to plot (6 ) plus one

1 Comment

It is not really what I am looking for..It works for the first 3 rows but I want to plot from row number 3 to number 6, How can I do that with a similar loop?

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.