0
0
Matplotlibdata~10 mins

Ranking charts 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 simple bar chart showing rankings.

Matplotlib
import matplotlib.pyplot as plt

items = ['A', 'B', 'C', 'D']
scores = [4, 2, 3, 1]

plt.bar(items, [1])
plt.show()
Drag options to blanks, or click blank then click option'
Arange(4)
Bitems
C[1, 2, 3, 4]
Dscores
Attempts:
3 left
💡 Hint
Common Mistakes
Using the item names instead of scores for bar heights.
Passing a range object instead of a list of scores.
2fill in blank
medium

Complete the code to sort the items by their scores before plotting.

Matplotlib
items = ['A', 'B', 'C', 'D']
scores = [4, 2, 3, 1]

sorted_pairs = sorted(zip(items, scores), key=lambda x: x[[1]])
sorted_items, sorted_scores = zip(*sorted_pairs)

import matplotlib.pyplot as plt
plt.bar(sorted_items, sorted_scores)
plt.show()
Drag options to blanks, or click blank then click option'
A0
B1
C-1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by index 0 which is the item name, not the score.
Using an invalid index like 2 which does not exist.
3fill in blank
hard

Fix the error in the code to display the ranking chart with labels rotated.

Matplotlib
import matplotlib.pyplot as plt

items = ['A', 'B', 'C', 'D']
scores = [4, 2, 3, 1]

plt.bar(items, scores)
plt.xticks(rotation=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A45
Brotation=45
C'45deg'
D'45 degrees'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing rotation as a string causes an error.
Using an invalid keyword inside plt.xticks.
4fill in blank
hard

Fill both blanks to create a horizontal bar chart sorted by scores.

Matplotlib
import matplotlib.pyplot as plt

items = ['A', 'B', 'C', 'D']
scores = [4, 2, 3, 1]

sorted_pairs = sorted(zip(items, scores), key=lambda x: x[[1]])
sorted_items, sorted_scores = zip(*sorted_pairs)

plt.[2](sorted_items, sorted_scores)
plt.show()
Drag options to blanks, or click blank then click option'
A1
B0
Cbarh
Dbar
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by index 0 which is item name.
Using plt.bar instead of plt.barh for horizontal bars.
5fill in blank
hard

Fill all three blanks to create a ranking chart with colors based on score thresholds.

Matplotlib
import matplotlib.pyplot as plt

items = ['A', 'B', 'C', 'D']
scores = [4, 2, 3, 1]

colors = ['green' if score [1] 3 else 'red' for score in scores]

plt.bar(items, scores, color=[2])
plt.title('Ranking Chart')
plt.ylabel([3])
plt.show()
Drag options to blanks, or click blank then click option'
A>
Bcolors
C'Scores'
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' changes color logic.
Passing a string instead of the colors list to color.
Not labeling the y-axis properly.