Mar-20-2024, 04:24 AM
I figured out the problem. I posted my answer to Stack Exchange. Just gonna leave a copy here.
What changed: added the line
#setup second axis of plot 2 ax22.clear() ln4 = ax22.plot(x2,y22,colors[1],label = 'CO2(%)') ax22.tick_params(axis = 'y',labelcolor = colors[1]) #---------------------------------------------# #set the x-axis tick labels visibility to False plt.setp(ax21.get_xticklabels(),visible = False) #----------------------------------------------# #keep two legends from the two axes together. legends belong to an axis. In our case, we have two y-axis, so two separate legend made rather than together lns2 = ln3 + ln4 labs2 = [l.get_label() for l in lns2] ax21.legend(lns2,labs2,bbox_to_anchor = (0,-0.1,1,0) # (0,1.02,1,0.102) ,loc = 'lower left',ncol = 3,borderaxespad = 0)Final picture:
What changed: added the line
plt.setp(ax21.get_xticklabels(),visible = False)
and change bbox_to_anchor
parameters to (0,-0.1,1,0)