Sub topic 6
Building a Pareto Chart in Python
This chapter assumes you know basic percentages, cumulative sums, and bar charts. It also assumes basic Python and pandas. Matplotlib is used for the chart. A Pareto chart ranks categories from largest to smallest and adds a line showing the cumulative percentage.
Start with categorical data
A Pareto analysis begins with events grouped into categories. For example, an inspection process may record several defect types:
| Defect type | Count |
|---|---|
| Scratches | 42 |
| Misalignment | 27 |
| Wrong label | 11 |
| Cracks | 8 |
| Discoloration | 5 |
| Other | 7 |
The counts must represent comparable events from a defined period, product group, or process step. If the categories overlap, one event may be counted twice and the chart will mislead you.
The Pareto principle says that a relatively small number of causes often accounts for a large share of the result. The familiar “80/20 rule” is an approximation. A real chart might show 75/25, 85/30, or another pattern.
Compute the cumulative percentage
First, sort the categories by count in descending order. Then calculate a running total and divide each running total by the grand total.
import pandas as pd
defects = pd.Series({
"Scratches": 42,
"Misalignment": 27,
"Wrong label": 11,
"Cracks": 8,
"Discoloration": 5,
"Other": 7,
})
# Keep the catch-all category last by convention.
main = defects.drop("Other").sort_values(ascending=False)
data = pd.concat([main, defects[["Other"]]])
cum_pct = data.cumsum() / data.sum() * 100
table = pd.DataFrame({
"count": data,
"cumulative_pct": cum_pct.round(1),
})
print(table)The cumulative percentages are approximately 42.0%, 69.0%, 80.0%, 88.0%, 93.0%, and 100.0%. The line always ends at 100% because it represents the complete set of recorded events.
The Other category needs special care. It is a catch-all category, so it usually belongs at the end even if its count is larger than a named category. If “Other” is not a real category in the source data, do not invent it merely to make the chart look familiar.
Understand the chart anatomy
A Pareto chart combines two related displays:
- Bars show the size of each category.
- A line shows the cumulative percentage.
- The left vertical axis measures the bars.
- The right vertical axis measures percentages from 0% to 100%.
- A horizontal reference line, often at 80%, helps identify the categories near the chosen target.
The cumulative line is not another count series. It uses a different scale, which is why a second vertical axis is needed.
Build the chart
The following code creates a complete chart. The right axis is fixed from 0 to 100 so that the line remains easy to interpret.
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter
defects = pd.Series({
"Scratches": 42,
"Misalignment": 27,
"Wrong label": 11,
"Cracks": 8,
"Discoloration": 5,
"Other": 7,
})
main = defects.drop("Other").sort_values(ascending=False)
data = pd.concat([main, defects[["Other"]]])
cum_pct = data.cumsum() / data.sum() * 100
fig, ax = plt.subplots(figsize=(8, 4.5))
ax.bar(data.index, data.values, color="C0")
ax.set_ylabel("Defect count")
ax.tick_params(axis="x", rotation=30)
ax2 = ax.twinx()
ax2.plot(data.index, cum_pct.values, color="C1", marker="o")
ax2.set_ylim(0, 100)
ax2.yaxis.set_major_formatter(PercentFormatter())
ax2.axhline(80, color="gray", linestyle="--", linewidth=1)
ax2.set_ylabel("Cumulative share")
ax.set_title("Defects by type")
fig.tight_layout()
plt.show()Read the vital few
The “vital few” are the leading categories that explain a large share of the total. In this example, Scratches, Misalignment, and Wrong label account for exactly 80% of defects. The remaining categories form the “useful many”: they still matter, but they may not be the best starting point for improvement work.
Do not treat 80% as a mandatory cutoff. Choose a practical target based on the project. A team might focus on the first two categories if they are costly or easy to address, even when they explain less than 80% of events.
Choose the measure deliberately
Counts are useful when every event has similar impact. They can be misleading when categories have different costs or consequences. A rare defect that causes a large rework expense may deserve more attention than a frequent defect that is cheap to correct.
You can build the same type of chart from repair cost, downtime, customer impact, or risk score. State the measure clearly in the title and axis labels. Never compare a count-based Pareto chart with a cost-based chart as though they answer the same question.
> Callout note > A Pareto chart ranks priorities. It does not prove that the largest category causes the others, or that fixing it will produce the largest improvement.
Common mistakes and follow-up analysis
A chart can be misleading when the time period is too short, categories are inconsistent, or the total mixes different products or process steps. Check the data definition before interpreting the shape.
After taking action, create a second Pareto chart for the same measure and population. Compare the categories and total level before and after the change. You can also create separate charts for shifts, machines, products, or defect subtypes. This drill-down helps turn a broad category such as “Scratches” into more specific causes that the team can investigate.
Exercise
The cumulative percentages reach 69% after the first two categories and 80% after the third. Which categories belong to the vital few when the target is at least 80%?
Show answer
Scratches, Misalignment, and Wrong label. The third category must be included because it brings the cumulative share to the target.