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:
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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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) # 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:
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:
The legend in the first subplot does not have a border.
You might also be interested in –
- How to change the legend position in Matplotlib?
- Matplotlib – Change Line to Dots
- Matplotlib – Add an Average Line to the Plot
- How to make text italic in a Matplotlib plot?
- How to bold text in a Matplotlib plot?
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.