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);