如何在Matplotlib的饼图中更改自动百分比文本颜色为白色?
要在Matplotlib的饼图中将自动百分比文本颜色更改为白色,可以执行以下步骤 –
- 设置图形大小并调整子图之间和周围的填充。
- 制作要绘制饼图的“小时”,“活动”和“颜色”的列表。
- 在制作饼图时,制作数字标签的“.Text”实例的列表。
- 迭代autotexts并将autotext的颜色设置为白色。
- 使用“show()”方法显示图形。
示例
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
plt.figure()
hours = [8, 1, 11, 4]
activities = ['睡觉', '锻炼', '学习', '工作']
colors = ["灰色", "绿色", "橙色", "蓝色"]
_, _, autotexts = plt.pie(hours, labels=activities, colors=colors, autopct="%.2f")
for ins in autotexts:
ins.set_color('white')
plt.show()