Python Forum

Full Version: Multiple plots with matplotlib - looping
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I am tryting to draw multiple plots with matplot lib. plot_general_list is a list of lists - something like
plot_list = [list1, list2, list3, list4...]. So if there are 10 lists in plot_list, I would like to get 10 plots (with data of those lists).
I would like the data in those lists to be plotted in separate plots. I tried this...


for i in range(len(plot_list)):
    plt.figure()
    plt.plot(plot_list[i])
    plt.show
... but I get no figure drawn. matplotlib works fine since I tried the basic sample (plot[1,2,3,4]) and it works fine.
What did I miss in my code? Thank you!
JC
Hey JC,

From what I can see you're missing your () on your plt.show() method call.

It should be plt.show() instead of plt.show
(Sep-20-2016, 02:55 PM)Crimson King Wrote: [ -> ]Hey JC,

From what I can see you're missing your () on your plt.show() method call.

It should be plt.show() instead of plt.show

Hi Crimson King,

You are right, I really was missing parentheses. Although the effect I get now is that one plot shows at a time, until I close it and next one shows up.
Do you know of a way to get all plots to show up in one run? Thanks
JC,

To show the plots at the same time on different graphs you'd have to make the plt.show() call outside the for loop:



for i in plot_list:
    plt.figure()
    plt.plot(i)
plt.show
And if you want to show every plot from the list on the same graph you need to get rid of the plt.figure() call

for i in plot_list:
    plt.plot(i)
plt.show
PS: I changed your code a bit, that way of looping thru a list is more pythonic than doing it with i in range(len(plot_list))
Thanks Crimson King, I was looking for solution for showing all plots at same time on different graphs.

I still have issues when I want to show two graphs (with different functions on them). e.g.:


for i in plot_list1:
   plt.plot(i)
plt.show

for j in plot_list2:
   plt.plot()
plt.show
I get the second graph shown only after closing first one. Do you know how to get both graphs shown at same time, without having to close one first?

Ok, I somehow found the answer, though I can't really say I know how it works...

before each plot, I should add the line:
plt.figure()
like this:


plt.figure()
for i in plot_list1:
  plt.plot(i)

plt.figure()
for j in plot_list2:
  plt.plot()

plt.show
hey you can use subplots if it suits you..
(Sep-22-2016, 06:01 AM)j.crater Wrote: [ -> ]I still have issues when I want to show two graphs (with different functions on them).
You never actually call plt.show(). The parentheses are not optional.
@ujjwalrathod007 - thanks, I know about subplots, they really are handy in many situations =)
@nilamo - I copied the code from above, which doesn't have parentheses, my mistake, I have them now in code of course =)