Open In App

Remove the legend border in Matplotlib

Last Updated : 11 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A legend helps explain the elements in a plot, and for adding a legend to a plot in Matplotlib, we use the legend() function. A border is added by default to the legend, but there may be some cases when you will prefer not having a border for a cleaner appearance. We will demonstrate how to do it quickly.

Example: Here is an example with legends.

Python
import numpy as np
import matplotlib.pyplot as plt

# create data
x = np.linspace(1, 10, 1000)
y1 = np.sin(x)
y2 = np.cos(x)

# plot graph
plt.plot(x, y1)
plt.plot(x, y2)

# add legend
plt.legend(['Sine wave', 'Cos wave'])
plt.show()

Output

A plot with legends that have a border by default.

Method 1: Remove the Legend Border Using frameon=False

To remove the border around the legend, you can use the frameon=False parameter in the legend() function. Here’s how we can do it:

Python
import numpy as np
import matplotlib.pyplot as plt

# create data
x = np.linspace(1, 10, 1000)
y1 = np.sin(x)
y2 = np.cos(x)

# plot graph
plt.plot(x, y1)
plt.plot(x, y2)

# add legend and remove frame
plt.legend(['Sine wave', 'Cos wave'], frameon=False)
plt.show()

Output

Method 2: Remove the Legend Border Using legend.get_frame().set_alpha(0)

Another way to remove the border from the legend is by setting the alpha transparency of the legend’s frame to zero. This effectively makes the frame invisible while keeping the legend content intact.

Here’s how we can do it:

Python
import numpy as np
import matplotlib.pyplot as plt

# create data
x = np.linspace(1, 10, 1000)
y1 = np.sin(x)
y2 = np.cos(x)

# plot graph
plt.plot(x, y1)
plt.plot(x, y2)

# add legend
leg = plt.legend(['Sine wave', 'Cos wave'])

# set opacity equal to zero i.e; transparent
leg.get_frame().set_alpha(0)
plt.show()

Output


Next Article
Article Tags :
Practice Tags :

Similar Reads