0
0
Matplotlibdata~10 mins

Donut chart variation in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Donut chart variation
Prepare data and labels
Create pie chart with wedges
Create donut hole with wedgeprops
Display donut chart
END
The flow shows preparing data, creating a pie chart, creating the donut hole with wedgeprops, then displaying the chart.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
sizes = [30, 20, 25, 25]
labels = ['A', 'B', 'C', 'D']
plt.pie(sizes, labels=labels, wedgeprops=dict(width=0.4))
plt.show()
This code creates a donut chart by setting wedge width less than 1 to create a hole in the pie.
Execution Table
StepActionData/ParameterResult/Output
1Define sizes[30, 20, 25, 25]Data ready for chart
2Define labels['A', 'B', 'C', 'D']Labels ready for chart
3Call plt.piesizes and labels, wedge width=0.4Pie chart wedges created with hole
4Call plt.showNoneDonut chart displayed on screen
5EndNoneExecution stops after showing chart
💡 Chart displayed and program ends
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
sizesundefined[30, 20, 25, 25][30, 20, 25, 25][30, 20, 25, 25][30, 20, 25, 25]
labelsundefinedundefined['A', 'B', 'C', 'D']['A', 'B', 'C', 'D']['A', 'B', 'C', 'D']
Key Moments - 2 Insights
Why does setting wedgeprops width less than 1 create a donut shape?
Because wedgeprops width controls the thickness of pie slices. Setting it to 0.4 leaves a hole in the center, as shown in execution_table step 3.
What happens if we don't call plt.show()?
The chart will not display on screen. Execution_table step 4 shows plt.show triggers the display.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'sizes' after step 2?
Aundefined
B['A', 'B', 'C', 'D']
C[30, 20, 25, 25]
D[25, 25, 30, 20]
💡 Hint
Check variable_tracker row for 'sizes' after Step 2
At which step is the donut hole created in the chart?
AStep 3
BStep 4
CStep 1
DStep 2
💡 Hint
See execution_table step 3 where wedge width is set to 0.4
If wedgeprops width was set to 1, what would happen?
ADonut hole becomes bigger
BChart becomes a full pie with no hole
CChart will not display
DLabels disappear
💡 Hint
Recall wedge width controls slice thickness; 1 means full slices with no hole
Concept Snapshot
Donut chart variation in matplotlib:
- Use plt.pie() with wedgeprops={'width': value}
- width < 1 creates a hole (donut)
- Provide sizes and labels
- Call plt.show() to display
- Adjust width to control hole size
Full Transcript
This visual execution shows how to create a donut chart using matplotlib. First, we prepare the data sizes and labels. Then we call plt.pie with wedgeprops width set to 0.4 to create slices with a hole in the center. Finally, plt.show displays the donut chart. Variables sizes and labels remain unchanged after definition. The key step is setting wedgeprops width less than 1 to get the donut shape. Without plt.show, the chart won't appear. The quiz tests understanding of variable values and the effect of wedge width on the chart shape.