Python Central HubSign in

Data Analytics with Python / Phase 5: Data Visualization with Matplotlib

Pie Chart

Use pie charts carefully, and learn better alternatives for comparisons.

A warning about pie charts

Pie charts are often hard to compare precisely.

Prefer:

  • Bar charts (best)
  • Donut charts only for simple cases

Still, you may see pie charts in reporting, so here’s how to make one safely.

diagram How a pie chart is built mermaid
Each category's share of the total becomes an angle; all the angles together sweep a full circle.

Basic pie chart

Python
import matplotlib.pyplot as plt
 
labels = ["A", "B", "C"]
values = [50, 30, 20]
 
plt.figure(figsize=(6, 6))
plt.pie(values, labels=labels, autopct="%1.0f%%", startangle=90)
plt.title("Category share")
plt.tight_layout()
plt.show()

Improve readability

Python
import matplotlib.pyplot as plt
 
explode = [0.1, 0.0, 0.0]
 
plt.figure(figsize=(6, 6))
plt.pie(values, labels=labels, autopct="%1.0f%%", startangle=90, explode=explode)
plt.title("Category share")
plt.tight_layout()
plt.show()

Visualize it

A pie chart divides a whole into slices, where each slice's angle is proportional to its share of the total — so all the slices together make up 100%. Watch it build slice by slice:

sketch A pie chart splits a whole into slices p5.js
Each slice's angle is proportional to its share of the total; together they add up to 100%.

Better alternative

If your goal is comparison, use a bar chart.

Check your understanding

quizCheck yourself
  1. What does autopct="%1.0f%%" do in plt.pie?

    Show answer

    A — Prints each slice's percentage with no decimals — autopct formats the percentage text on each slice. For values 50, 30 and 20 it shows 50%, 30% and 20%.

  2. What does explode = [0.1, 0.0, 0.0] do?

    Show answer

    A — Pulls the first slice out from the center — Each number is how far a slice is offset from the center, so only slice A is pulled out.

  3. What does startangle=90 change?

    Show answer

    A — Where the first slice begins, so it starts at the top — startangle rotates the pie so the first slice begins at 90 degrees, the top.

  4. If your goal is to compare categories precisely, what does the lesson recommend?

    Show answer

    A — A bar chart — Pie charts are hard to compare precisely. Bar charts are the better alternative.

Next

Continue to Subplots and Figure Size to combine several charts into one dashboard.

🧪 Try It Yourself

Exercise 1 – Slices sum to 100%

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Explode the biggest slice

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Convert to a bar chart instead

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did