A bar chart is used to show and compare the sizes or counts of different categories using rectangular bars. Each bar's length or height represents the value of that category.
Click to reveal answer
beginner
How do you create a simple vertical bar chart using Python's matplotlib?
Use plt.bar() with a list of categories for the x-axis and their values for the y-axis. For example:<br><pre>import matplotlib.pyplot as plt
categories = ['A', 'B', 'C']
values = [5, 7, 3]
plt.bar(categories, values)
plt.show()</pre>
Click to reveal answer
beginner
What is the difference between vertical and horizontal bar charts?
Vertical bar charts have bars going up and down, showing categories on the x-axis and values on the y-axis. Horizontal bar charts have bars going left to right, showing categories on the y-axis and values on the x-axis.
Click to reveal answer
intermediate
Why might you use a horizontal bar chart instead of a vertical one?
Horizontal bar charts are useful when category names are long or when you want to compare many categories clearly because the labels are easier to read on the y-axis.
Click to reveal answer
beginner
How can you add labels and a title to a bar chart in matplotlib?
Use plt.xlabel() and plt.ylabel() to add axis labels, and plt.title() to add a chart title. For example:
plt.bar(categories, values)
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Sample Bar Chart')
plt.show()
Click to reveal answer
Which matplotlib function creates a vertical bar chart?
Aplt.bar()
Bplt.plot()
Cplt.scatter()
Dplt.hist()
✗ Incorrect
plt.bar() creates vertical bar charts by default.
In a horizontal bar chart, which axis shows the categories?
Aboth axes
Bx-axis
Cy-axis
Dneither axis
✗ Incorrect
In horizontal bar charts, categories are shown on the y-axis.
What does the length of a bar represent in a bar chart?
AThe value or count of that category
BThe category name
CThe color of the bar
DThe number of bars
✗ Incorrect
The bar length shows the value or count for that category.
Which of these is NOT a good reason to use a horizontal bar chart?
ALong category names
BComparing many categories
CImproving label readability
DShowing time series data
✗ Incorrect
Time series data is better shown with line charts, not bar charts.
How do you add a title to a matplotlib bar chart?
Aplt.name('Your Title')
Bplt.title('Your Title')
Cplt.label('Your Title')
Dplt.header('Your Title')
✗ Incorrect
Use plt.title() to add a title to the chart.
Explain how to create a basic vertical bar chart in Python using matplotlib.
Think about what lists you need and the function to draw bars.
You got /5 concepts.
Describe when and why you would choose a horizontal bar chart over a vertical one.
Consider the axis where category names appear.
You got /4 concepts.
Practice
(1/5)
1. What does a bar chart mainly help you do with data?
easy
A. See differences between groups using bars
B. Calculate exact averages of numbers
C. Show data as a continuous line
D. Display data in a table format
Solution
Step 1: Understand the purpose of bar charts
Bar charts use bars to visually compare values across categories.
Step 2: Compare options to bar chart use
Only See differences between groups using bars describes using bars to see differences between groups.
Final Answer:
See differences between groups using bars -> Option A
Quick Check:
Bar charts = Visual group comparison [OK]
Hint: Bar charts show group sizes as bars [OK]
Common Mistakes:
Confusing bar charts with line charts
Thinking bar charts show exact calculations
Mixing bar charts with tables
2. Which of these is the correct way to create a vertical bar chart using matplotlib in Python?
Using plt.bar() causes vertical bars, so to get horizontal bars, plt.barh() is needed.
Final Answer:
plt.bar() creates vertical bars; use plt.barh() for horizontal bars -> Option D
Quick Check:
Horizontal bars need plt.barh() [OK]
Hint: Use plt.barh() for horizontal bars [OK]
Common Mistakes:
Thinking labels must be numbers
Swapping order of plt.show() and plt.bar()
Using string values instead of numbers
5. You have sales data for 3 products: {'Product A': 30, 'Product B': 45, 'Product C': 25}. You want to create a bar chart showing these sales with product names on the x-axis and sales on the y-axis. Which code snippet correctly creates this chart with labels and a title?
hard
A. import matplotlib.pyplot as plt
sales = {'Product A': 30, 'Product B': 45, 'Product C': 25}
plt.barh(sales.values(), sales.keys())
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales by Product')
plt.show()
B. import matplotlib.pyplot as plt
sales = {'Product A': 30, 'Product B': 45, 'Product C': 25}
plt.bar(sales.keys(), sales.values())
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales by Product')
plt.show()
C. import matplotlib.pyplot as plt
sales = {'Product A': 30, 'Product B': 45, 'Product C': 25}
plt.plot(sales.keys(), sales.values())
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales by Product')
plt.show()
D. import matplotlib.pyplot as plt
sales = {'Product A': 30, 'Product B': 45, 'Product C': 25}
plt.bar(sales.values(), sales.keys())
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales by Product')
plt.show()
Solution
Step 1: Understand data and desired chart
We want vertical bars with product names on x-axis and sales on y-axis.
Step 2: Check code snippets for correct bar chart
import matplotlib.pyplot as plt
sales = {'Product A': 30, 'Product B': 45, 'Product C': 25}
plt.bar(sales.keys(), sales.values())
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales by Product')
plt.show() uses plt.bar() with keys as x and values as heights, plus labels and title correctly set.
Step 3: Identify errors in other options
import matplotlib.pyplot as plt
sales = {'Product A': 30, 'Product B': 45, 'Product C': 25}
plt.barh(sales.values(), sales.keys())
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales by Product')
plt.show() uses plt.barh() but swaps x and y labels incorrectly. import matplotlib.pyplot as plt
sales = {'Product A': 30, 'Product B': 45, 'Product C': 25}
plt.plot(sales.keys(), sales.values())
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales by Product')
plt.show() uses plt.plot() which is a line chart. import matplotlib.pyplot as plt
sales = {'Product A': 30, 'Product B': 45, 'Product C': 25}
plt.bar(sales.values(), sales.keys())
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales by Product')
plt.show() swaps keys and values in plt.bar(), causing wrong axes.
Final Answer:
Code that correctly creates the labeled vertical bar chart -> Option B
Quick Check:
Keys on x, values on y with plt.bar() [OK]
Hint: Use plt.bar(keys, values) with labels and title [OK]
Common Mistakes:
Swapping keys and values in plt.bar()
Using plt.plot() instead of plt.bar()
Mixing up plt.bar() and plt.barh() axes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.