0
0
Matplotlibdata~15 mins

Donut chart variation in Matplotlib - Deep Dive

Choose your learning style9 modes available
Overview - Donut chart variation
What is it?
A donut chart is a circular chart similar to a pie chart but with a blank center, creating a donut-like shape. It shows parts of a whole, making it easy to compare proportions visually. The blank center can be used to display additional information or improve aesthetics. Donut charts help highlight data segments while keeping the chart clean and readable.
Why it matters
Donut charts solve the problem of cluttered pie charts by providing space in the center for labels or totals, making data easier to understand at a glance. Without donut charts, visualizations might be harder to interpret, especially when many categories are present. They improve communication of proportions in reports, presentations, and dashboards, helping people make better decisions based on data.
Where it fits
Before learning donut charts, you should understand basic pie charts and how to use matplotlib for plotting. After mastering donut charts, you can explore advanced visualization techniques like nested donut charts, interactive charts, or combining charts with annotations for richer storytelling.
Mental Model
Core Idea
A donut chart is a pie chart with a hole in the middle, allowing space for extra information while still showing parts of a whole.
Think of it like...
Imagine a round cake sliced into pieces, but with the center removed so you can write a message or place a decoration in the empty space.
  _________
 /         \
|  Slice 1  |
|  Slice 2  |
|  Slice 3  |
 \___   ___/
     | |
     | |  <-- Empty center space (hole)
     |_|
Build-Up - 6 Steps
1
FoundationUnderstanding basic pie charts
πŸ€”
Concept: Learn what a pie chart is and how it represents data proportions.
A pie chart divides a circle into slices where each slice size shows the proportion of a category. For example, if you have sales data for 3 products, the pie chart shows how much each product contributes to total sales. In matplotlib, you use plt.pie() to create this chart.
Result
A circular chart with colored slices representing each category's share.
Understanding pie charts is essential because donut charts build directly on this concept by modifying the pie chart's center.
2
FoundationCreating a simple donut chart
πŸ€”
Concept: Learn how to make a donut chart by adding a hole in the pie chart.
In matplotlib, you create a donut chart by drawing a pie chart and then adding a white circle in the center to cover the middle part. This creates the donut shape. The size of the hole is controlled by the circle's radius.
Result
A pie chart with a blank center, visually appearing as a donut.
Knowing how to add a center circle transforms a pie chart into a donut chart, opening new design possibilities.
3
IntermediateCustomizing donut chart appearance
πŸ€”
Concept: Explore how to change colors, labels, and hole size for better visuals.
You can customize slice colors using a color list, add labels to slices for clarity, and adjust the hole size by changing the radius of the center circle. These tweaks help make the chart more readable and visually appealing.
Result
A donut chart with distinct colors, clear labels, and a balanced hole size.
Customizing appearance improves communication by making data easier to interpret and visually attractive.
4
IntermediateAdding center text inside donut hole
πŸ€”Before reading on: do you think you can add text inside the donut hole using matplotlib's pie function directly? Commit to your answer.
Concept: Learn how to place text in the empty center to display totals or messages.
Matplotlib does not add text inside the donut hole automatically. You use plt.text() to place text at the center coordinates. This can show total values or other info, enhancing the chart's usefulness.
Result
A donut chart with informative text displayed in the center hole.
Adding center text leverages the empty space to convey more information without cluttering the slices.
5
AdvancedCreating nested donut charts
πŸ€”Before reading on: do you think nested donut charts are just multiple pie charts stacked or something else? Commit to your answer.
Concept: Build multi-level donut charts to show hierarchical data.
Nested donut charts have multiple rings, each representing a data level. You draw several pie charts with different radii and hole sizes, layering them to create rings. This shows relationships between categories and subcategories.
Result
A multi-ring donut chart visualizing hierarchical proportions.
Nested donuts reveal complex data structures in a compact, intuitive visual form.
6
ExpertHandling small slices and label overlap
πŸ€”Before reading on: do you think matplotlib automatically prevents label overlap in donut charts? Commit to your answer.
Concept: Learn techniques to manage tiny slices and overlapping labels for clarity.
Small slices can cause labels to overlap or be unreadable. Experts use strategies like grouping small slices into an 'Other' category, adjusting label positions manually, or using leader lines. These improve chart clarity and prevent misinterpretation.
Result
A clean donut chart with readable labels and no clutter from tiny slices.
Managing label overlap is crucial for professional-quality charts that communicate data clearly.
Under the Hood
Matplotlib draws pie charts by calculating angles for each slice based on data proportions. To create a donut chart, it overlays a white circle at the center, masking the middle part of the pie. Text placement uses coordinate positioning relative to the plot's center. Nested donuts are multiple pie charts drawn with different radii, layered to form rings.
Why designed this way?
The donut chart design evolved to improve pie charts by adding a center space for extra info or aesthetics. Using a white circle overlay is a simple, flexible method that works with existing pie chart code. Alternatives like redrawing slices with holes would be more complex and less efficient.
Pie chart slices calculation
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚  Data values  β”‚
  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ Calculate     β”‚
  β”‚ slice angles  β”‚
  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ Draw pie chartβ”‚
  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ Overlay white β”‚
  β”‚ center circle β”‚
  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ Add center    β”‚
  β”‚ text if neededβ”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Myth Busters - 3 Common Misconceptions
Quick: Do you think a donut chart always shows more information than a pie chart? Commit to yes or no.
Common Belief:Donut charts always provide more data insight than pie charts because of the center hole.
Tap to reveal reality
Reality:Donut charts mainly improve aesthetics and allow center text, but they do not inherently show more data than pie charts.
Why it matters:Believing donut charts add data can lead to overusing them where a simple pie chart would be clearer.
Quick: Do you think matplotlib automatically adjusts labels to avoid overlap in donut charts? Commit to yes or no.
Common Belief:Matplotlib automatically prevents label overlap in donut charts.
Tap to reveal reality
Reality:Matplotlib does not automatically manage label overlap; manual adjustments or strategies are needed.
Why it matters:Ignoring label overlap can produce confusing charts that mislead viewers.
Quick: Do you think nested donut charts are just decorative and not useful for data? Commit to yes or no.
Common Belief:Nested donut charts are only decorative and do not add meaningful data representation.
Tap to reveal reality
Reality:Nested donut charts effectively show hierarchical data relationships in a compact form.
Why it matters:Underestimating nested donuts may cause missed opportunities to visualize complex data clearly.
Expert Zone
1
The hole size affects perception: too large can make slices look thin and hard to compare, too small reduces center space benefits.
2
Center text positioning must consider figure size and DPI to remain centered and readable across different output formats.
3
When layering nested donuts, slice order and color consistency are critical to avoid confusing viewers.
When NOT to use
Donut charts are not ideal when precise quantitative comparison is needed; bar charts or tables are better. Also, avoid donut charts with too many small slices as they become cluttered. For interactive exploration, consider tools like Plotly or Tableau instead.
Production Patterns
Professionals use donut charts in dashboards to show category shares with center totals, in reports to highlight key metrics, and nested donuts to represent multi-level data like sales by region and product. They combine donuts with annotations and color coding for clarity.
Connections
Pie chart
Donut charts are a direct variation of pie charts with a center hole added.
Understanding pie charts fully helps grasp donut charts since they share the same data representation principles.
Hierarchical data visualization
Nested donut charts build on hierarchical visualization concepts by showing multiple data levels in rings.
Knowing hierarchical visualization helps design nested donuts that clearly communicate complex data.
User interface design
Donut charts use empty center space similar to UI design principles of whitespace for clarity and focus.
Recognizing the role of whitespace in UI helps appreciate why donut charts improve readability and aesthetics.
Common Pitfalls
#1Creating a donut chart without adding a center circle, resulting in a normal pie chart.
Wrong approach:plt.pie(data, labels=labels) plt.show()
Correct approach:plt.pie(data, labels=labels) centre_circle = plt.Circle((0,0),0.70,fc='white') fig = plt.gcf() fig.gca().add_artist(centre_circle) plt.show()
Root cause:Not knowing that the donut hole is created by overlaying a white circle on the pie chart.
#2Adding labels directly on small slices causing overlap and unreadable text.
Wrong approach:plt.pie(data, labels=labels, autopct='%1.1f%%') plt.show()
Correct approach:# Group small slices or adjust label positions manually # Example: combine small slices into 'Other' data = [40, 35, 15, 10] labels = ['A', 'B', 'Other', 'Other'] plt.pie(data, labels=labels, autopct='%1.1f%%') plt.show()
Root cause:Ignoring the visual limits of label space and not managing small slice clutter.
#3Placing center text without adjusting coordinates, causing misalignment.
Wrong approach:plt.text(0, 0, 'Total: 100') plt.show()
Correct approach:plt.text(0, 0, 'Total: 100', horizontalalignment='center', verticalalignment='center', fontsize=12) plt.show()
Root cause:Not specifying alignment parameters leads to text not being centered properly.
Key Takeaways
Donut charts are pie charts with a center hole that improves readability and allows extra information display.
Creating a donut chart in matplotlib involves overlaying a white circle on a pie chart and optionally adding center text.
Customizing colors, labels, and hole size enhances the chart's clarity and visual appeal.
Nested donut charts visualize hierarchical data by layering multiple rings, each representing a data level.
Managing small slices and label overlap is essential for professional, clear donut charts.