Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to remove legend box [python]

So I need to make this plot in python. I wish to remove my legend's border. However, when I tried the different solutions other posters made, they were unable to work with mine. Please help.

This doesn't work:

plt.legend({'z$\sim$0.35', 'z$\sim$0.1','z$\sim$1.55'})
plt.legend(frameon=False)

plt.legend({'z$\sim$0.35', 'z$\sim$0.1','z$\sim$1.55'})
plt.legend.get_frame().set_linewidth(0.0)

plt.legend({'z$\sim$0.35', 'z$\sim$0.1','z$\sim$1.55'}, 'Box', 'off')

Additionally, when I plotted, I imported two different files and graphed them with a line and with circles respectively. How could I put a line or a circle within the legend key?

The plot:

enter image description here

like image 999
Shawn Zhang Avatar asked Jun 20 '16 21:06

Shawn Zhang


People also ask

How do I get rid of legend in Matplotlib?

We could use remove() and set_visible() methods of the legend object to remove legend from a figure in Matplotlib. We can also remove legend from a figure in Matplotlib by setting the label to _nolegend_ in plot() method, axes. legend to None and figure.

How do you move the legend box in Python?

legend() to move the legend outside the plot. Call matplotlib. pyplot. legend(loc=str, bbox_to_anchor=tuple, ncol=n) with str as a string representing the general location to place the legend, tuple as an x, y coordinate further specifying the location of the legend, and n as the desired number of columns in the legend ...


1 Answers

It's very strange because the command :

plt.legend(frameon=False)

Should work very well. You can also try this command, to compare :

plt.legend(frameon=None)

You can also read the documentation on this page about plt.legend

I scripted something as example to you :

import numpy as np
import matplotlib.pyplot as plt


x = np.array([0,4,8,13])
y = np.array([0,1,2,3])

fig1, ((ax1, ax2)) = plt.subplots(1, 2)

ax1.plot(x,y, label=u'test')
ax1.legend(loc='upper left', frameon=False)

ax2.plot(x,y, label=u'test2')
ax2.legend(loc='upper left', frameon=None)

plt.show()

enter image description here

like image 187
Essex Avatar answered Oct 04 '22 11:10

Essex