0
0
Matplotlibdata~10 mins

Basic pie chart with plt.pie in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Basic pie chart with plt.pie
Prepare data values
Call plt.pie() with data
Matplotlib calculates slice sizes
Draw pie slices on plot
Display pie chart with plt.show()
This flow shows how data is prepared, passed to plt.pie(), which calculates and draws the pie slices, then displays the chart.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
sizes = [30, 20, 50]
plt.pie(sizes)
plt.show()
This code draws a pie chart with three slices sized 30, 20, and 50.
Execution Table
StepActionInput DataCalculationOutput/Plot
1Prepare data[30, 20, 50]Sum = 100Data ready for pie chart
2Call plt.pie()[30, 20, 50]Calculate slice angles: 108°, 72°, 180°Slices sized accordingly
3Draw slicesAngles from step 2Draw arcs for each slicePie chart with 3 slices drawn
4Show plotPie chart objectRender chart windowPie chart displayed on screen
5End--Execution complete
💡 All slices drawn and chart displayed, execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
sizesundefined[30, 20, 50][30, 20, 50][30, 20, 50][30, 20, 50]
sum_sizesundefined100100100100
slice_anglesundefinedundefined[108°, 72°, 180°][108°, 72°, 180°][108°, 72°, 180°]
Key Moments - 3 Insights
Why do the slice angles add up to 360 degrees?
Because plt.pie() converts data values into angles proportional to their share of the total sum, and a full circle is 360 degrees. See execution_table step 2.
What happens if the data values do not sum to 100?
plt.pie() automatically calculates the sum and scales slices accordingly, so the sum does not need to be 100. This is shown in variable_tracker where sum_sizes is calculated.
Why do we need plt.show() after plt.pie()?
plt.pie() prepares the pie chart but does not display it. plt.show() opens the window to display the chart. See execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the sum of the data values after step 1?
A50
B100
C30
D200
💡 Hint
Check the 'Calculation' column in execution_table row for step 1.
At which step are the pie slices actually drawn on the plot?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in execution_table to find when drawing happens.
If the sizes list changed to [10, 10, 80], how would the slice angles change?
AThey would be [36°, 36°, 288°]
BThey would be [30°, 20°, 50°]
CThey would be [10°, 10°, 80°]
DThey would be equal slices
💡 Hint
Use the formula: angle = (value / sum) * 360 degrees, see variable_tracker slice_angles.
Concept Snapshot
plt.pie(data)
- data: list of values for slices
- Automatically calculates slice sizes as % of total
- Draws pie chart slices
- Use plt.show() to display
- Slices angles sum to 360 degrees
Full Transcript
This visual execution shows how to create a basic pie chart using matplotlib's plt.pie function. First, data values are prepared as a list. Then plt.pie is called with this data, which calculates the sum and converts each value into an angle proportional to the total. The pie slices are drawn based on these angles. Finally, plt.show() displays the pie chart window. Variables like sizes, sum_sizes, and slice_angles track the data and calculations step-by-step. Key moments clarify why angles sum to 360 degrees, how plt.pie handles sums, and the role of plt.show(). The quizzes test understanding of sums, drawing steps, and angle calculations for different data.