1 Replies - 236 Views - Last Post: 19 November 2019 - 11:41 AM Rate Topic: -----

#1 esadri21   User is offline

  • D.I.C Head

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

Legend in for loop just shows the label of the last curve

Posted 19 November 2019 - 03:58 AM

I try to put the labels of 7 curves inside the legend, but when plots are shown the label of 7th curve is on the plot and the other 6 curves do not have any labels.

I tried to use plt.legend() in different indentation, but inside the 3rd for loop we have more than 7 labels because for loop of z like 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3... in other for loops the legend cannot appear.

we should have 1 2 3 4 5 6 7. Any help please?

for x in range(len(ng)):
    fig, (ax1) = plt.subplots(1,figsize=(9,6))
    plt.subplots_adjust(wspace=0.5,hspace=0.1)
    for xx in range(1,819):
            Ju_list=[]
            ET_list=[]
            ET1_list=[]
            Unu_list=[]
            z_list=[]
            uf_list=[]
            for z in np.arange(0,11):
                    Ju = dfimppara.iloc[xx, 1]
                    Jl = dfimppara.iloc[xx, 2]
                    lim = Ju - Jl
                    if lim > 1:
                        pass
                    else:
                        if Ju<8:
                           
                            T = ET(xx, z, ng[x], 1e-24, Tg[x], 1)
                            BC = Bcmb(xx,z)
                            Btex = B(xx,T)
                            ET_list.append(1-BC/Btex)
                            #Unf_list.append(EFIR (xx, z, 100, 1e9, 3, 1))
                            z_list.append(z)
                                                                    
                            l1,=ax1.plot(z_list, ET_list)
                            
                            ax1.title.set_text(f'UF=1e-24,Tg={Tg[x]},ng={ng[x]}')
                            ax1.set_ylabel('1-BB(CMB)/BB(Tex)')
                            ax1.set_xlabel('z')
                            plt.legend([l1],[f'{dfimppara.iloc[xx, 1]}'])
                            
                            
                        else:
                            pass

    
    plt.show()



Is This A Good Question/Topic? 0
  • +

Replies To: Legend in for loop just shows the label of the last curve

#2 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 562
  • View blog
  • Posts: 1,798
  • Joined: 27-December 13

Re: Legend in for loop just shows the label of the last curve

Posted 19 November 2019 - 11:41 AM

I'm not 100% sure here, but in line 32 you assign a legend to plt; i.e. the full plot.
I guess this is why you only see one (the last assigned) legend.
You need to assign the legends to the individual sub-plots. Maybe: l1.legend(...)

It is difficult to help with this code, partly because it is poorly written:
Variable names without meaning (x, xx, z, etc) makes understanding the code problematic.
A name like "l1" is asking for problems; almost unreadable in some fonts.
You should use white-space according to python practice, after komma and on both sides of operators.
Convoluted code, line 15-18 + 35, 36 simplifies to a single line:
if lim <= 1 and Ju < 8:

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1