Pie Charts with pie(x)ΒΆ
A pie chart displays proportional data as wedges of a circle, where each wedgeβs angular size is proportional to its value relative to the total. The Axes.pie() method takes an array of values and automatically computes the proportions, drawing colored wedges that sum to a full circle.
Why this matters for data science: Pie charts are best suited for showing parts of a whole when there are few categories (ideally 2-5) and the proportions are sufficiently different to distinguish visually. They are commonly used in business dashboards for market share, budget allocation, or category breakdowns. However, be aware that humans are poor at comparing angles accurately β for more than a few categories or when precise comparison matters, a bar chart is usually more effective. The example uses plt.get_cmap('Blues') with np.linspace(0.2, 0.7, len(x)) to sample a sequential colormap, producing a visually cohesive set of blue shades. The wedgeprops dictionary adds white edges between wedges for clarity, while radius=3 and center=(4, 4) control the pieβs size and position within the axes. Setting frame=True keeps the axis frame visible, which is unusual for pie charts but useful when combining with other plot types.
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery-nogrid')
# make data
x = [1, 2, 3, 4]
colors = plt.get_cmap('Blues')(np.linspace(0.2, 0.7, len(x)))
# plot
fig, ax = plt.subplots()
ax.pie(x, colors=colors, radius=3, center=(4, 4),
wedgeprops={"linewidth": 1, "edgecolor": "white"}, frame=True)
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()