Plotting a pie chart out of a dictionary
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
A Python dictionary is a natural source for a pie chart because it already maps category names to numeric values. The main job is turning the dictionary into a list of labels and a list of sizes, then passing those to matplotlib.pyplot.pie with options that make the chart readable.
Converting a Dictionary Into Pie Chart Inputs
Suppose you have category totals stored in a dictionary:
matplotlib does not plot dictionaries directly. It wants sequences, so extract keys and values in matching order.
That is the minimum working example. The order of the keys and values stays aligned because both lists come from the same dictionary in the same iteration order.
Making the Pie Chart More Readable
Most real charts need at least percentages and equal aspect ratio.
The important options are:
- '
autopctto display percentages' - '
startangleto rotate the chart into a cleaner position' - '
axis("equal")to keep the pie circular instead of stretched'
Sorting or Highlighting Important Categories
If the dictionary is large or built from aggregated data, it is often helpful to sort it before plotting.
The explode list offsets one slice from the center, which can be useful when one category deserves emphasis.
Handling Small or Many Categories
Pie charts become hard to read when there are too many categories or many tiny slices. In that case, combine small values into an Other bucket before plotting.
That preprocessing step often improves the final chart more than any styling option.
When a Pie Chart Is the Wrong Choice
A pie chart works best for a small number of parts that represent a whole. If you need accurate comparison across many categories, a bar chart is usually easier to read.
This is worth mentioning because many pie chart problems are really chart-selection problems.
Common Pitfalls
A common mistake is passing the dictionary itself to plt.pie and expecting it to understand labels automatically. Convert the dictionary into separate sequences first.
Another issue is misaligned labels and values. Always derive both from the same ordered sequence of items so category names match the correct slice sizes.
Too many slices is another frequent problem. Even technically correct pie charts can become unreadable when there are many tiny categories.
Finally, remember that pie charts represent proportions. Negative values or unrelated magnitudes usually indicate that a different chart type would be more appropriate.
Summary
- Convert dictionary keys and values into label and size lists before plotting.
- Use
autopct,startangle, andaxis("equal")for a cleaner result. - Sort or explode slices when one category should stand out.
- Group tiny categories into
Otherwhen the chart gets crowded. - Prefer a bar chart when comparison matters more than part-to-whole display.
Related reading
- Plotting a ROC curve in scikit yields only 3 points
- Plotting decision boundary for High Dimension Data
- Plotting in a non-blocking way with Matplotlib
- Plotting numpy array using Seaborn
- Polygon enclosing a set of points
- Populate nested array in mongoose
- Poetry fails to install tensorflow
- Pool.apply_async nested function is not executed

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.