如何设置轮廓标签的背景颜色

问题描述 投票:0回答:2

我正在使用命令:

axins.clabel(c, levls, fontsize=4, fmt='%4.2f', colors= 'white')

要为我的轮廓生成标签,我希望它们是白色(颜色=“白色”有效)和红色背景,我不知道是否可以为它们指定背景颜色?

python matplotlib colors label contour
2个回答
9
投票

我参加派对晚了几年,但这个答案仍然在谷歌上出现,所以这是我受@pelson的答案启发而破解的解决方案。

如果将等高线图设置为:

CS = ax.contour(X, Y, Z)
clabels = ax.clabel(CS)

然后您可以简单地使用

更新背景颜色
[txt.set_backgroundcolor('white') for txt in clabels]

然而,边界框(

bbox
)相当大,并且经常不必要地遮挡其他特征。所以还是直接更新
bbox
比较好:

[txt.set_bbox(dict(facecolor='white', edgecolor='none', pad=0)) for txt in clabels]

3
投票

文本艺术家的

backgroundcolor
可能正是您所需要的(http://matplotlib.org/users/text_props.html)。
clabel
通过
labelTexts
属性公开文本艺术家(似乎未记录)。

类似(未经测试):

clabels = ax.clabel(c, levls, color='white', ...)
[txt.set_backgroundcolor('red') for txt in clabels.labelTexts].

如果这不起作用,请使用 SSCCE 更新您的问题,我将发布一些工作代码。

HTH,

© www.soinside.com 2019 - 2024. All rights reserved.