My matplotlib.pyplot legend is being cut off

PythonMatplotlib

Python Problem Overview


I'm attempting to create a plot with a legend to the side of it using matplotlib. I can see that the plot is being created, but the image bounds do not allow the entire legend to be displayed.

lines = []
ax = plt.subplot(111)
for filename in args:
    lines.append(plt.plot(y_axis, x_axis, colors[colorcycle], linestyle='steps-pre', label=filename))
ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

This produces: enter image description here

Python Solutions


Solution 1 - Python

Eventhough that it is late, I want to refer to a nice recently introduced alternative:

New matplotlib feature: The tight bounding box

If you are interested in the output file of plt.savefig: in this case the flag bbox_inches='tight' is your friend!

import matplotlib.pyplot as plt

fig = plt.figure(1)
plt.plot([1, 2, 3], [1, 0, 1], label='A')
plt.plot([1, 2, 3], [1, 2, 2], label='B')
plt.legend(loc='center left', bbox_to_anchor=(1, 0))

fig.savefig('samplefigure', bbox_inches='tight')

Output file: samplefigure.png

I want to refer also to a more detailed answer: Moving matplotlib legend outside of the axis makes it cutoff by the figure box

Advantages

  • There is no need to adjust the actual data/picture.

  • It is compatible with plt.subplots as-well where as the others are not!

  • It applies at least to the mostly used output files, e.g. png, pdf.

Solution 2 - Python

As pointed by Adam, you need to make space on the side of your graph. If you want to fine tune the needed space, you may want to look at the add_axes method of matplotlib.pyplot.artist.

Below is a rapid example:

import matplotlib.pyplot as plt
import numpy as np

# some data
x = np.arange(0, 10, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)

# plot of the data
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.6, 0.75])
ax.plot(x, y1,'-k', lw=2, label='black sin(x)')
ax.plot(x, y2,'-r', lw=2, label='red cos(x)')
ax.set_xlabel('x', size=22)
ax.set_ylabel('y', size=22)
ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

plt.show()

and the resulting image: image

Solution 3 - Python

Just use plt.tight_layout()

import matplotlib.pyplot as plt
    
fig = plt.figure(1)
plt.plot([1, 2, 3], [1, 0, 1], label='A')
plt.plot([1, 2, 3], [1, 2, 2], label='B')
plt.legend(loc='center left', bbox_to_anchor=(1, 0))

plt.tight_layout()

This is probably introduced in the newer matplotlib version and neatly does the job.

Solution 4 - Python

Here is another way of making space (shrinking an axis):

# get the current axis
ax = plt.gca()
# Shink current axis by 20%
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

where 0.8 scales the width of the axis by 20%. On my win7 64 machine, using a factor greater than 1 will make room for the legend if it's outside the plot.

This code was referenced from: https://stackoverflow.com/questions/4700614/how-to-put-the-legend-out-of-the-plot

Solution 5 - Python

Edit: @gcalmettes posted a better answer.
His solution should probably be used instead of the method shown below.
Nonetheless I'll leave this since it sometimes helps to see different ways of doing things.


As shown in the legend plotting guide, you can make room for another subplot and place the legend there.

import matplotlib.pyplot as plt
ax = plt.subplot(121) # <- with 2 we tell mpl to make room for an extra subplot
ax.plot([1,2,3], color='red', label='thin red line')
ax.plot([1.5,2.5,3.5], color='blue', label='thin blue line')
ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.show()

Produces:

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1005909View Question on Stackoverflow
Solution 1 - PythonstrpeterView Answer on Stackoverflow
Solution 2 - PythongcalmettesView Answer on Stackoverflow
Solution 3 - PythonM.RezaView Answer on Stackoverflow
Solution 4 - PythonecoeView Answer on Stackoverflow
Solution 5 - Pythonmechanical_meatView Answer on Stackoverflow