How to insert a legend in a subplot secondary y axis and twinx

302 Views Asked by At

Hi all my code below works with the exception of the legend. I tried to brute force something to come up but the actual lines in each legend don't match.

After correcting for the get_labels_handles the code below is still not correct. The below creates graphs with a legend showing 4 lines (so I have to remove one of the existing legends. I think its the secondary_y axis argument that is causing my problems that isn't in the solutions that I can find). The labels (test1,test2,test3) are ignored and the legend just shows the text of the column headers of the dataframes.

The issue appears to be that I have secondary_y axis and then also use a twinx. I think using 2 twinx (and moving the spines) is probably the only way to go to get a correct legend up.

state = ['a','b','c','d']
city = ['a1','b1','c1','d1']

nrows=2 
ncols=2
i=0
fig,ax = plt.subplots(nrows,ncols,figsize=(20,6*nrows))
    
for row in range(nrows):
    for col in range(ncols): 
        
        state_ = state[i]
        city_ = city[i]

        df_state_approvals_original[[state_]].plot(ax=ax[row,col],label='test1')
        ax2= ax[row,col].twinx()
        ax2.spines['right'].set_position(('outward', 60))

        df_mean_price_state[[state_]].plot(ax=ax[row,col],secondary_y=True,label='Test2')

        df_annual_price_change_city[[city_]].plot(ax=ax2,color='red',ls='--',label = 'Test3')
          
        
        #lns=['Dwelling Approvals (lhs)',city_ + ' annual property price % chng (rhs2)','Mean property price (rhs1)']
        #fig.legend(labels=lns,loc='upper left', bbox_to_anchor=(0,1), bbox_transform=ax[row,col].transAxes)

        lines, labels =ax[row,col].get_legend_handles_labels()
        lines2, labels2=ax2.get_legend_handles_labels()
        
        ax2.legend(lines+lines2, labels + labels2,loc=0)
        
        ax[row,col].set_ylabel("Y1")
        ax[row,col].right_ax.set_ylabel('Y2')
        ax2.set_ylabel("T3")
        ax[row,col].title.set_text('Title')
        
        
        i=i+1
        
fig.subplots_adjust(wspace=0.4, hspace=0.25);
1

There are 1 best solutions below

0
On

Solution was changing my plot argument and then collecting handles and labels. df.plot (ax=ax) doesn't work but ax.plot(df.index, df.values, labels ='test','color='red') works

    ax[row,col].plot(df_state_approvals_original.index,df_state_approvals_original[[state_]], label = 'TEST1')
            ax2 = ax[row,col].twinx()
            ax3 = ax[row,col].twinx()
            
            ax2.plot(df_mean_price_state.index,df_mean_price_state[[state_]], label = 'TEST2', color='lightblue')
    
            ax2.spines['right'].set_position(('outward', 60))
        
            ax3.plot(df_annual_price_change_city.index, df_annu_al_price_change_city[[city_]], label = 'TEST3', color='red',ls='--' )

        lines, labels = ax[row,col].get_legend_handles_labels()
        lines2, labels2 = ax2.get_legend_handles_labels()
        lines3, labels3 = ax3.get_legend_handles_labels()
        
        handles = lines + lines2 + lines3
        labels_ = labels + labels2 +labels3
        
        ax3.legend(handles, labels_,loc=0)