What function in matplotlib is used to create a horizontal bar chart?
The function plt.barh() is used to create horizontal bar charts in matplotlib.
Click to reveal answer
beginner
In a horizontal bar chart, which axis represents the categories and which axis represents the values?
In a horizontal bar chart, the y-axis represents the categories, and the x-axis represents the values.
Click to reveal answer
beginner
How do you add labels to the bars in a horizontal bar chart using matplotlib?
You can add labels by using plt.xlabel() for the x-axis, plt.ylabel() for the y-axis, and plt.title() for the chart title.
Click to reveal answer
intermediate
What parameter in plt.barh() controls the height of the bars?
The height parameter controls the thickness (height) of the horizontal bars in plt.barh().
Click to reveal answer
beginner
How can you change the color of the bars in a horizontal bar chart?
You can change the bar colors by passing the color parameter to plt.barh(), for example, color='skyblue'.
Click to reveal answer
Which matplotlib function creates a horizontal bar chart?
Aplt.barh()
Bplt.bar()
Cplt.plot()
Dplt.scatter()
✗ Incorrect
plt.barh() creates horizontal bar charts, while plt.bar() creates vertical bar charts.
In a horizontal bar chart, what does the y-axis represent?
AValues
BCategories
CTime
DFrequency
✗ Incorrect
The y-axis shows the categories in a horizontal bar chart.
How do you set the thickness of bars in plt.barh()?
Alength
Bwidth
Csize
Dheight
✗ Incorrect
The height parameter controls the thickness of horizontal bars.
Which parameter changes the color of bars in plt.barh()?
Afill
Bstyle
Ccolor
Dshade
✗ Incorrect
Use the color parameter to set bar colors.
What does plt.xlabel() do in a horizontal bar chart?
ASets the label for the x-axis
BSets the chart title
CSets the label for the y-axis
DSets the legend text
✗ Incorrect
plt.xlabel() sets the label for the x-axis, which shows values in a horizontal bar chart.
Explain how to create a simple horizontal bar chart using matplotlib's plt.barh function.
Think about what data goes on each axis and how to label the chart.
You got /4 concepts.
Describe how to customize the appearance of a horizontal bar chart, including bar thickness and color.
Focus on parameters inside plt.barh and labeling functions.
You got /3 concepts.
Practice
(1/5)
1. What does the plt.barh() function in matplotlib do?
easy
A. Generates a scatter plot with dots
B. Creates a vertical bar chart with bars extending bottom to top
C. Creates a horizontal bar chart with bars extending left to right
D. Plots a line graph connecting data points
Solution
Step 1: Understand the function purpose
plt.barh() is designed to create horizontal bar charts, where bars extend from left to right.
Step 2: Compare with other plot types
Vertical bar charts use plt.bar(), line graphs use plt.plot(), and scatter plots use plt.scatter().
Final Answer:
Creates a horizontal bar chart with bars extending left to right -> Option C
Quick Check:
Horizontal bar chart = plt.barh() [OK]
Hint: Remember 'h' in barh means horizontal bars [OK]
Common Mistakes:
Confusing plt.barh() with plt.bar()
Thinking plt.barh() creates vertical bars
Mixing bar charts with line or scatter plots
2. Which of the following is the correct syntax to create a horizontal bar chart with categories ['A', 'B', 'C'] and values [5, 7, 3] using matplotlib?
easy
A. plt.barh(['A', 'B', 'C'], [5, 7, 3])
B. plt.bar([5, 7, 3], ['A', 'B', 'C'])
C. plt.barh([5, 7, 3], ['A', 'B', 'C'])
D. plt.plot(['A', 'B', 'C'], [5, 7, 3])
Solution
Step 1: Identify correct function and argument order
plt.barh() takes categories as the first argument (y-axis labels) and values as the second argument (bar lengths).
B. Arguments are reversed; categories should be first, values second
C. Missing plt.show() call
D. Color argument is required for plt.barh()
Solution
Step 1: Check argument order in plt.barh()
The first argument should be categories (labels), the second argument should be values (bar lengths).
Step 2: Identify the mistake in the code
The code passes values first and labels second, which reverses the expected order and causes incorrect plotting.
Final Answer:
Arguments are reversed; categories should be first, values second -> Option B
Quick Check:
plt.barh(categories, values) order matters [OK]
Hint: Categories first, values second in plt.barh() [OK]
Common Mistakes:
Swapping the order of categories and values
Forgetting plt.show() (though present here)
Thinking color argument is mandatory
5. You have a dataset with categories ['Jan', 'Feb', 'Mar'] and sales [200, 150, 300]. You want to create a horizontal bar chart with red bars, bar height of 0.5, and display the sales values on the bars. Which code snippet correctly achieves this?
hard
A. plt.barh(['Jan', 'Feb', 'Mar'], [200, 150, 300], color='red', height=0.5)
for i, v in enumerate([200, 150, 300]):
plt.text(v + 5, i, str(v))
plt.show()
B. plt.barh([200, 150, 300], ['Jan', 'Feb', 'Mar'], color='red', height=0.5)
plt.show()
C. plt.bar(['Jan', 'Feb', 'Mar'], [200, 150, 300], color='red', height=0.5)
plt.show()
D. plt.barh(['Jan', 'Feb', 'Mar'], [200, 150, 300], color='red', width=0.5)
plt.show()
Solution
Step 1: Use plt.barh() with correct arguments
Categories come first, then values. Set color='red' and height=0.5 to adjust bar thickness.
Step 2: Add sales values as text labels on bars
Use a for loop with plt.text() to place sales numbers slightly right of each bar end.
plt.barh(['Jan', 'Feb', 'Mar'], [200, 150, 300], color='red', height=0.5)
for i, v in enumerate([200, 150, 300]):
plt.text(v + 5, i, str(v))
plt.show() -> Option A