Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove frame around legend in Python Object-Oriented plotting

I would like to remove the frame around the legend box. I found few ways. However, none implement them with "axis method".

The code shown below gets the result but I wish to know a cleaner, more elegant way, perhaps like ax.legend.draw_frame(False) or something similar. Any ideas if such a way exists without using pylab?

SOLUTION: Make use of ax.legend(numpoints=1, loc=3, frameon=False) as suggested by Evert

import numpy as np
import matplotlib.pyplot as plt
from pylab import legend

x = np.linspace(1,10, 100)
y = x**3

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(x,y, 'bo', label='Blah!')
lg = legend(numpoints = 1, loc=2)
lg.get_frame().set_alpha(0)
#ax.legend(numpoints = 1, loc=2)

plt.show()
like image 413
Rohit Avatar asked Sep 18 '14 21:09

Rohit


People also ask

How do I get rid of the legend border?

Therefore, we can use bty="n" with the legend function and it will remove the border of the legend.

How do I remove a border from a chart in Python?

You can remove the border of the legend by using the argument frameon=False in the call to plt. legend() .

How do you remove the legend from a graph in Python?

Example 1: By using ax. legend_ = None, legend can be removed from figure in matplotlib.


1 Answers

As @Evert does not wish to answer I will do so in order to mark it as solved. But please give him/her points.

Make use of ax.legend(numpoints=1, loc=3, frameon=False) as suggested by Evert

like image 74
Rohit Avatar answered Nov 14 '22 16:11

Rohit