Python-如何在饼图中将autoct文本颜色更改为白色?

2024-09-28 17:21:48 发布

您现在位置:Python中文网/ 问答频道 /正文

pie(fbfrac,labels = fblabel,autopct='%1.1f%%',pctdistance=0.8,startangle=90,colors=fbcolor)

我让图表按我所希望的那样显示,但如果文本是白色而不是黑色,那么它在绘图中会更突出。


Tags: 文本绘图labels图表colorspie黑色白色
2条回答

您可以使用pyplot.pietextprops参数在一行中完成此操作。很简单:

plt.pie(data, autopct='%1.1f%%', textprops={'color':"w"})

就你而言:

pie(fbfrac, labels=fblabel, autopct='%1.1f%%', pctdistance=0.8, startangle=90, colors=fbcolor, textprops={'color':"w"})

一个启发性的例子可以找到here

来自^{} documentation

Return value:

If autopct is not None, return the tuple (patches, texts, autotexts), where patches and texts are as above, and autotexts is a list of Text instances for the numeric labels.

您需要更改autotexts的颜色;这只需通过set_color()完成:

_, _, autotexts = pie(fbfrac,labels = fblabel,autopct='%1.1f%%',pctdistance=0.8,startangle=90,colors=fbcolor)
for autotext in autotexts:
    autotext.set_color('white')

这将产生(使用Hogs and Dogs example): enter image description here

相关问题 更多 >