Python Forum
Multiple plots with matplotlib - looping
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple plots with matplotlib - looping
#1
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
Reply
#2
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
Reply
#3
(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
Reply
#4
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))
Reply
#5
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
Reply
#6
hey you can use subplots if it suits you..
Reply
#7
(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.
Reply
#8
@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 =)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiple Plots in Spyder PythonAndMe2023 0 822 Feb-03-2023, 07:00 PM
Last Post: PythonAndMe2023
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,577 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Multiple user defined plots with secondary axes using for loop maltp 1 1,393 Apr-30-2022, 10:19 AM
Last Post: maltp
  Matplotlib multiple set_over / under G_rizzle 0 1,062 Feb-14-2022, 12:13 PM
Last Post: G_rizzle
  Matplotlib - close multple plots with user input Positron79 0 1,699 Dec-01-2021, 05:26 PM
Last Post: Positron79
  Subplot - Plotting 4 plots on the same row Menthix 1 1,392 Nov-07-2021, 09:03 PM
Last Post: deanhystad
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,163 Mar-11-2021, 10:52 AM
Last Post: buran
  Sharing X Axis in Sub plots JoeDainton123 1 2,142 Aug-22-2020, 04:11 AM
Last Post: deanhystad
  Help With Sub Plots JoeDainton123 0 1,676 Aug-20-2020, 10:48 PM
Last Post: JoeDainton123
  Group scatter plots Mekala 0 1,620 Jul-23-2020, 02:18 PM
Last Post: Mekala

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020