Reputation: 31
I'm new to Python and struggling with plotting many curves on one graph with matplotlib.
I have 50 data files. I've read them all in and stored them in a 3D numpy list. First dimension is the filenumber, 2nd dimension is the time, 3rd dimension is the parameter being measured (time, pressure, ...). For a single file the data might look like this:
(time) (pressure) 1 3.4 2 5.1 3 5.1 4 5.1 5 7.8
I want to plot all fifty time vs. pressure curves on one graph. What I have so far is:
plt.plot(\
clipData[0,:,0], clipData[0,:,1],\
clipData[1,:,0], clipData[1,:,1],\
clipData[2,:,0], clipData[2,:,1])
plt.show()
This works fine for the first three curves, but I figure there must be a way to do this with out manually generating the list out to 50. I thought about using a loop to build the list as a long string and then shoving this string into plot().
guts = 'clipData[0,:,0], clipData[0,:,1]'
plt.plot(guts)
plt.show
But my simple test made it clear I was doing something very wrong.
Any help would be appreciated, I'm sure there's an elegant way to do this.
Upvotes: 2
Views: 148
Reputation: 12590
You can pass two 2D arrays to plot
. Each column in the first array will be plotted against the corresponding column of the second array.
plt.plot(clipData[:, :, 0].T, clipData[:, :, 1].T)
Plotting in this way you are somewhat limited when specifying line properties, in particular you can't pass a label for each line. For full control over line properties plotting in a loop can be more convenient.
Upvotes: 1
Reputation: 3852
for i in range(0,len(clipData)):
plt.plot(clipData[i,:,0],clipData[i,:,1])
plt.show()
This way your plotting routine will run for any length of clipData or if you change it in the future.
Upvotes: 0
Reputation: 2788
Maybe something like :
for i in range(50):
plt.plot(clipData[i,:,0], clipData[i,:,1])
plt.show()
Because when you use
guts = 'clipData[0,:,0], clipData[0,:,1]'
guts
is just a string which cannot be plotted.
Upvotes: 1