Put Legend Outside Plot Matplotlib

When I first started working with Matplotlib, one of the things I quickly realized was how important clear and well-placed legends are for making plots understandable. In many cases, the default legend placement inside the plot area can clutter the visualization, especially when working with dense data or multiple plot elements.

Putting the legend outside the plot not only cleans up the graph but also makes it more professional and easier to interpret. Over the years, I’ve used several easy methods to achieve this, and in this article, I’ll share these techniques with you so that you can apply them effortlessly in your projects.

Methods to Put the Legend Outside the Plot?

Before getting into the methods, let me share why placing the legend outside the plot area is often beneficial:

  • Improved clarity: The plot area stays clean, giving more space to the data visualization itself.
  • Better aesthetics: Your charts look more professional, especially when presenting to clients or stakeholders.
  • Avoid overlapping: When you have many plot elements, legends inside the plot can overlap with data points or lines, making them hard to distinguish.

Read Matplotlib Invert y axis

Method 1: Use bbox_to_anchor with loc Argument

One of the most flexible ways I use to place the legend outside the plot is by combining the loc parameter with bbox_to_anchor. This method allows you to specify the exact position of the legend relative to the axes.

Here’s a quick example using a US state population scatter plot:

import matplotlib.pyplot as plt

states = ['California', 'Texas', 'Florida', 'New York', 'Pennsylvania']
population = [39538223, 29145505, 21538187, 20201249, 13002700]
growth = [0.06, 0.04, 0.03, -0.01, 0.01]

plt.scatter(states, population, label='Population', color='blue')
plt.scatter(states, growth, label='Growth Rate', color='green')

# Place legend outside the plot on the right
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))

plt.title('US States Population and Growth Rate')
plt.xlabel('States')
plt.ylabel('Values')
plt.tight_layout()  # Adjust layout to make room for legend
plt.show()

You can see the output in the screenshot below.

matplotlib legend outside
  • loc='center left' positions the legend’s anchor point at the center left of the legend box.
  • bbox_to_anchor=(1, 0.5) places the anchor point just outside the right edge of the plot (1 means 100% to the right of the axes), vertically centered (0.5).
  • plt.tight_layout() ensures the plot area adjusts to make room for the legend.

Method 2: Use fig.legend() for a Global Legend

Sometimes, you have multiple subplots and want a single legend outside all of them. In such cases, I prefer using the figure-level legend (fig.legend()), which can be positioned outside the entire figure.

Here’s how I do it with a two-panel comparison of tech adoption rates:

import matplotlib.pyplot as plt

years = [2015, 2016, 2017, 2018, 2019]
tech_adoption_A = [50, 55, 60, 65, 70]
tech_adoption_B = [45, 50, 55, 60, 65]

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))

ax1.plot(years, tech_adoption_A, label='Company A', color='red')
ax1.set_title('Company A Tech Adoption')

ax2.plot(years, tech_adoption_B, label='Company B', color='blue')
ax2.set_title('Company B Tech Adoption')

# Place legend outside the figure on the right
fig.legend(loc='center left', bbox_to_anchor=(1, 0.5))

plt.tight_layout(rect=[0, 0, 0.85, 1])  # Leave space on the right for legend
plt.show()

You can see the output in the screenshot below.

legend outside plot matplotlib
  • It creates a single legend for all subplots.
  • Positioning the legend outside the figure keeps the plots uncluttered.
  • rect in tight_layout reserves space for the legend.

Check out Matplotlib Two y axes

Method 3: Use plt.subplots_adjust() to Make Space

Another way I’ve found useful is manually adjusting the subplot parameters to create room for the legend outside the plot.

Example:

import matplotlib.pyplot as plt

categories = ['Tech', 'Healthcare', 'Finance', 'Retail']
values = [120, 90, 100, 80]

plt.bar(categories, values, label='Sector Revenue')

# Adjust subplot to leave space on the right
plt.subplots_adjust(right=0.75)

# Place legend outside the plot on the right
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))

plt.title('Sector Revenue in 2024')
plt.show()

You can see the output in the screenshot below.

matplotlib legend outside plot

This approach gives you control over how much space you want to leave for the legend.

Tips for Working with Legends Outside the Plot

  • Use plt.tight_layout() or fig.tight_layout() to automatically adjust plot elements and avoid overlap.
  • Adjust figure size (figsize) if your legend is large or contains many items.
  • Consider legend font size with fontsize argument to keep it readable but compact.
  • For complex plots, use ncol in legend() to arrange legend entries in multiple columns.

Putting the legend outside the plot is a small change that significantly improves the clarity and professionalism of your visualizations. Whether you’re working on business reports, data science projects, or presentations, these methods will help you communicate your data more effectively.

I hope you found these tips useful. Feel free to experiment with the parameters to fit your specific visualization needs best.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.