Để đổi màu chữ autopct thành trắng trong pie chart Matplotlib, chúng ta có thể thực hiện các bước sau:
- Thiết lập kích thước figure và điều chỉnh padding giữa và xung quanh subplots.
- Tạo danh sách hours, activities, và colors để vẽ pie chart.
- Tạo danh sách instance '.Text' cho nhãn số, khi tạo pie chart.
- Lặp qua autotexts và đặt màu autotext thành trắng.
- Để hiển thị figure, dùng phương thức show().
Ví dụ
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()
Kết quả
