Remove or adapt border of frame of legend using matplotlib

PythonMatplotlib

Python Problem Overview


When plotting a plot using matplotlib:

  1. How to remove the box of the legend?

  2. How to change the color of the border of the legend box?

  3. How to remove only the border of the box of the legend?

Python Solutions


Solution 1 - Python

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)

Solution 2 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMattijnView Question on Stackoverflow
Solution 1 - PythonMattijnView Answer on Stackoverflow
Solution 2 - PythonKevin J. BlackView Answer on Stackoverflow