7 Replies - 199 Views - Last Post: 17 November 2019 - 10:42 AM Rate Topic: -----

#1 esadri21   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 09-November 17

Plotting figures inside the forloop in one run in Python

Posted 17 November 2019 - 06:50 AM

In the following code Im trying to plot 8 figures and to save all of them with specific name.

But the problem in first step is that 8 figures are not plotted simultaneously, they appear once we close previous one.

Any help please?

row, uf, ng, Tg = np.genfromtxt('texdata.txt',unpack=True)
fig, (ax1) = plt.subplots(1)
for x in range(len(ng)):
    for xx in range(1,819):
            ET_list=[]
            z_list=[]
            for z in np.arange(1,7):
                    Ju = dfimppara.iloc[xx, 1]
                    Jl = dfimppara.iloc[xx, 2]
                    lim = Ju - Jl
                    if lim > 1:
                        pass
                    else:
                        if Ju<7:
                            ET_list.append(ET(xx, z, 100, 1e9, 1, ng[x], 0, Tg[x], 1))
                            z_list.append(z)                  
                            plt.plot(z_list, ET_list))
                        else:
                            pass
    ax1.title.set_text(f'Fig {x}: TG = {Tg[x]}, ng = {ng[x]} ')  # we need to change this value for each figure and number of the figure
    plt.savefig(f'Fig{row[x]}.png') # number of the figure should change
    plt.show()


Is This A Good Question/Topic? 0
  • +

Replies To: Plotting figures inside the forloop in one run in Python

#2 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 561
  • View blog
  • Posts: 1,794
  • Joined: 27-December 13

Re: Plotting figures inside the forloop in one run in Python

Posted 17 November 2019 - 08:45 AM

I'm unsure of your target; do you want 8 small sub plot in the same view?
Please explain.
Was This Post Helpful? 0
  • +
  • -

#3 esadri21   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 09-November 17

Re: Plotting figures inside the forloop in one run in Python

Posted 17 November 2019 - 08:54 AM

By the way I prefer to have them separately. But in this code one plot appears, when we close it the next one appears untill the 8th. but I want to see 8 windows of plots. I could not manage to find any way. mm Im very amateur in Python plot
Was This Post Helpful? 0
  • +
  • -

#4 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 561
  • View blog
  • Posts: 1,794
  • Joined: 27-December 13

Re: Plotting figures inside the forloop in one run in Python

Posted 17 November 2019 - 10:11 AM

Are you looking for subplots like this:

Posted Image

Copied form here: https://towardsdatas...ry-b5d1b8f67596
Was This Post Helpful? 0
  • +
  • -

#5 esadri21   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 09-November 17

Re: Plotting figures inside the forloop in one run in Python

Posted 17 November 2019 - 10:17 AM

this one or some thing like separate windows not in one. both are ok.
but here we have to use for loops or read the data from file. we cannot for each subplot write a line as you wrote for 4 subplots, 4 line, because consider we have 200 plots, or 50, or another numbers of plots. so In this case I have to plot all of them by reading data from for loops or from data file and then each one should be saved. This is what I could not to do.
Was This Post Helpful? 0
  • +
  • -

#6 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 561
  • View blog
  • Posts: 1,794
  • Joined: 27-December 13

Re: Plotting figures inside the forloop in one run in Python

Posted 17 November 2019 - 10:36 AM

Basically, you want to save the plots - without first displaying them to the screen?
I never did this, but maybe simply skip the plt.show() line.
Was This Post Helpful? 0
  • +
  • -

#7 esadri21   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 09-November 17

Re: Plotting figures inside the forloop in one run in Python

Posted 17 November 2019 - 10:38 AM

no iwant too see them and then save them. but in this code we cannot see them simultaneously. i need to see 8 figures at once similar to the results of this code:


import matplotlib.pyplot as plt
x=[[1,2,3,4],[12,16,38,50],[10,12,13,14],[21,22,33,34]]
y=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
for i in range(len(x)):
    plt.figure()
    plt.plot(x[i],y[i])
plt.show()


you can see that 4 plots will be shown together at once
Was This Post Helpful? 0
  • +
  • -

#8 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 561
  • View blog
  • Posts: 1,794
  • Joined: 27-December 13

Re: Plotting figures inside the forloop in one run in Python

Posted 17 November 2019 - 10:42 AM

What happens if you out-dent line 22 of post #1 ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1