Therefore, we can use bty="n" with the legend function and it will remove the border of the legend.
To change the position of a legend in Matplotlib, you can use the plt. legend() function. The default location is “best” – which is where Matplotlib automatically finds a location for the legend based on where it avoids covering any data points.
When plotting a plot using matplotlib:
How to remove the box of the legend?
plt.legend(frameon=False)
How to change the color of the border of the legend box?
leg = plt.legend()
leg.get_frame().set_edgecolor('b')
How to remove only the border of the box of the legend?
leg = plt.legend()
leg.get_frame().set_linewidth(0.0)
For the matplotlib
object oriented approach:
axes.legend(frameon=False)
leg = axes.legend()
leg.get_frame().set_edgecolor('b')
leg.get_frame().set_linewidth(0.0)
One more related question, since it took me forever to find the answer:
How to make the legend background blank (i.e. transparent, not white):
legend = plt.legend()
legend.get_frame().set_facecolor('none')
Warning, you want 'none'
(the string). None
means the default color instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With