Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีเปลี่ยนสีข้อความ autopct เป็นสีขาวใน Pie Chart Matplotlib

ในการเปลี่ยนสีข้อความ autopct เป็นสีขาวใน pie chart ใน Matplotlib เราสามารถทำตามขั้นตอนต่อไปนี้:

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots
  • สร้างลิสต์ของ hours, activities, และ colors สำหรับพลอต pie chart
  • สร้างลิสต์ของอินสแตนซ์ 'Text' สำหรับ label ตัวเลข ขณะสร้าง pie chart
  • วนลูป autotexts และตั้งค่าสีของ auttext เป็นสีขาว
  • เพื่อแสดงรูป ใช้เมธอด 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()

ผลลัพธ์

วิธีเปลี่ยนสีข้อความ autopct เป็นสีขาวใน Pie Chart Matplotlib