What if you could turn boring numbers into a picture everyone understands in seconds?
Why Horizontal bar chart with plt.barh in Matplotlib? - Purpose & Use Cases
Imagine you have a list of your favorite fruits and how many you ate last week. You want to show this to your friends clearly. Writing all the numbers in a list or table is boring and hard to understand quickly.
Trying to explain data with just numbers or text takes time and can confuse people. It's easy to make mistakes counting or comparing. You might spend too long making a chart by hand, drawing bars on paper, or using complicated tools.
Using a horizontal bar chart with plt.barh in matplotlib lets you quickly turn your numbers into a clear picture. Bars show the size of each fruit count side by side, making it easy to compare at a glance.
fruits = ['Apple', 'Banana', 'Cherry'] counts = [5, 3, 7] for i in range(len(fruits)): print(f'{fruits[i]}: ' + '*' * counts[i])
import matplotlib.pyplot as plt plt.barh(['Apple', 'Banana', 'Cherry'], [5, 3, 7]) plt.show()
You can instantly see which items are bigger or smaller, making your data easy to understand and share with anyone.
A teacher shows students' test scores with horizontal bars so the class can quickly see who did well and who needs help.
Manual lists are slow and hard to read.
plt.barh creates clear horizontal bar charts fast.
Visual charts help compare data easily and share insights.
Practice
plt.barh() function in matplotlib do?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 useplt.bar(), line graphs useplt.plot(), and scatter plots useplt.scatter().Final Answer:
Creates a horizontal bar chart with bars extending left to right -> Option CQuick Check:
Horizontal bar chart = plt.barh() [OK]
- Confusing plt.barh() with plt.bar()
- Thinking plt.barh() creates vertical bars
- Mixing bar charts with line or scatter plots
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).Step 2: Check each option
plt.barh(['A', 'B', 'C'], [5, 7, 3]) correctly usesplt.barh(['A', 'B', 'C'], [5, 7, 3]). plt.bar([5, 7, 3], ['A', 'B', 'C']) uses vertical bars and reverses arguments. plt.barh([5, 7, 3], ['A', 'B', 'C']) reverses arguments for horizontal bars. plt.plot(['A', 'B', 'C'], [5, 7, 3]) plots a line graph.Final Answer:
plt.barh(['A', 'B', 'C'], [5, 7, 3]) -> Option AQuick Check:
Categories first, values second in plt.barh() [OK]
- Swapping categories and values in arguments
- Using plt.bar() instead of plt.barh() for horizontal bars
- Using plt.plot() instead of bar chart functions
import matplotlib.pyplot as plt categories = ['X', 'Y', 'Z'] values = [10, 5, 8] plt.barh(categories, values, color='green') plt.show()
Solution
Step 1: Analyze the function and parameters
The code usesplt.barh()with categories on y-axis and values as bar lengths, colored green.Step 2: Understand the plot output
This produces a horizontal bar chart with green bars labeled X, Y, Z with lengths 10, 5, and 8 respectively.Final Answer:
A horizontal bar chart with green bars for categories X, Y, Z -> Option DQuick Check:
plt.barh() + color='green' = horizontal green bars [OK]
- Thinking plt.barh() creates vertical bars
- Assuming color must be a list
- Confusing bar chart with line plot
import matplotlib.pyplot as plt labels = ['A', 'B', 'C'] values = [4, 6, 8] plt.barh(values, labels) plt.show()
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 BQuick Check:
plt.barh(categories, values) order matters [OK]
- Swapping the order of categories and values
- Forgetting plt.show() (though present here)
- Thinking color argument is mandatory
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 withplt.text()to place sales numbers slightly right of each bar end.Step 3: Verify other options
plt.barh([200, 150, 300], ['Jan', 'Feb', 'Mar'], color='red', height=0.5) plt.show() reverses arguments, plt.bar(['Jan', 'Feb', 'Mar'], [200, 150, 300], color='red', height=0.5) plt.show() uses vertical bars, plt.barh(['Jan', 'Feb', 'Mar'], [200, 150, 300], color='red', width=0.5) plt.show() uses 'width' instead of 'height' which is invalid for barh.Final Answer:
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 AQuick Check:
plt.barh(categories, values, height=0.5) + plt.text() labels [OK]
- Swapping categories and values
- Using width instead of height for bar thickness
- Not adding text labels or placing them incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
