# set ylim to match the largest range of any subplotĪx_invis = fig.add_subplot(111, frameon=False) Here I've hard coded the amount to shrink, but you can play around to find a number that works for you or calculate it like in the method above. Set_shared_ylabel(axes, 'common X', 'common Y')Īlternatively, if you are fine with colorless axis, I've modified Julian Chen's solution so ylabel won't overlap with tick labels.īasically, we just have to set ylims of the colorless so it matches the largest ylims of the subplots so the colorless tick labels sets the correct location for the ylabel.Īgain, we have to shrink the plot to prevent clipping. It works for the following example, while Hagne's answer won't draw ylabel (since it's outside of the canvas) and KYC's ylabel overlaps with the tick labels: import matplotlib.pyplot as pltįig, axes = plt.subplots(3, 4, sharey='row', sharex=True, squeeze=False)įor i, a in enumerate(itertools.chain(*axes)): # note that (figleftpad-labelpad) refers to the middle of the ylabelĪ.t_label_coords(figleftpad-labelpad,(bottom + top)/2, transform=f.transFigure)Īt.set_xlabel('') # just to make sure we don't and up with multiple labelsīboxes, _ = at.xaxis.get_ticklabel_extents()īboxes = bboxes.inverse_transformed(fig.transFigure)Ī_label_coords((left + right) / 2, tick_label_bottom - labelpad, transform=fig.transFigure) Plt.subplots_adjust(left=(x1 - tick_label_left) + figleftpad) # figleftpad is additional padding to fit the ylabel # basically how much padding is needed to fit tick labels in the figure # (x1 - tick_label_left) is the x coordinate of right end of tick label, # shrink plot on left to prevent ylabel clipping # get the coordinates of the left side of the tick labelsĪt.set_ylabel('') # just to make sure we don't and up with multiple labelsīboxes, _ = at.yaxis.get_ticklabel_extents(f.canvas.renderer)īboxes = bboxes.inverse_transformed(f.transFigure) Sets the padding between ticklabels and axis label"""į.canvas.draw() #sets f.canvas.renderer needed below def set_shared_ylabel(a, xlabel, ylabel, labelpad = 0.01, figleftpad=0.05): I've modified Hagne's answer so it works with more than 1 column of subplots, for both xlabel and ylabel, and it shifts the plot to keep the ylabel visible in the figure. The ylabel will either overlap with ticks, be clipped on the left or completely invisible/outside of the figure. The methods in the other answers will not work properly when the yticks are large.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |