如何在 Matplotlib 中将饼图中的 autopct 文本颜色更改为白色?
要在 Matplotlib 中更改饼图中 autopct 文本颜色为白色,我们可以采取以下步骤 −
- 设置图形大小并调整子图之间和周围的填充。
- 制作 小时,活动,颜色 列表来绘制饼图。
- 绘制饼图时,制作'。文本'实例的列表以显示数字标签。
- 迭代 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 = ['sleeping', 'exercise', 'studying', 'working']
colors = ["grey", "green", "orange", "blue"]
_, _, autotexts = plt.pie(hours, labels=activities, colors=colors, autopct="%.2f")
for ins in autotexts:
ins.set_color('white')
plt.show()输出

广告
数据结构
网络
RDBMS
操作系统
Java
IOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP