2

I use the following code for plotting and saving 8 figures. This code does not show all 8 figures in one time. How to do that? because now we should close one figure and another one appears.

and how can change the name and title of figure once for each figure using the values of data in data file? for example Fig1: ng = 100, Fig2: ng = 250 and so on. 100 and 250 are the data inside the datafile.

Thank you

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('Fig 1: ng = 100 ')  # we need to change this value for each figure and number of the figure
    plt.savefig('Fig1:T_j.png') # number of the figure should change
    plt.show()
Ma Y
  • 696
  • 8
  • 19

1 Answers1

0

You can use f string formatting:

ax1.title.set_text(f'Fig {x}: TG = {Tg[x]}, ng = {ng[x]} ')
plt.savefig(f'Fig1:T_{x}.png')

Assuming ng and x are the values you want to utilize.

Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69
  • and this way put the values of `Tg[x]` and `ng[x]` in the title? – Ma Y Nov 17 '19 at 11:27
  • I put them, but it shows all the value of `ng` in one plot and no for other plots. And My first question was about having all 8 plots in one time. – Ma Y Nov 17 '19 at 11:31
  • I did what you are updating, but it just works for first plot. Im confused how to do all plot in one time with this code together – Ma Y Nov 17 '19 at 11:35
  • F strings will insert any variable into the string. Which variable of yours is dynamic? Put that in the for loop and your names will be corrected. – Yaakov Bressler Nov 17 '19 at 12:37
  • One thing is misunderstood. First I should do plots simultaneously. because in this current code of mine, I cannot plot 8 plots in one time. – Ma Y Nov 17 '19 at 12:39
  • I have to close one to see another one – Ma Y Nov 17 '19 at 12:39
  • Consider clarifying in your Q above @MaY – Yaakov Bressler Nov 17 '19 at 12:41
  • in my code I should have 8 plots. when I run this code I have a plot, when I close it, another one appears unill the 8th plot. But I should have 8plots in once – Ma Y Nov 17 '19 at 12:43
  • Consider utilizing subplots. – Yaakov Bressler Nov 17 '19 at 12:50
  • I appreciate if you could help me, because I have never used python plot before. and in these days I got stuck into in alot – Ma Y Nov 17 '19 at 12:53
  • I'm sorry, your Q is too confusing at this point. I'm not sure I understand what you're trying to accomplish. – Yaakov Bressler Nov 17 '19 at 13:13