0

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.

Edit: ***************************************

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.

Final edit ***************** 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);
  • See [Secondary axis with twinx(): how to add to legend?](https://stackoverflow.com/questions/5484922/secondary-axis-with-twinx-how-to-add-to-legend) – JohanC Mar 05 '22 at 01:00
  • Thanks Johan. I did have a look at that post. But i tried the solutions and end up with different results including that I have to remove legends. Also the labels that I've written are being ignored and the column headers are instead used in the legend – pythonpypy Mar 06 '22 at 23:18
  • So, you might create a minimal reproducible example? Including some test data so the code can run stand-alone? You could also [edit](https://stackoverflow.com/posts/71358166/edit) your post and replace `get_legend_handles` by `get_legend_handles_labels`. – JohanC Mar 06 '22 at 23:31
  • Thanks Johan i've just updated my code. I think its the secondary_y axis causing my problems. But i'm not sure why the labels are also being ignored – pythonpypy Mar 06 '22 at 23:34
  • Could you please remove the wrong code from your post? Could you also add some reproducible test data? See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – JohanC Mar 06 '22 at 23:40
  • One of the problems might be that you are using `twinx` as well as `secondary_y=True`. You should either use one or the other. – JohanC Mar 06 '22 at 23:48
  • Thanks Johan I think i will create 2 twinx. I'm sure that will work. Cheers – pythonpypy Mar 06 '22 at 23:55

1 Answers1

0

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)