0
0
Matplotlibdata~10 mins

Horizontal grouped bars in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Horizontal grouped bars
Prepare data for groups and categories
Calculate bar positions for each group
Plot horizontal bars for each category side-by-side
Add labels, legend, and display plot
END
The flow shows preparing data, calculating positions for grouped bars, plotting horizontal bars side-by-side, then labeling and displaying the chart.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt

labels = ['G1', 'G2', 'G3']
cat1 = [5, 7, 3]
cat2 = [6, 2, 4]

bar_width = 0.35
indices = range(len(labels))

plt.barh([i for i in indices], cat1, height=bar_width, label='Cat1')
plt.barh([i + bar_width for i in indices], cat2, height=bar_width, label='Cat2')

plt.yticks([i + bar_width/2 for i in indices], labels)
plt.legend()
plt.show()
This code plots two categories of horizontal bars grouped by labels G1, G2, G3 side-by-side.
Execution Table
StepActionVariable/ValueEffect/Output
1Define labelslabels = ['G1', 'G2', 'G3']Groups to plot
2Define category valuescat1 = [5,7,3], cat2 = [6,2,4]Data for bars
3Set bar widthbar_width = 0.35Thickness of each bar
4Calculate indicesindices = [0,1,2]Positions for groups
5Plot cat1 barsplt.barh(indices, cat1, height=0.35)Bars at positions 0,1,2 with cat1 values
6Plot cat2 barsplt.barh([i+0.35 for i in indices], cat2, height=0.35)Bars shifted up by 0.35 for cat2
7Set y-ticksplt.yticks([i+0.175 for i in indices], labels)Labels centered between grouped bars
8Add legendplt.legend()Shows category labels
9Show plotplt.show()Displays horizontal grouped bar chart
10End-Plot complete
💡 All bars plotted and displayed, execution ends.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
labelsundefined['G1', 'G2', 'G3']['G1', 'G2', 'G3']['G1', 'G2', 'G3']['G1', 'G2', 'G3']
cat1undefined[5,7,3][5,7,3][5,7,3][5,7,3]
cat2undefined[6,2,4][6,2,4][6,2,4][6,2,4]
bar_widthundefinedundefined0.350.350.35
indicesundefinedundefined[0,1,2][0,1,2][0,1,2]
cat2_positionsundefinedundefinedundefined[0.35,1.35,2.35][0.35,1.35,2.35]
Key Moments - 3 Insights
Why do we add bar_width to indices for the second category bars?
Adding bar_width shifts the second category bars up so they don't overlap the first category bars, creating side-by-side grouping as shown in execution_table step 6.
How do y-ticks align with grouped bars?
Y-ticks are set at indices plus half the bar_width (step 7) to center labels between the two bars in each group.
What happens if bar_width is too large?
Bars may overlap or labels may misalign. The bar_width controls thickness and spacing, so it must be chosen carefully as in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what are the y positions for the second category bars?
A[0.35, 1.35, 2.35]
B[0.175, 1.175, 2.175]
C[0, 1, 2]
D[1, 2, 3]
💡 Hint
Check the 'Variable/Value' column at step 6 in execution_table for cat2 bar positions.
According to variable_tracker, what is the value of 'indices' after step 4?
A[1, 2, 3]
B[0, 1, 2]
C[0.35, 1.35, 2.35]
Dundefined
💡 Hint
Look at the 'indices' row under 'After Step 4' in variable_tracker.
If bar_width was changed to 0.5, how would the y-ticks positions change?
AThey would be at indices + 0.5
BThey would stay at indices
CThey would be at indices + 0.25
DThey would be at indices + 1
💡 Hint
Y-ticks are set at indices plus half the bar_width as shown in execution_table step 7.
Concept Snapshot
Horizontal grouped bars:
- Use plt.barh() for horizontal bars
- Calculate positions for each group and category
- Shift bars by bar_width to group side-by-side
- Set y-ticks centered between bars
- Add legend and show plot
- Controls thickness and spacing with bar_width
Full Transcript
This visual execution traces how to create horizontal grouped bar charts using matplotlib. We start by defining group labels and two categories of data. We set a bar width to control thickness. We calculate indices for group positions. We plot the first category bars at these indices. Then we plot the second category bars shifted up by the bar width to avoid overlap. Y-ticks are set centered between the grouped bars for clear labeling. Finally, we add a legend and display the plot. Variables like labels, cat1, cat2, bar_width, and indices change step-by-step as shown. Key points include shifting bars to group them side-by-side and centering labels. The quiz tests understanding of bar positions and label alignment.