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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25540259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:21:56  来源:igfitidea点击:

Remove or adapt border of frame of legend using matplotlib

pythonmatplotlib

提问by Mattijn

When plotting a plot using matplotlib:

使用 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?
  1. 如何去除图例的框?
  2. 如何更改图例框边框的颜色?
  3. 如何仅删除图例框的边框?

采纳答案by Mattijn

When plotting a plot using matplotlib:

使用 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)

回答by Kevin J. Black

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). Nonemeans the default color instead.

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