Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Remove the legend border in Matplotlib

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article, we will learn how to Remove the legend border in Matplotlib. Let’s discuss some concepts :

  • A legend is an area describing the elements of the graph. In the matplotlib library, there’s a function called legend() which is used to Place a legend on the axes.

Approach:

  1. Import Library (Matplotlib)
  2. Import / create data.
  3. Plot a chart.
  4. Add legend.
  5. Remove legend border.

Example: Here is an example with legends.

Python3




# importing packages
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:

Method 1: (Using frameon = False)

Python3




# importing packages
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: (Using legend.get_frame().set_alpha(0))

Python3




# importing packages
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 :


My Personal Notes arrow_drop_up
Last Updated : 29 May, 2021
Like Article
Save Article
Similar Reads
Related Tutorials