0
0
Matplotlibdata~20 mins

Ranking charts in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ranking Chart Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Ranking Chart Code
What will be the output of this code snippet that creates a ranking chart using matplotlib?
Matplotlib
import matplotlib.pyplot as plt
import pandas as pd

scores = {'Alice': 88, 'Bob': 95, 'Charlie': 70, 'David': 85}
ranked = pd.Series(scores).rank(ascending=False, method='min')

plt.bar(ranked.index, ranked.values)
plt.gca().invert_yaxis()
plt.title('Ranking Chart')
plt.ylabel('Rank')
plt.show()
AA bar chart with names on x-axis and ranks on y-axis, ranks inverted so 1 is at top
BA line chart showing scores over names
CA bar chart with scores on x-axis and names on y-axis
DA scatter plot with scores vs ranks
Attempts:
2 left
💡 Hint
Look at how rank is calculated and how y-axis is inverted.
data_output
intermediate
1:30remaining
Number of Items in Ranked Data
Given this code that ranks data, how many items are in the resulting ranked Series?
Matplotlib
import pandas as pd

scores = {'Anna': 78, 'Ben': 82, 'Cara': 78, 'Derek': 90}
ranked = pd.Series(scores).rank(ascending=False, method='min')
A3
B1
C4
D2
Attempts:
2 left
💡 Hint
Count the keys in the original dictionary.
visualization
advanced
2:30remaining
Identify the Correct Ranking Chart Visualization
Which option shows the correct matplotlib code to create a horizontal ranking bar chart with names on y-axis and ranks on x-axis, with rank 1 at left?
A
plt.barh(ranked.index, ranked.values)
plt.xlabel('Rank')
plt.title('Horizontal Ranking Chart')
plt.show()
B
plt.bar(ranked.index, ranked.values)
plt.gca().invert_yaxis()
plt.ylabel('Rank')
plt.title('Ranking Chart')
plt.show()
C
plt.barh(ranked.values, ranked.index)
plt.gca().invert_xaxis()
plt.xlabel('Rank')
plt.title('Horizontal Ranking Chart')
plt.show()
D
plt.barh(ranked.index, ranked.values)
plt.gca().invert_xaxis()
plt.xlabel('Rank')
plt.title('Horizontal Ranking Chart')
plt.show()
Attempts:
2 left
💡 Hint
Horizontal bar charts use barh; invert x-axis to put rank 1 at left.
🔧 Debug
advanced
2:00remaining
Error in Ranking Chart Code
What error will this code produce when run?
Matplotlib
import matplotlib.pyplot as plt
import pandas as pd

scores = {'Eve': 92, 'Frank': 85}
ranked = pd.Series(scores).rank(ascending=False)

plt.bar(ranked.values, ranked.index)
plt.show()
ATypeError: unhashable type: 'numpy.float64'
BValueError: x and height must be the same size
CTypeError: 'float' object cannot be interpreted as an integer
DNo error, chart displays correctly
Attempts:
2 left
💡 Hint
Check the order of arguments in plt.bar(x, height).
🚀 Application
expert
3:00remaining
Create a Ranking Chart with Ties Correctly Displayed
You have scores with ties: {'Gina': 88, 'Hank': 88, 'Ivy': 75}. Which code correctly creates a ranking chart that assigns the same rank to ties and plots them with rank 1 at top?
A
import matplotlib.pyplot as plt
import pandas as pd
scores = {'Gina': 88, 'Hank': 88, 'Ivy': 75}
ranked = pd.Series(scores).rank(ascending=False, method='min')
plt.bar(ranked.index, ranked.values)
plt.gca().invert_yaxis()
plt.title('Ranking with Ties')
plt.ylabel('Rank')
plt.show()
B
import matplotlib.pyplot as plt
import pandas as pd
scores = {'Gina': 88, 'Hank': 88, 'Ivy': 75}
ranked = pd.Series(scores).rank(ascending=False, method='average')
plt.bar(ranked.index, ranked.values)
plt.gca().invert_yaxis()
plt.title('Ranking with Ties')
plt.ylabel('Rank')
plt.show()
C
import matplotlib.pyplot as plt
import pandas as pd
scores = {'Gina': 88, 'Hank': 88, 'Ivy': 75}
ranked = pd.Series(scores).rank(ascending=True, method='min')
plt.bar(ranked.index, ranked.values)
plt.gca().invert_yaxis()
plt.title('Ranking with Ties')
plt.ylabel('Rank')
plt.show()
D
import matplotlib.pyplot as plt
import pandas as pd
scores = {'Gina': 88, 'Hank': 88, 'Ivy': 75}
ranked = pd.Series(scores).rank(ascending=False, method='max')
plt.bar(ranked.index, ranked.values)
plt.gca().invert_yaxis()
plt.title('Ranking with Ties')
plt.ylabel('Rank')
plt.show()
Attempts:
2 left
💡 Hint
Use method='min' to assign the same lowest rank to ties and invert y-axis for rank 1 at top.