如何在matplotlib中设置轮廓标签的背景颜色?

MCF*_*MCF 4 matplotlib

我正在使用命令:

axins.clabel(c, levls, fontsize=4, fmt='%4.2f', colors= 'white')
Run Code Online (Sandbox Code Playgroud)

为我的轮廓生成标签,我希望它们是白色的(颜色='白色'有效),红色背景,我无法找到是否可以为它们指定背景颜色?

Gre*_*reg 5

我参加派对的时间已经晚了几年,但这个答案仍然会出现在Google上,所以这里的解决方案是我在@pelson的回答中受到启发的.

如果将等高线图设置为:

CS = ax.contour(X, Y, Z)
clabels = ax.clabel(CS)
Run Code Online (Sandbox Code Playgroud)

然后您可以使用简单地更新背景颜色

[txt.set_backgroundcolor('white') for txt in clabels]
Run Code Online (Sandbox Code Playgroud)

然而,边界框(bbox)非常大并且经常不必要地掩盖其他特征.所以最好bbox直接更新:

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


pel*_*son 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].
Run Code Online (Sandbox Code Playgroud)

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

哈特哈,