0

I am trying to plot multiple graphs on a single set of axis.

I have a 2D array of data and want to break it down into 111 1D arrays and plot them. Here is an example of my code so far:

from numpy import *
import matplotlib.pyplot as plt

x = linspace(1, 130, 130) # create a 1D array of 130 integers to set as the x axis
y = Te25117.data # set 2D array of data as y
plt.plot(x, y[1], x, y[2], x, y[3]) 

This code works fine, but I cannot see a way of writing a loop which will loop within the plot itself. I can only make the code work if I explicitly write a number 1 to 111 each time, which is not ideal! (The range of numbers I need to loop over is 1 to 111.)

1
  • 2
    Welcome to StackOverflow! Please could you give a simple example of your dataset, that way we can just paste and go (rather than try and create one which satisfies your criteria) :) Commented Dec 10, 2012 at 11:12

2 Answers 2

3

Let me guess...long time matlab user? Matplotlib automatically add a line plot to the present plot if you don't create a new one. So your code can be simply:

from numpy import *
import matplotlib.pyplot as plt

x = linspace(1, 130, 130) # create a 1D array of 130 integers to set as the x axis
y = Te25117.data # set 2D array of data as y
L = len(y) # I assume you can infere the size of the data in this way...
#L = 111 # this is if you don't know any better
for i in range(L)
    plt.plot(x, y[i], color='mycolor',linewidth=1) 
Sign up to request clarification or add additional context in comments.

Comments

0
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2])
y = np.array([[1,2],[3,4]])

In [5]: x
Out[5]: array([1, 2])

In [6]: y
Out[6]: 
array([[1, 2],
       [3, 4]])

In [7]: for y_i in y:
  ....:     plt.plot(x, y_i)

Will plot these in one figure.

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.