

Because of the default spacings between axes and figure edge, this suffices to place the legend such that it does not overlap with the pie. Here (1,0) is the lower right corner of the figure. plt.legend(pie,labels, bbox_to_anchor=(1,0), loc="lower right", The coordinates given to bbox_to_anchor are then interpreted as figure coordinates. To this end, one would use the bbox_transform argument and supply the figure transformation to it. The advantage is that this will allow to simply place the legend in one corner of the figure without adjusting much of the rest. Instead of specifying the legend position in axes coordinates, one may use figure coordinates. There are some useful alternatives to this approach: Using figure coordinates
SUBPLOT LEGEND CONSISTENT LEGEND LOCATION MATPLOTLIB HOW TO
How to use or interprete this is detailed in this question: What does a 4-element tuple argument for 'bbox_to_anchor' mean in matplotlib?Īnd one may then use the mode="expand" argument to make the legend fit into the specified bounding box. There is also the option to use a 4-tuple to bbox_to_anchor. In order for the legend not to exceed the figure, we use plt.subplots_adjust to obtain more space between the figure edge and the axis, which can then be taken up by the legend. Pie = plt.pie(total, startangle=90, colors=, Plt.title('Origin of Miss Universe since 1952') Labels = ["Earth", "Mercury", "Venus", "Mars", "Jupiter", "Saturn",

This concept is used for the above plot, which tells us the shocking truth about the bias in Miss Universe elections. if loc is "upper left", the upper left corner of the legend is at position (1,1) and the legend will expand to the right and downwards. It will then expand out of the zero-size bounding box. It then acts as a point relative to which the legend will be placed according to the loc argument. bbox_to_anchor=(1,1) it means that the bounding box is located at the upper right corner of the axes and has no extent. If bbox_to_anchor is given a 2-tuple e.g. However, we may specify our own bounding box using the bbox_to_anchor argument. If no further argument is specified, this bounding box will be the entire axes. plt.legend(loc="upper left") placed the legend such that it sits in the upper left corner of its bounding box. The most important and in itself sufficient is the loc argument.Į.g. Plt.legend() has two main arguments to determine the position of the legend. If the need for a pie chart is unambiguously determined, let's proceed to place the legend. In many cases pie charts are not the best way to convey information. Step 2: Making sure, a pie chart is needed. If indeed the plot cannot live without a legend, proceed to step 2. In many cases no legend is needed at all and the information can be inferred by the context or the color directly: The long version: Step 1: Making sure a legend is needed.

# to place center point specified by loc at the position specified by bbox_to_anchor!įig.legend(,, loc='lower center', ncol=2,ībox_to_anchor=, bbox_transform=fig.The short answer is: You may use plt.legend's arguments loc, bbox_to_anchor and additionally bbox_transform and mode, to position the legend in an axes or figure. Ncol=2, fancybox=True, shadow=True, prop=) I can add a nice looking figure legend with fig.legend((l1, l2),, loc="lower center",

I have a figure with two subplots as 2 rows and 1 column.
