

The argument bbox_inches = "tight" to plt.savefig can be used to save the figure such that all artist on the canvas (including the legend) are fit into the saved area. Saving the figure with bbox_inches = "tight" Unfortunately, the legend is not taken into account in this automatism, but we can supply a rectangle box that the whole subplots area (including labels) will fit into. Using plt.tight_layout Allows to automatically adjust the subplot parameters such that the elements in the figure sit tight against the figure edges. Leaves 30% space on the right-hand side of the figure, where one could place the legend. One can adjust the subplot parameters such, that the axes take less space inside the figure (and thereby leave more space to the legend) by using plt.subplots_adjust. Having placed the legend outside the axes often leads to the undesired situation that it is completely or partially outside the figure canvas. This is shown in the example l5 from above, where the bbox_transform argument is used to put the legend in the lower left corner of the figure. Sometimes it may be useful to specify the bounding box in figure coordinates instead of axes coordinates. For a vertically expanded legend, see this question. The mode="expand" expands the legend horizontally inside the bounding box given by the 4-tuple.
#Matlab subplot legend being cut off how to#
L6 = plt.legend(bbox_to_anchor=(0.4, 0.8), loc="upper right")ĭetails about how to interpret the 4-tuple argument to bbox_to_anchor, as in l4, can be found in this question. L5 = plt.legend(bbox_to_anchor=(1, 0), loc="lower right", L2 = plt.legend(bbox_to_anchor=(1.04, 0), loc="lower left", borderaxespad=0) Places the legend outside the axes, such that the upper left corner of the legend is at position (1.04, 1) in axes coordinates.įurther examples are given below, where additionally the interplay between different arguments like mode and ncols are shown. This creates a zero span box, out of which the legend will expand in the direction given by the loc argument. One can restrict oneself to supply only the (x0, y0) part of the bbox.

plt.legend(loc=(1.04, 0))Ī more versatile approach is to manually specify the bounding box into which the legend should be placed, using the bbox_to_anchor argument. To place the legend outside of the axes bounding box, one may specify a tuple (x0, y0) of axes coordinates of the lower left corner of the legend.

You might also take a look at plt.figlegend().Ī legend is positioned inside the bounding box of the axes using the loc argument to plt.legend.Į.g., loc="upper right" places the legend in the upper right corner of the bounding box, which by default extents from (0, 0) to (1, 1) in axes coordinates (or in bounding box notation (x0, y0, width, height) = (0, 0, 1, 1)).

Have a look at the matplotlib legend guide. # Shrink current axis's height by 10% on the bottomĪx.set_position([box.x0, box.y0 + box.height * 0.1,Īx.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), # Put a legend to the right of the current axisĪx.legend(loc='center left', bbox_to_anchor=(1, 0.5))Īnd in a similar manner, shrink the plot vertically, and put a horizontal legend at the bottom: import matplotlib.pyplot as plt Line, = ax.plot(x, i * x, label='$y = %ix$'%i)Īx.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05),Īlternatively, shrink the current plot's width, and put the legend entirely outside the axis of the figure (note: if you use tight_layout(), then leave out ax.set_position(): import matplotlib.pyplot as pltĪx.set_position() Similarly, make the legend more horizontal and/or put it at the top of the figure (I'm also turning on rounded corners and a simple drop shadow): import matplotlib.pyplot as plt If we do the same thing, but use the bbox_to_anchor keyword argument we can shift the legend slightly outside the axes boundaries: import matplotlib.pyplot as plt So, let's start with a generic example: import matplotlib.pyplot as plt To add to what Christian Alis and Navi already said, you can use the bbox_to_anchor keyword argument to place the legend partially outside the axes and/or decrease the font size.īefore you consider decreasing the font size (which can make things awfully hard to read), try playing around with placing the legend in different places: There are a number of ways to do what you want.
