Fix Python – Remove or adapt border of frame of legend using matplotlib

Question

Asked By – Mattijn

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?

Now we will see solution for issue: Remove or adapt border of frame of legend using matplotlib


Answer

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)

This question is answered By – Mattijn

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0