Ma Y
Ma Y

Reputation: 706

Subplots in forloop do not appear in Python

In the following code as it is obvious we have subplot. the first for loop makes 8 plots or 4 plots with 2 subplots (4 *2 = 8). But the problem is in this code the first plot is shown and other ones do not appear. What should I do to see all plots offor x in range(len(ng)).

fig, (ax1,ax2) = plt.subplots(1,2)
for x in range(len(ng)):
    for xx in range(1,819):
            Ju_list=[]
            ET_list=[]
            ET1_list=[]
            Unu_list=[]
            z_list=[]
            uf_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:
                            Ju_list.append(dfimppara.iloc[xx, 1])
                            ET_list.append(ET(xx, z, ng[x], 1e-24, Tg[x], 1)/(2.73*(1+z)))
                            ET1_list.append(ET(xx, z, ng[x], 1e-20,Tg[x], 1)/(2.73*(1+z)))))
                            z_list.append(z)

                            ax1.plot(z_list, ET_list)#,label="test1")
                            ax1.title.set_text(f'Fig1:Tg={Tg[x]}')
                            ax2.plot(z_list, ET1_list)
                            ax2.title.set_text(f'Fig1:Tg={Tg[x]}')

                            #ax1.plot(Ju_list, Unu_list)

                        else:
                            pass


    plt.show()

Upvotes: 2

Views: 431

Answers (1)

Andrea
Andrea

Reputation: 3077

You are creating only one figure at the beginning using fig, (ax1,ax2) = plt.subplots(1,2). I think what you want to do is to create len(ng) figures:

for x in range(len(ng)):
    fig, (ax1,ax2) = plt.subplots(1,2)
    for xx in range(1,819):
            Ju_list=[]
            ET_list=[]
            ET1_list=[]
            Unu_list=[]
            z_list=[]
            uf_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:
                            Ju_list.append(dfimppara.iloc[xx, 1])
                            ET_list.append(ET(xx, z, ng[x], 1e-24, Tg[x], 1)/(2.73*(1+z)))
                            ET1_list.append(ET(xx, z, ng[x], 1e-20,Tg[x], 1)/(2.73*(1+z)))))
                            z_list.append(z)

                            ax1.plot(z_list, ET_list)#,label="test1")
                            ax1.title.set_text(f'Fig1:Tg={Tg[x]}')
                            ax2.plot(z_list, ET1_list)
                            ax2.title.set_text(f'Fig1:Tg={Tg[x]}')

                            #ax1.plot(Ju_list, Unu_list)

                        else:
                            pass


    plt.show()

Upvotes: 1

Related Questions