Bird
Raised Fist0
Matplotlibdata~20 mins

Ranking charts in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of a ranking chart in matplotlib?
easy
A. To display items ordered by their values from highest to lowest
B. To show random data points without any order
C. To plot data only on the x-axis without y-axis labels
D. To create 3D surface plots

Solution

  1. Step 1: Understand ranking chart purpose

    Ranking charts are designed to show items sorted by their values, usually from highest to lowest.
  2. Step 2: Compare options with definition

    Only To display items ordered by their values from highest to lowest correctly describes this purpose, while others describe unrelated chart types or features.
  3. Final Answer:

    To display items ordered by their values from highest to lowest -> Option A
  4. Quick Check:

    Ranking chart = ordered display [OK]
Hint: Ranking charts always sort data before plotting [OK]
Common Mistakes:
  • Thinking ranking charts show unsorted data
  • Confusing ranking charts with scatter plots
  • Assuming ranking charts are 3D plots
2. Which of the following matplotlib code snippets correctly sorts data for a ranking chart?
easy
A. data_sorted = data.sort_values(ascending=False)
B. data_sorted = data.random_shuffle()
C. data_sorted = data.sort_index()
D. data_sorted = data.dropna()

Solution

  1. Step 1: Identify sorting method for ranking

    Ranking charts require sorting values in descending order to rank from highest to lowest.
  2. Step 2: Evaluate each option

    data_sorted = data.sort_values(ascending=False) uses sort_values(ascending=False) which sorts data correctly. Others either shuffle, sort by index, or drop missing values, which are unrelated.
  3. Final Answer:

    data_sorted = data.sort_values(ascending=False) -> Option A
  4. Quick Check:

    Sort values descending = correct sorting [OK]
Hint: Use sort_values(ascending=False) to rank highest first [OK]
Common Mistakes:
  • Using sort_index instead of sort_values
  • Shuffling data randomly before plotting
  • Dropping data instead of sorting
3. What will be the output of this code snippet?
import matplotlib.pyplot as plt
values = [50, 20, 30]
labels = ['A', 'B', 'C']
plt.barh(labels, values)
plt.gca().invert_yaxis()
plt.show()
medium
A. A horizontal bar chart with 'C' at the top and 'A' at the bottom
B. A vertical bar chart with bars labeled A, B, C
C. A horizontal bar chart with 'A' at the top and 'C' at the bottom
D. An error because invert_yaxis() is invalid here

Solution

  1. Step 1: Understand horizontal bar chart with invert_yaxis()

    The barh function plots horizontal bars with labels on y-axis. By default, y-axis starts from bottom.
  2. Step 2: Effect of invert_yaxis()

    Calling invert_yaxis() flips the y-axis so the first label 'A' appears at the top, making ranking easier to read.
  3. Final Answer:

    A horizontal bar chart with 'A' at the top and 'C' at the bottom -> Option C
  4. Quick Check:

    invert_yaxis flips labels top-down [OK]
Hint: invert_yaxis() flips bars so top label is first [OK]
Common Mistakes:
  • Thinking invert_yaxis() causes error
  • Confusing horizontal with vertical bars
  • Assuming labels order stays bottom-up
4. Identify the error in this ranking chart code:
import matplotlib.pyplot as plt
values = [10, 40, 30]
labels = ['X', 'Y', 'Z']
plt.barh(labels, values)
plt.show()
medium
A. plt.show() is missing
B. The bars are not sorted, so ranking is incorrect
C. barh() cannot plot horizontal bars
D. The labels list is missing one label

Solution

  1. Step 1: Check if data is sorted for ranking

    The values are [10, 40, 30] but not sorted. Ranking charts require sorted data to show correct order.
  2. Step 2: Confirm other parts are correct

    Labels match values count, barh is valid, and plt.show() is present. So only sorting is missing.
  3. Final Answer:

    The bars are not sorted, so ranking is incorrect -> Option B
  4. Quick Check:

    Ranking needs sorted data [OK]
Hint: Always sort values before plotting ranking charts [OK]
Common Mistakes:
  • Ignoring sorting before plotting
  • Assuming barh() plots vertical bars
  • Forgetting plt.show()
5. You have a dictionary of sales data:
sales = {'Store A': 300, 'Store B': 450, 'Store C': 200, 'Store D': 450}

How can you create a ranking chart that correctly shows stores ranked by sales, with ties handled by alphabetical order, using matplotlib?
hard
A. Sort by sales ascending, then plot horizontal bars without inverting y-axis
B. Plot bars directly without sorting, then invert y-axis
C. Sort only by store name ascending, then plot vertical bars
D. Sort by sales descending, then by store name ascending, then plot horizontal bars with inverted y-axis

Solution

  1. Step 1: Sort data by sales descending and store name ascending

    To handle ties, first sort by sales descending, then by store name ascending to break ties alphabetically.
  2. Step 2: Plot horizontal bars and invert y-axis for ranking

    Plot sorted data with barh and call invert_yaxis() to show highest rank at top.
  3. Final Answer:

    Sort by sales descending, then by store name ascending, then plot horizontal bars with inverted y-axis -> Option D
  4. Quick Check:

    Sort by value desc + name asc + invert_yaxis = ranking [OK]
Hint: Sort by value desc and name asc, then invert y-axis [OK]
Common Mistakes:
  • Not sorting by store name to break ties
  • Plotting without sorting
  • Using vertical bars without ranking logic