使用matplotlib删除或调整图例框架的边框

问题:使用matplotlib删除或调整图例框架的边框

使用matplotlib绘制图时:

  1. 如何删除图例框?
  2. 如何更改图例框边框的颜色?
  3. 如何仅删除图例框的边框?

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?

回答 0

使用matplotlib绘制图时:

如何删除图例框?

plt.legend(frameon=False)

如何更改图例框边框的颜色?

leg = plt.legend()
leg.get_frame().set_edgecolor('b')

如何仅删除图例框的边框?

leg = plt.legend()
leg.get_frame().set_linewidth(0.0)

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)

回答 1

还有一个相关的问题,因为我花了很长时间才找到答案:

如何使图例背景为空白(透明而不是白色):

legend = plt.legend()
legend.get_frame().set_facecolor('none')

警告,您想要'none'(字符串)。None表示默认颜色。

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.