# 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)

plt.xlabel('Year')
plt.ylabel('Goals in Club Football')

plt.title("Messi vs Ronaldo")

plt.legend(['Messi', 'Ronaldo'])

Output:

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)

plt.xlabel('Year')
plt.ylabel('Goals in Club Football')

plt.title("Messi vs Ronaldo")

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

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

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)
ax1.set_xlabel('Year')
ax1.set_ylabel('Goals in Club Football')
ax1.legend(['Messi', 'Ronaldo'])

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

Output:

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)
ax1.set_xlabel('Year')
ax1.set_ylabel('Goals in Club Football')
ax1.legend(['Messi', 'Ronaldo'], frameon=False)

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

Output:

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

You might also be interested in –