Using matplotlib in python. The legend overlaps with my pie chart. Tried various options for "loc" such as "best" ,1,2,3... but to no avail. Any Suggestions as to how to either exactly mention the legend position (such as giving padding from the pie chart boundaries) or at least make sure that it does not overlap?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
The short answer is: You may use
plt.legend
's argumentsloc
,bbox_to_anchor
and additionallybbox_transform
andmode
, to position the legend in an axes or figure.The long version:
Step 1: Making sure a legend is needed.
In many cases no legend is needed at all and the information can be inferred by the context or the color directly:
If indeed the plot cannot live without a legend, proceed to step 2.
Step 2: Making sure, a pie chart is needed.
In many cases pie charts are not the best way to convey information.
If the need for a pie chart is unambiguously determined, let's proceed to place the legend.
Placing the legend
plt.legend()
has two main arguments to determine the position of the legend. The most important and in itself sufficient is theloc
argument.E.g.
plt.legend(loc="upper left")
placed the legend such that it sits in the upper left corner of its bounding box. If no further argument is specified, this bounding box will be the entire axes.However, we may specify our own bounding box using the
bbox_to_anchor
argument. Ifbbox_to_anchor
is given a 2-tuple e.g.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. It then acts as a point relative to which the legend will be placed according to theloc
argument. It will then expand out of the zero-size bounding box. E.g. ifloc
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.This concept is used for the above plot, which tells us the shocking truth about the bias in Miss Universe elections.
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.There is also the option to use a 4-tuple to
bbox_to_anchor
. 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?and one may then use the
mode="expand"
argument to make the legend fit into the specified bounding box.There are some useful alternatives to this approach:
Using figure coordinates
Instead of specifying the legend position in axes coordinates, one may use figure coordinates. The advantage is that this will allow to simply place the legend in one corner of the figure without adjusting much of the rest. To this end, one would use the
bbox_transform
argument and supply the figure transformation to it. The coordinates given tobbox_to_anchor
are then interpreted as figure coordinates.Here
(1,0)
is the lower right corner of the figure. 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.In other cases, one might still need to adapt those spacings such that no overlap is seen, e.g.
Saving the file with
bbox_inches="tight"
Now there may be cases where we are more interested in the saved figure than at what is shown on the screen. We may then simply position the legend at the edge of the figure, like so
but then save it using the
bbox_inches="tight"
tosavefig
,This will create a larger figure, which sits tight around the contents of the canvas:
A sophisticated approach, which allows to place the legend tightly inside the figure, without changing the figure size is presented here: Creating figure with exact size and no padding (and legend outside the axes)
Using Subplots
An alternative is to use subplots to reserve space for the legend. In this case one subplot could take the pie chart, another subplot would contain the legend. This is shown below.