0
0
Matplotlibdata~10 mins

Diverging bar charts in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Diverging bar charts
Prepare data with positive and negative values
Sort or order data if needed
Assign colors based on value sign
Plot horizontal bars with lengths = values
Add labels and title
Display chart with diverging bars
This flow shows how to create a diverging bar chart by preparing data, assigning colors based on positive or negative values, plotting bars, and displaying the chart.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
values = [20, -15, 30, -10, 5]
colors = ['green' if v >= 0 else 'red' for v in values]
plt.barh(range(len(values)), values, color=colors)
plt.show()
This code plots a horizontal bar chart with bars colored green for positive and red for negative values.
Execution Table
StepActionVariable/ValueResult/Effect
1Define values listvalues = [20, -15, 30, -10, 5]List with positive and negative numbers created
2Assign colors based on signcolors = ['green' if v >= 0 else 'red' for v in values]colors = ['green', 'red', 'green', 'red', 'green']
3Call plt.barh with values and colorsplt.barh(range(5), values, color=colors)Horizontal bars plotted with correct colors
4Call plt.show()plt.show()Chart window opens showing diverging bar chart
5End-Execution stops after displaying chart
💡 Chart displayed and script ends
Variable Tracker
VariableStartAfter Step 1After Step 2Final
valuesundefined[20, -15, 30, -10, 5][20, -15, 30, -10, 5][20, -15, 30, -10, 5]
colorsundefinedundefined['green', 'red', 'green', 'red', 'green']['green', 'red', 'green', 'red', 'green']
Key Moments - 2 Insights
Why do some bars appear in red and others in green?
Bars are colored based on whether their values are negative or positive, as shown in step 2 of the execution table where colors are assigned conditionally.
What does plt.barh(range(len(values)), values, color=colors) do exactly?
It plots horizontal bars at positions 0 to 4 with lengths from values and colors from the colors list, as explained in step 3 of the execution table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the color assigned to the value -10 at step 2?
Ablue
Bgreen
Cred
Dyellow
💡 Hint
Check the colors list in step 2 of the execution table where negative values get 'red'.
At which step does the chart actually get displayed?
AStep 1
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the action in step 4 where plt.show() is called.
If the values list was all positive, how would the colors list change at step 2?
AAll colors would be 'green'
BAll colors would be 'red'
CColors would alternate between 'green' and 'red'
DColors would be empty
💡 Hint
Refer to the color assignment rule in step 2: positive values get 'green'.
Concept Snapshot
Diverging bar charts show positive and negative values with bars going right or left.
Use plt.barh() for horizontal bars.
Assign colors based on sign: green for positive, red for negative.
Plot bars with lengths = values and colors assigned.
Add labels and show chart with plt.show().
Full Transcript
This visual execution traces how to create a diverging bar chart using matplotlib in Python. First, a list of values with positive and negative numbers is defined. Then, colors are assigned to each value: green if the value is positive or zero, red if negative. Next, plt.barh() plots horizontal bars at positions 0 to 4 with lengths equal to the values and colors assigned accordingly. Finally, plt.show() displays the chart window with the diverging bars. The execution table shows each step and variable changes, helping beginners understand how the chart is built step-by-step.