0
0
Matplotlibdata~10 mins

Horizontal bar chart with plt.barh in Matplotlib - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a horizontal bar chart using plt.barh.

Matplotlib
import matplotlib.pyplot as plt

categories = ['A', 'B', 'C']
values = [10, 15, 7]

plt.[1](categories, values)
plt.show()
Drag options to blanks, or click blank then click option'
Abarh
Bbar
Cplot
Dscatter
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.bar instead of plt.barh creates vertical bars.
Using plt.plot or plt.scatter will not create bar charts.
2fill in blank
medium

Complete the code to label the x-axis of the horizontal bar chart.

Matplotlib
import matplotlib.pyplot as plt

categories = ['X', 'Y', 'Z']
values = [5, 8, 12]

plt.barh(categories, values)
plt.[1]('Value Count')
plt.show()
Drag options to blanks, or click blank then click option'
Aylabel
Btitle
Clegend
Dxlabel
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.ylabel instead of plt.xlabel for the x-axis label.
Using plt.title or plt.legend which do not label axes.
3fill in blank
hard

Fix the error in the code to correctly display the horizontal bar chart with custom colors.

Matplotlib
import matplotlib.pyplot as plt

categories = ['Red', 'Green', 'Blue']
values = [3, 6, 9]
colors = ['red', 'green', 'blue']

plt.barh(categories, values, color=[1])
plt.show()
Drag options to blanks, or click blank then click option'
Acolor
B'colors'
Ccolors
D['colors']
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the string 'colors' instead of the variable colors.
Passing a single string instead of a list of colors.
4fill in blank
hard

Fill both blanks to create a horizontal bar chart with categories sorted by values in descending order.

Matplotlib
import matplotlib.pyplot as plt

categories = ['Cat1', 'Cat2', 'Cat3']
values = [20, 10, 30]

sorted_pairs = sorted(zip(values, categories), key=[1], reverse=[2])
sorted_values, sorted_categories = zip(*sorted_pairs)

plt.barh(sorted_categories, sorted_values)
plt.show()
Drag options to blanks, or click blank then click option'
Alambda x: x[0]
Blambda x: x[1]
CFalse
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by category instead of value.
Setting reverse to False which sorts ascending.
5fill in blank
hard

Fill all three blanks to create a horizontal bar chart with labels showing the value at the end of each bar.

Matplotlib
import matplotlib.pyplot as plt

categories = ['A', 'B', 'C']
values = [7, 12, 5]

bars = plt.barh(categories, values)

for bar in bars:
    width = bar.[1]()
    plt.text(width + [2], bar.[3](), str(int(width)), va='center')

plt.show()
Drag options to blanks, or click blank then click option'
Aget_width
B0.1
Cget_y
Dget_height
Attempts:
3 left
💡 Hint
Common Mistakes
Using get_height() which is for vertical bars.
Not adding an offset, causing labels to overlap bars.
Using get_x() instead of get_y() for vertical position.