如何在 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()

輸出

更新時間:2021 年 7 月 8 日

1 千個以上瀏覽量

開啟您的 職業生涯

透過完成課程獲取認證

立即開始
廣告
© . All rights reserved.