Skip to Content

How to remove the legend border (frame) in Matplotlib?

The matplotlib library is a charting library in Python that allows you to plot highly customizable plots. In this tutorial, we will look at how to remove the border (or, the frame) of the legend in a matplotlib plot.

By default, when you create a plot in matplotlib with a legend, the contents of the legend are present inside a rectangular area with a solid border. To remove the legend border from a matplotlib plot, pass frameon=False as an argument to the matplotlib.pyplot.legend() function used to add the legend to the plot.

Let’s look at some examples of using the above method.

First, let’s create a plot and add a legend to it to show how the legend looks by default.

import matplotlib.pyplot as plt

# x values - years
x = [2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]
# y1 values - goals by Lionel Messi in club football
y1 = [73, 60, 41, 58, 41, 54, 45, 51, 31, 38]
# y2 values - goals by Cristiano Ronaldo in club football 
y2 = [60, 55, 51, 61, 51, 42, 44, 28, 37, 36]

# plot y1 on a line plot
plt.plot(x, y1)
# plot y2 on a line plot
plt.plot(x, y2)

# add axes labels
plt.xlabel('Year')
plt.ylabel('Goals in Club Football')

# add plot title
plt.title("Messi vs Ronaldo")

# add plot legend
plt.legend(['Messi', 'Ronaldo'])

Output:

line chart with legend having borders

Here, we created a line plot with two lines. You can see that the legend is also present in this plot and the legend has a border surrounding it.

Example 1 – Remove the legend border

Let’s plot the above chart again but this time with the legend border removed. We can do so using the frameon parameter to the matplotlib.pyplot.legend() function.

# plot y1 on a line plot
plt.plot(x, y1)
# plot y2 on a line plot
plt.plot(x, y2)

# add axes labels
plt.xlabel('Year')
plt.ylabel('Goals in Club Football')

# add plot title
plt.title("Messi vs Ronaldo")

# add plot legend
plt.legend(['Messi', 'Ronaldo'], frameon=False)

Output:

legend border removed in the line plot

You can see that the legend in the plot now doesn’t have a frame (or border).

Example 2 – Remove legend border from subplots

You can similarly remove the legend border from subplots. The only difference is, instead of using the matplotlib.pyplot.legend() function, you’ll have to use the respective subplot’s axes object’s legend() function and pass frameon=False.

Let’s look at an example.

First, we will create two subplots, both having the default legend.

# create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))

# plot the first subplot
ax1.plot(x, y1)
ax1.plot(x, y2)
# add axis labels
ax1.set_xlabel('Year')
ax1.set_ylabel('Goals in Club Football')
# add legend
ax1.legend(['Messi', 'Ronaldo'])

# plot the second subplot
ax2.plot(x, y1)
ax2.plot(x, y2)
# add axis labels
ax2.set_xlabel('Year')
ax2.set_ylabel('Goals in Club Football')
# add legend
ax2.legend(['Messi', 'Ronaldo'])

Output:

subplots with each subplot having a legend with a border

Here, we created two subplots. Both the subplots have the same data but that is not important. You can see that legend is present inside both the plots and has a border to it.

Let’s now remove the border from the first subplot and keep the second subplot as it is.

# create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))

# plot the first subplot
ax1.plot(x, y1)
ax1.plot(x, y2)
# add axis labels
ax1.set_xlabel('Year')
ax1.set_ylabel('Goals in Club Football')
# add legend
ax1.legend(['Messi', 'Ronaldo'], frameon=False)

# plot the second subplot
ax2.plot(x, y1)
ax2.plot(x, y2)
# add axis labels
ax2.set_xlabel('Year')
ax2.set_ylabel('Goals in Club Football')
# add legend
ax2.legend(['Messi', 'Ronaldo'])

Output:

legend removed in the first subplot

The legend in the first subplot does not have a border.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.