0
0
Matplotlibdata~10 mins

Horizontal bar chart with plt.barh in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Horizontal bar chart with plt.barh
Prepare data: categories, values
Call plt.barh(categories, values)
Matplotlib draws horizontal bars
Show or save the plot
END
We prepare categories and their values, then call plt.barh to draw horizontal bars, and finally display the chart.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
categories = ['A', 'B', 'C']
values = [5, 3, 7]
plt.barh(categories, values)
plt.show()
This code draws a horizontal bar chart with three categories and their values.
Execution Table
StepActionInputOutput/Effect
1Prepare categories list['A', 'B', 'C']List ready for plotting
2Prepare values list[5, 3, 7]Values ready for plotting
3Call plt.barh(categories, values)categories=['A','B','C'], values=[5,3,7]Horizontal bars drawn: A=5, B=3, C=7
4Call plt.show()NonePlot window opens showing horizontal bar chart
5User closes plot windowNoneExecution ends
💡 Plot displayed and program ends after user closes the window
Variable Tracker
VariableStartAfter Step 1After Step 2Final
categoriesNone['A', 'B', 'C']['A', 'B', 'C']['A', 'B', 'C']
valuesNoneNone[5, 3, 7][5, 3, 7]
Key Moments - 3 Insights
Why do we pass categories first and values second in plt.barh?
plt.barh expects the first argument as the y-axis labels (categories) and the second as the bar lengths (values). This matches the horizontal layout where categories go vertically.
What happens if categories and values lists have different lengths?
Matplotlib will raise an error because each category needs a corresponding value. See execution_table step 3 where inputs must match in length.
Why do we call plt.show() after plt.barh?
plt.barh prepares the plot but does not display it. plt.show() opens the window to display the chart, as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'values' after Step 2?
A['A', 'B', 'C']
B[5, 3, 7]
CNone
D[3, 5, 7]
💡 Hint
Check variable_tracker row for 'values' after Step 2
At which step does the horizontal bar chart actually get drawn?
AStep 3
BStep 1
CStep 4
DStep 5
💡 Hint
See execution_table action column for when plt.barh is called
If we swapped categories and values in plt.barh, what would happen?
AMatplotlib would raise an error immediately
BPlot would show correctly with no change
CBars would be drawn with categories as lengths and values as labels
DBars would be vertical instead of horizontal
💡 Hint
Recall plt.barh expects categories first, values second (see key_moments)
Concept Snapshot
plt.barh(categories, values) draws horizontal bars.
First argument: categories (y-axis labels).
Second argument: values (bar lengths).
Call plt.show() to display the plot.
Categories and values must be same length.
Useful for comparing quantities across categories horizontally.
Full Transcript
We start by preparing two lists: categories and values. Categories are labels like 'A', 'B', 'C'. Values are numbers like 5, 3, 7. Then we call plt.barh(categories, values) which draws horizontal bars for each category with length equal to its value. Finally, plt.show() displays the chart window. The variables categories and values hold the data throughout. It's important that categories come first and values second in plt.barh. If their lengths differ, matplotlib will raise an error. The plot only appears after plt.show() is called. This method is great for visualizing data horizontally, making category comparison easy.