0
0
Matplotlibdata~20 mins

Horizontal bar chart with plt.barh in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Horizontal Bar Chart Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of plt.barh with given data
What will be the output of this code snippet that creates a horizontal bar chart?
Matplotlib
import matplotlib.pyplot as plt

categories = ['A', 'B', 'C']
values = [3, 7, 5]
plt.barh(categories, values)
plt.show()
AA vertical bar chart with bars for A=3, B=7, C=5
BSyntaxError due to wrong function usage
CA horizontal bar chart with bars for A=7, B=3, C=5
DA horizontal bar chart with bars for A=3, B=7, C=5
Attempts:
2 left
💡 Hint
plt.barh creates horizontal bars where the first argument is the y-axis labels and the second is the bar lengths.
data_output
intermediate
1:30remaining
Number of bars in plt.barh chart
How many bars will be displayed by this code?
Matplotlib
import matplotlib.pyplot as plt

labels = ['X', 'Y', 'Z', 'W']
values = [10, 20, 15, 5]
plt.barh(labels, values)
plt.show()
A3
B4
C5
D0
Attempts:
2 left
💡 Hint
Count the number of labels given to plt.barh.
🔧 Debug
advanced
2:00remaining
Identify the error in horizontal bar chart code
What error will this code produce?
Matplotlib
import matplotlib.pyplot as plt

labels = ['Jan', 'Feb', 'Mar']
values = [5, 8]
plt.barh(labels, values)
plt.show()
AValueError: y and width must be the same size
BTypeError: unsupported operand type(s)
CNo error, chart displays correctly
DSyntaxError due to missing colon
Attempts:
2 left
💡 Hint
Check if the lengths of labels and values match.
visualization
advanced
1:30remaining
Effect of changing bar height in plt.barh
What visual effect does changing the 'height' parameter in plt.barh have?
Matplotlib
import matplotlib.pyplot as plt

labels = ['P', 'Q', 'R']
values = [4, 6, 8]
plt.barh(labels, values, height=0.1)
plt.show()
ABars change color
BBars become wider horizontally
CBars become thinner vertically
DBars disappear
Attempts:
2 left
💡 Hint
Height controls the thickness of horizontal bars.
🚀 Application
expert
3:00remaining
Create a horizontal bar chart with sorted values
Which code snippet correctly creates a horizontal bar chart with categories sorted by their values in ascending order?
A
import matplotlib.pyplot as plt
categories = ['A', 'B', 'C']
values = [5, 2, 7]
sorted_pairs = sorted(zip(values, categories))
sorted_values, sorted_categories = zip(*sorted_pairs)
plt.barh(sorted_categories, sorted_values)
plt.show()
B
import matplotlib.pyplot as plt
categories = ['A', 'B', 'C']
values = [5, 2, 7]
plt.barh(categories.sort(), values.sort())
plt.show()
C
import matplotlib.pyplot as plt
categories = ['A', 'B', 'C']
values = [5, 2, 7]
plt.barh(sorted(categories), sorted(values))
plt.show()
D
import matplotlib.pyplot as plt
categories = ['A', 'B', 'C']
values = [5, 2, 7]
plt.barh(categories, values)
plt.show()
Attempts:
2 left
💡 Hint
You need to sort values and categories together to keep their pairs aligned.