Skip to main content
Data Visualization
CHAPTER 07 Beginner

Pie Charts and Distribution Visualization

Updated: May 18, 2026
5 min read

# CHAPTER 7

Pie Charts and Distribution Visualization

1. Chapter Introduction

Pie charts are the most debated chart type — powerful when used correctly, misleading when misused. This chapter teaches proper use of pie/donut charts, their limitations, and better alternatives for when categories exceed 5.

2. Pie Chart Fundamentals

python
123456789101112131415161718192021222324252627282930313233343536373839
import matplotlib.pyplot as plt
import numpy as np

labels = ['Google', 'Facebook', 'Amazon', 'Other']
sizes  = [38.5, 19.2, 10.1, 32.2]
explode = (0.05, 0, 0, 0)   # Slightly pop out Google
colors = ['#4285F4', '#1877F2', '#FF9900', '#9E9E9E']

fig, axes = plt.subplots(1, 2, figsize=(14, 6))

# Basic pie
wedges, texts, autotexts = axes[0].pie(
    sizes, labels=labels, autopct='%1.1f%%',
    colors=colors, explode=explode,
    startangle=90,          # Start from top
    pctdistance=0.8,        # Label position (0=center, 1=edge)
    textprops={'fontsize': 11}
)
for at in autotexts:
    at.set_fontweight('bold')
    at.set_color('white')
axes[0].set_title('US Digital Ad Market Share 2024', fontsize=13, fontweight='bold')

# Donut chart (more modern, shows KPI in center)
wedges, texts = axes[1].pie(
    sizes, labels=None, colors=colors,
    startangle=90, pctdistance=0.85,
    wedgeprops={'width': 0.5, 'edgecolor': 'white', 'linewidth': 2}
)
axes[1].text(0, 0, f'Total\n$285B', ha='center', va='center',
             fontsize=14, fontweight='bold', color='#333')
axes[1].legend(wedges, [f'{l}: {s}%' for l, s in zip(labels, sizes)],
               loc='lower center', bbox_to_anchor=(0.5, -0.1), ncol=2)
axes[1].set_title('Donut Chart Style', fontsize=13, fontweight='bold')

plt.suptitle('Part-of-Whole Visualization Styles', fontsize=14, fontweight='bold')
plt.tight_layout()
plt.savefig('pie_donut.png', dpi=150)
plt.show()

3. When to Use Pie Charts (and When NOT To)

text
123456789101112131415161718
✅ USE PIE CHARTS WHEN:
  - You have ≤5 categories
  - Proportions clearly differ (not 22% vs 24%)
  - The whole = meaningful total (100% market share, 100% budget)
  - You want to show one dominant segment

❌ DON'T USE PIE CHARTS WHEN:
  - More than 5-6 categories (use bar chart or treemap)
  - Values are similar (hard to judge equal-ish angles)
  - You need to compare two pies side-by-side
  - Exact values matter (bar chart is more accurate)
  - You have negative values

Better Alternatives:
  - Bar chart → More accurate comparison
  - Treemap   → Better for many categories
  - Waffle    → Shows "out of 100" clearly
  - Donut     → Modern, allows center KPI text

4. Mini Project: Market Share Visualization

python
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np

# Smartphone market data
brands = ['Samsung', 'Apple', 'Xiaomi', 'OPPO', 'Vivo', 'Others']
share  = [20.1, 17.3, 12.5, 10.2, 8.6, 31.3]
colors = ['#1428A0', '#555555', '#FF6900', '#1F7A1F', '#415FFF', '#90A4AE']

fig = plt.figure(figsize=(15, 6))
gs = gridspec.GridSpec(1, 3, figure=fig)

# Left: Donut chart
ax1 = fig.add_subplot(gs[0])
wedges, _ = ax1.pie(share, colors=colors, startangle=90,
                     wedgeprops={'width': 0.5, 'edgecolor': 'white', 'linewidth': 2})
ax1.text(0, 0.05, 'Global', ha='center', fontsize=10, color='#666')
ax1.text(0, -0.15, 'Smartphone', ha='center', fontsize=10, color='#666')
ax1.text(0, -0.35, 'Market', ha='center', fontsize=10, color='#666')
ax1.set_title('Market Share Q4 2024', fontsize=12, fontweight='bold')

# Middle: Sorted bar chart
ax2 = fig.add_subplot(gs[1])
sorted_pairs = sorted(zip(share, brands, colors), reverse=True)
s_sorted, b_sorted, c_sorted = zip(*sorted_pairs)
bars = ax2.barh(b_sorted, s_sorted, color=c_sorted, edgecolor='white', height=0.6)
for bar, val in zip(bars, s_sorted):
    ax2.text(val + 0.3, bar.get_y() + bar.get_height()/2,
             f'{val}%', va='center', fontsize=10, fontweight='bold')
ax2.set_xlabel('Market Share (%)')
ax2.set_title('Ranked Comparison', fontsize=12, fontweight='bold')
ax2.spines['top'].set_visible(False)
ax2.spines['right'].set_visible(False)

# Right: Trend (simulated)
ax3 = fig.add_subplot(gs[2])
quarters = ['Q1', 'Q2', 'Q3', 'Q4']
top3 = {'Samsung': [21.5, 20.8, 20.3, 20.1],
         'Apple': [15.2, 16.1, 16.8, 17.3],
         'Xiaomi': [13.1, 12.8, 12.6, 12.5]}
for brand, data in top3.items():
    idx = brands.index(brand)
    ax3.plot(quarters, data, 'o-', label=brand, color=colors[idx], linewidth=2, markersize=7)
ax3.set_title('Top 3 Trend', fontsize=12, fontweight='bold')
ax3.set_ylabel('Share (%)')
ax3.legend(fontsize=9)
ax3.grid(True, alpha=0.3)
ax3.spines['top'].set_visible(False)
ax3.spines['right'].set_visible(False)

plt.suptitle('Smartphone Market Analysis Dashboard', fontsize=14, fontweight='bold')
plt.tight_layout()
plt.savefig('market_share.png', dpi=150, bbox_inches='tight')
plt.show()

5. Common Mistakes

  • Too many slices: A pie with 10 categories has slices too thin to read. Consolidate into "Other" or switch to a bar chart.
  • 3D pie charts: Perspective distortion makes near slices look larger than they are. Never use 3D pie charts.

6. MCQs

Question 1

Pie charts work best with?

Question 2

wedgeprops={'width': 0.5} creates?

Question 3

startangle=90 in pie chart?

Question 4

autopct='%1.1f%%' shows?

Question 5

When pie slices are similar size (e.g., 23% vs 24%)?

Question 6

Donut chart advantage over pie?

Question 7

explode=(0.05, 0, 0) does?

Question 8

Best alternative to pie for many categories?

Question 9

3D pie charts are problematic because?

Question 10

pctdistance=0.8 controls?

7. Interview Questions

  • Q: When would you choose a bar chart over a pie chart?
  • Q: What is a donut chart and when is it preferred over a pie chart?

8. Summary

Pie charts: use for ≤5 clearly differentiated categories showing part-of-whole. Donut charts add modern style and center text space. For >5 categories, use sorted bar charts or treemaps. Avoid 3D, avoid too-similar slices, avoid side-by-side pies. The bar chart is more accurate for most comparison tasks.

9. Next Chapter Recommendation

In Chapter 8: Scatter Plots and Correlation Analysis, we visualize relationships between two numeric variables and detect correlation patterns.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·