Skillversitas

Pie Charts

A pie chart shows parts of a whole: each slice's angle is proportional to its share. It is one of the most misused charts - angles are hard to compare, and more than a handful of slices turns the figure into a wheel of color with no readable information. Use it when there are a small number of categories (ideally three to six) and the story is literally "what fraction of the total." For everything else, a bar chart is more honest and easier to read.

Basic Pie

import matplotlib.pyplot as plt

labels = ["Search", "Direct", "Referral", "Other"]
sizes = [45, 25, 15, 15]

fig, ax = plt.subplots()
ax.pie(sizes, labels=labels, autopct="%1.1f%%", startangle=90)
plt.show()

autopct prints the percentage inside each wedge (%1.1f%% = one decimal, literal percent sign). startangle=90 begins at 12 o'clock, the conventional orientation. Slices are drawn counterclockwise from the start angle by default; counterclock=False reverses the order.

Styling and Emphasis

import matplotlib.pyplot as plt

labels = ["Search", "Direct", "Referral", "Other"]
sizes = [45, 25, 15, 15]
colors = ["#2563eb", "#93c5fd", "#bfdbfe", "#e5e7eb"]

fig, ax = plt.subplots()
ax.pie(sizes, labels=labels, colors=colors,
       explode=(0.05, 0, 0, 0),        # pull the hero slice out
       autopct="%1.1f%%",
       startangle=90,
       textprops=dict(fontsize=10))
plt.show()

explode offsets individual slices - one small offset on the category that matters is emphasis; exploding several is noise. Explicit colors keep the palette under control instead of cycling the default.

Donut Charts

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
wedges, _ = ax.pie([45, 25, 15, 15],
                   wedgeprops=dict(width=0.4, edgecolor="white"))
ax.text(0, 0.08, "100k", ha="center", va="center",
        fontsize=18, fontweight="bold")
ax.text(0, -0.18, "total visits", ha="center", va="center", fontsize=9)
ax.set_title("Traffic sources", pad=20)
plt.show()

wedgeprops=dict(width=0.4) hollows the center into a donut, and ax.text at the origin places the total - the standard "big number in the middle" dashboard form. Add a legend with ax.legend(wedges, labels, loc="center left", bbox_to_anchor=(1, 0.5)) when slice labels get crowded.

When Not to Use a Pie

  • More than ~6 categories - merge the tail into "Other" or switch to a bar chart.
  • Comparing values across two time periods - angle differences between pies are nearly unreadable; grouped bars win.
  • Values close to each other (45 vs 42) - the eye cannot resolve that angle gap; a labeled bar chart can.
  • Parts that do not sum to a meaningful whole - a pie of "favorite languages" is a distribution, not a composition.

Summary

Reserve pies for small, exhaustive compositions where the total is the point: pie with autopct, a 90-degree start angle, one exploded slice for emphasis, and a donut with a center total for dashboards. The moment you need to compare values rather than shares, redraw it as bars.