Bird
Raised Fist0
Matplotlibdata~10 mins

Vertical bar chart with plt.bar in Matplotlib - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Vertical bar chart with plt.bar
Prepare data: categories, values
↓
Call plt.bar(categories, values)
↓
Bars drawn vertically
↓
Add labels, title
↓
Show plot with plt.show()
This flow shows how data is prepared, passed to plt.bar to draw vertical bars, then labels and title are added before displaying the chart.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
categories = ['A', 'B', 'C']
values = [5, 7, 3]
plt.bar(categories, values)
plt.show()
This code draws a vertical bar chart with 3 bars labeled A, B, and C with heights 5, 7, and 3.
Execution Table
StepActionInputResult
1Prepare categories['A', 'B', 'C']Categories list ready
2Prepare values[5, 7, 3]Values list ready
3Call plt.barcategories=['A','B','C'], values=[5,7,3]Bars created at positions 0,1,2 with heights 5,7,3
4Draw barsBars dataVertical bars drawn on plot
5Call plt.showNonePlot window opens showing the bar chart
💡 Plot displayed and program waits for user to close window
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
categoriesNone['A', 'B', 'C']['A', 'B', 'C']['A', 'B', 'C']['A', 'B', 'C']
valuesNoneNone[5, 7, 3][5, 7, 3][5, 7, 3]
Key Moments - 2 Insights
Why do the bars appear at positions 0, 1, and 2 instead of the category names?
plt.bar uses the categories as labels on the x-axis but places bars at numeric positions 0, 1, 2 internally. The labels show under each bar as seen in step 3 of the execution_table.
What happens if the lengths of categories and values lists do not match?
Matplotlib will raise an error because each category needs a corresponding value. This is why in step 2 and 3 both lists have the same length.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what are the heights of the bars created?
AA, B, C
B5, 7, 3
C0, 1, 2
DNone
💡 Hint
Check the 'Result' column in step 3 of execution_table for bar heights.
At which step does the plot window open to show the bar chart?
AStep 5
BStep 2
CStep 4
DStep 3
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table for when plt.show() is called.
If you change values to [2, 4, 6], how will the bars change in the execution_table?
ACategories will change
BBars heights remain 5, 7, 3
CBars heights will be 2, 4, 6 at step 3
DPlot will not show
💡 Hint
Refer to the 'Input' and 'Result' columns in step 3 of execution_table.
Concept Snapshot
plt.bar(categories, values) draws vertical bars.
Categories label x-axis positions 0,1,2...
Values set bar heights.
Call plt.show() to display.
Lengths of categories and values must match.
Full Transcript
This visual execution shows how to create a vertical bar chart using matplotlib's plt.bar function. First, we prepare two lists: categories for the x-axis labels and values for the bar heights. Then plt.bar is called with these lists, which draws vertical bars at positions 0, 1, 2 with heights from values. Finally, plt.show() opens a window displaying the chart. The execution table traces each step, showing how variables categories and values are set and used. Key moments clarify why bars are positioned numerically and the importance of matching list lengths. The quiz tests understanding of bar heights, display timing, and effect of changing values.

Practice

(1/5)
1. What does the plt.bar() function do in matplotlib?
easy
A. It creates a scatter plot with dots.
B. It creates a line chart connecting data points.
C. It creates a pie chart showing proportions.
D. It creates a vertical bar chart using categories and their values.

Solution

  1. Step 1: Understand the purpose of plt.bar()

    The plt.bar() function is designed to draw vertical bars representing data values for different categories.
  2. Step 2: Compare with other chart types

    Other functions like plt.plot() create line charts, plt.scatter() creates scatter plots, and plt.pie() creates pie charts, so they are different from plt.bar().
  3. Final Answer:

    It creates a vertical bar chart using categories and their values. -> Option D
  4. Quick Check:

    plt.bar() = vertical bar chart [OK]
Hint: Remember: bar = vertical bars for categories [OK]
Common Mistakes:
  • Confusing bar chart with line or pie charts
  • Thinking plt.bar() draws horizontal bars
  • Mixing up plt.bar() with scatter plot functions
2. Which of the following is the correct syntax to create a vertical bar chart with categories ['A', 'B', 'C'] and values [5, 7, 3]?
easy
A. plt.bar(['A', 'B', 'C'], [5, 7, 3])
B. plt.bar([5, 7, 3], ['A', 'B', 'C'])
C. plt.bar('A', 5, 'B', 7, 'C', 3)
D. plt.bar([5, 7, 3])

Solution

  1. Step 1: Identify correct parameter order for plt.bar()

    The first argument should be the categories (x-axis labels), and the second argument should be the heights (values).
  2. Step 2: Check each option

    plt.bar(['A', 'B', 'C'], [5, 7, 3]) uses categories first and values second, which is correct. plt.bar([5, 7, 3], ['A', 'B', 'C']) reverses them, which is wrong. plt.bar('A', 5, 'B', 7, 'C', 3) uses multiple arguments incorrectly. plt.bar([5, 7, 3]) misses categories.
  3. Final Answer:

    plt.bar(['A', 'B', 'C'], [5, 7, 3]) -> Option A
  4. Quick Check:

    Categories first, values second in plt.bar() [OK]
Hint: Categories go first, values second in plt.bar() [OK]
Common Mistakes:
  • Swapping categories and values order
  • Passing multiple separate arguments instead of lists
  • Omitting categories or values
3. What will be the height of the bar labeled 'B' in this code?
import matplotlib.pyplot as plt
categories = ['A', 'B', 'C']
values = [4, 9, 2]
plt.bar(categories, values)
plt.show()
medium
A. 9
B. Cannot determine
C. 2
D. 4

Solution

  1. Step 1: Match categories with values

    The list categories is ['A', 'B', 'C'] and values is [4, 9, 2]. The bar for 'B' corresponds to the second value, 9.
  2. Step 2: Confirm bar height for 'B'

    Since 'B' is the second category, its bar height is the second value, which is 9.
  3. Final Answer:

    9 -> Option A
  4. Quick Check:

    Bar 'B' height = 9 [OK]
Hint: Match category index to value index [OK]
Common Mistakes:
  • Mixing up category and value positions
  • Assuming bar height is category label
  • Ignoring list order
4. Identify the error in this code that tries to plot a vertical bar chart:
import matplotlib.pyplot as plt
categories = ['X', 'Y', 'Z']
values = [3, 6]
plt.bar(categories, values)
plt.show()
medium
A. The categories list has fewer items than values list.
B. plt.bar() requires numeric categories, not strings.
C. The values list has fewer items than categories list.
D. plt.show() is missing.

Solution

  1. Step 1: Compare lengths of categories and values

    The categories list has 3 items ['X', 'Y', 'Z'], but values list has only 2 items [3, 6]. This mismatch causes an error.
  2. Step 2: Understand plt.bar() requirements

    Each category must have a corresponding value. Since values are fewer, matplotlib will raise a ValueError.
  3. Final Answer:

    The values list has fewer items than categories list. -> Option C
  4. Quick Check:

    Lengths must match: values shorter than categories [OK]
Hint: Check categories and values list lengths match [OK]
Common Mistakes:
  • Ignoring length mismatch between categories and values
  • Thinking categories must be numeric
  • Forgetting plt.show()
5. You want to create a vertical bar chart showing sales for products ['Pen', 'Notebook', 'Eraser'] with sales [10, 15, 10]. You also want to highlight the product with the highest sales in red and others in blue. Which code correctly achieves this?
hard
A. plt.bar(['Pen', 'Notebook', 'Eraser'], [10, 15, 10], color='red') plt.show()
B. colors = ['blue', 'red', 'blue'] plt.bar(['Pen', 'Notebook', 'Eraser'], [10, 15, 10], color=colors) plt.show()
C. colors = ['red', 'blue', 'blue'] plt.bar(['Pen', 'Notebook', 'Eraser'], [10, 15, 10], color=colors) plt.show()
D. plt.bar(['Pen', 'Notebook', 'Eraser'], [10, 15, 10]) plt.show()

Solution

  1. Step 1: Identify highest sales product

    The sales are [10, 15, 10], so 'Notebook' has the highest sales (15).
  2. Step 2: Assign colors to highlight highest sales

    We want 'Notebook' bar in red and others in blue. colors = ['blue', 'red', 'blue'] plt.bar(['Pen', 'Notebook', 'Eraser'], [10, 15, 10], color=colors) plt.show() sets colors as ['blue', 'red', 'blue'], matching the order of products.
  3. Step 3: Check other options

    plt.bar(['Pen', 'Notebook', 'Eraser'], [10, 15, 10], color='red') plt.show() colors all bars red, not highlighting only highest. colors = ['red', 'blue', 'blue'] plt.bar(['Pen', 'Notebook', 'Eraser'], [10, 15, 10], color=colors) plt.show() colors 'Pen' red incorrectly. plt.bar(['Pen', 'Notebook', 'Eraser'], [10, 15, 10]) plt.show() uses default colors without highlight.
  4. Final Answer:

    colors = ['blue', 'red', 'blue'] plt.bar(['Pen', 'Notebook', 'Eraser'], [10, 15, 10], color=colors) plt.show() -> Option B
  5. Quick Check:

    Color list matches bars, red on highest sales [OK]
Hint: Use color list matching bars, red for highest [OK]
Common Mistakes:
  • Coloring all bars same color
  • Assigning wrong color order
  • Not using color parameter
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard