简体   繁体   English

matplotlib传奇背景颜色

[英]matplotlib legend background color

Is there while rcParams['legend.frameon'] = 'False' a simple way to fill the legend area background with a given colour. rcParams['legend.frameon'] = 'False'是一种用给定颜色填充图例区域背景的简单方法。 More specifically I would like the grid not to be seen on the legend area because it disturbs the text reading. 更具体地说,我希望在图例区域看不到网格,因为它会扰乱文本阅读。

The keyword framealpha sounds like what I need but it doesn't change anything. 关键字framealpha听起来像我需要的但它不会改变任何东西。

import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['legend.frameon'] = 'False'
plt.plot(range(5), label = u"line")
plt.grid(True)
plt.legend(loc = best)
plt.show()

I've also tried: 我也尝试过:

legend = plt.legend(frameon = 1)
frame = legend.get_frame()
frame.set_color('white')

but then I need to ask how can I change the background colour while keeping the frame on? 但是我需要问一下如何在保持框架的同时改变背景颜色? Sometimes I want it ON with a background colour other than white. 有时我想用白色以外的背景颜色打开它。 And also, is there a way of changing the colour of the frame? 还有,有没有办法改变框架的颜色? With the above code I was expecting to change the colour of the frame only, not the background. 使用上面的代码我期望只改变帧的颜色,而不是背景。

You can set the edge color and the face color separately like this: 您可以分别设置边缘颜色和面部颜色,如下所示:

frame.set_facecolor('green')
frame.set_edgecolor('red')

There's more information under FancyBboxPatch here . 还有FancyBboxPatch下更多的信息在这里

In addition to Molly 's method you can turn the frame off using the linewidth: 除了Molly的方法,您还可以使用线宽关闭帧:

frame.set_linewidth(0)

I used that method in a small convenience function I wrote to hide the legend frames for the same reason you cite. 我在一个小的便利函数中使用了这个方法,我写的是为了隐藏图例框架,原因与你引用的相同。 The function is called adjust_legends in the print_targeted_plots module available from github . 该函数在github提供的print_targeted_plots模块中称为adjust_legends

使用matplotlib.pyplot, plt.legend(facecolor='white', framealpha=1)将为您的图例提供没有透明度的白色背景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM