0
0
Matplotlibdata~5 mins

Pie chart color customization in Matplotlib

Choose your learning style9 modes available
Introduction

Pie charts show parts of a whole. Changing colors helps make each part clear and pretty.

You want to show sales share of different products with distinct colors.
You need to highlight a specific slice in a pie chart for a report.
You want to match chart colors to your company branding.
You want to make a pie chart easier to read for colorblind viewers.
Syntax
Matplotlib
plt.pie(data, labels=labels, colors=colors)

colors is a list of color names or codes matching each slice.

Colors can be named colors like 'red', hex codes like '#FF0000', or RGB tuples.

Examples
Simple pie chart with red, green, and blue slices.
Matplotlib
plt.pie([10, 20, 30], labels=['A', 'B', 'C'], colors=['red', 'green', 'blue'])
Pie chart using hex color codes for tomato, steel blue, and lime green.
Matplotlib
plt.pie([5, 15, 25], labels=['X', 'Y', 'Z'], colors=['#FF6347', '#4682B4', '#32CD32'])
Pie chart with RGB tuples for red, green, and blue.
Matplotlib
plt.pie([7, 14, 21], labels=['One', 'Two', 'Three'], colors=[(1,0,0), (0,1,0), (0,0,1)])
Sample Program

This program creates a pie chart showing fruit distribution. Each slice has a soft color for easy reading. Percentages are shown on slices.

Matplotlib
import matplotlib.pyplot as plt

# Data for pie chart
sizes = [40, 35, 25]
labels = ['Apples', 'Bananas', 'Cherries']
colors = ['#FF9999', '#FFE699', '#99FF99']  # soft red, yellow, green

plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%')
plt.title('Fruit Distribution')
plt.show()
OutputSuccess
Important Notes

If you provide fewer colors than slices, matplotlib repeats colors.

Use autopct to add percentage labels on slices.

Colors can be customized to improve accessibility and style.

Summary

Pie chart colors make parts easy to see and understand.

Use the colors parameter with a list of colors.

Colors can be names, hex codes, or RGB tuples.