如何在matplotlib中更改图例edgecolor和facecolor [英] How to change the legend edgecolor and facecolor in matplotlib

查看:246
本文介绍了如何在matplotlib中更改图例edgecolor和facecolor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在那里,而 rcParams ['legend.frameon'] ='False'是一种用给定颜色填充图例区域背景的简单方法.更具体地说,我希望在图例区域不显示网格,因为它会干扰文本阅读.

Is there while rcParams['legend.frameon'] = 'False' a simple way to fill the legend area background with a given colour. More specifically I would like the grid not to be seen on the legend area because it disturbs the text reading.

关键字 framealpha 听起来像我所需要的,但没有任何改变.

The keyword framealpha sounds like what I need but it doesn't change anything.

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()

我也试过:

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')

FancyBboxPatch 下有更多信息这里.

There's more information under FancyBboxPatch here.

这篇关于如何在matplotlib中更改图例edgecolor和facecolor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆