# 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 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. # 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)Ī.t_label_coords(tick_label_left - labelpad,(bottom + top)/2, transform=f.transFigure)į,a = plt.subplots(2, sharex=True, gridspec_kw=) Sets the padding between ticklabels and axis label"""į.canvas.draw() #sets f.canvas.renderer needed below import numpy as npĭef set_shared_ylabel(a, ylabel, labelpad = 0.01): This way you avoid problems mentioned by KYC. Here is a solution where you set the ylabel of one of the plots and adjust the position of it so it is centered vertically. Raise Exception("Unexpected axis: x or y") Va - vertical alignment (default: "center") Ha - horizontal alignment (default: "center") Labelpad - padding from the axis (default: 5) ''' Add super ylabel or xlabel to the figure My function can also be called after the plots have been created. Wen-wei Liao's answer doesn't, because fig.add_subplot(111) will return the same Axes object if it is already created. However, if you try to call it multiple times you will get text added on top of each other (as fig.suptitle does too). Therefore there is no axes artist being created and made colorless. My answer suplabel here is similar to the fig.suptitle which uses the fig.text function. Wen-wei Liao's answer is good if you are not trying to export vector graphics or that you have set up your matplotlib backends to ignore colorless axes otherwise the hidden axes would show up in the exported graphic. Plt.savefig('common_labels_text.png', dpi=300) Plt.savefig('common_labels.png', dpi=300)Īnother way is using fig.text() to set the locations of the common labels directly. # Turn off axis lines and ticks of the big subplotĪx.tick_params(labelcolor='w', top=False, bottom=False, left=False, right=False) Y2 = Īx = fig.add_subplot(111) # The big subplot You can create a big subplot that covers the two subplots and then set the common labels.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |