Matplotlib Pie Charts
Show proportions and percentages with circular pie charts
🥧 Pie Chart Visualization
Pie charts are perfect for showing how parts relate to the whole. Each slice represents a proportion of the total dataset.
import matplotlib.pyplot as plt
labels = ['A', 'B', 'C', 'D']
sizes = [25, 30, 20, 25]
plt.pie(sizes, labels=labels)
plt.title('Simple Pie Chart')
plt.show()
Pie Chart Features
Customize your pie charts with various visual elements:
Basic Pie
Simple proportional slices
Labels & Percentages
Add text and percentage values
Exploded Slices
Separate slices for emphasis
Custom Colors
Color schemes and styling
🔹 Basic Pie Charts
Create simple pie charts to show proportions
# Basic pie chart
import matplotlib.pyplot as plt
labels = ['Python', 'Java', 'JavaScript', 'C++']
sizes = [40, 25, 20, 15]
plt.pie(sizes, labels=labels)
plt.title('Programming Languages Usage')
plt.show()
# With percentages
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.title('Languages with Percentages')
plt.show()
🔹 Exploded Pie Charts
Separate slices to emphasize specific data
# Exploded pie chart
labels = ['A', 'B', 'C', 'D']
sizes = [30, 25, 25, 20]
explode = (0.1, 0, 0, 0) # explode 1st slice
plt.pie(sizes, explode=explode, labels=labels,
autopct='%1.1f%%', shadow=True)
plt.title('Exploded Pie Chart')
plt.show()
# Multiple exploded slices
explode = (0.1, 0.1, 0, 0)
plt.pie(sizes, explode=explode, labels=labels,
autopct='%1.1f%%')
plt.title('Multiple Exploded Slices')
plt.show()
🔹 Custom Colors and Styling
Apply colors and visual enhancements
# Custom colors
colors = ['gold', 'lightcoral', 'lightskyblue', 'lightgreen']
labels = ['Q1', 'Q2', 'Q3', 'Q4']
sizes = [25, 30, 20, 25]
plt.pie(sizes, labels=labels, colors=colors,
autopct='%1.1f%%', startangle=90)
plt.title('Quarterly Sales')
plt.show()
# Using colormap
import numpy as np
colors = plt.cm.Set3(np.linspace(0, 1, len(sizes)))
plt.pie(sizes, labels=labels, colors=colors,
autopct='%1.1f%%')
plt.title('Colormap Pie Chart')
plt.show()
🔹 Advanced Pie Chart Features
Add wedge properties and text formatting
# Advanced styling
labels = ['Category A', 'Category B', 'Category C']
sizes = [40, 35, 25]
colors = ['#ff9999', '#66b3ff', '#99ff99']
wedges, texts, autotexts = plt.pie(sizes, labels=labels,
colors=colors, autopct='%1.1f%%',
startangle=90,
wedgeprops={'edgecolor': 'black'})
# Customize text
for autotext in autotexts:
autotext.set_color('white')
autotext.set_fontweight('bold')
plt.title('Styled Pie Chart', fontsize=16, fontweight='bold')
plt.axis('equal') # Equal aspect ratio
plt.show()