This example uses a horizontal bar chart to rank students by scores.
Matplotlib
import matplotlib.pyplot as plt
names = ['John', 'Anna', 'Mike']
scores = [88, 92, 85]
# Sort by scores descending
sorted_names = [n for _, n insorted(zip(scores, names), reverse=True)]
sorted_scores = sorted(scores, reverse=True)
plt.barh(sorted_names, sorted_scores)
plt.title('Student Scores Ranking')
plt.xlabel('Score')
plt.show()
Sample Program
This program shows a ranking chart of the top 5 countries by population using a vertical bar chart.
Matplotlib
import matplotlib.pyplot as plt
# Sample data: countries and their populations (millions)
countries = ['USA', 'India', 'China', 'Brazil', 'Nigeria']
populations = [331, 1380, 1441, 213, 206]
# Sort countries by population descending
sorted_countries = [c for _, c insorted(zip(populations, countries), reverse=True)]
sorted_populations = sorted(populations, reverse=True)
# Create ranking bar chart
plt.bar(sorted_countries, sorted_populations, color='skyblue')
plt.title('Top 5 Countries by Population (millions)')
plt.xlabel('Country')
plt.ylabel('Population (millions)')
plt.show()
OutputSuccess
Important Notes
Always sort your data before plotting to show correct rankings.
Use labels and titles to make the chart easy to understand.
Colors can help make the chart more attractive and clear.
Summary
Ranking charts show items ordered by their values.
Sorting data is key to making ranking charts.
Bar charts are a simple and effective way to display rankings.
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
Step 1: Understand ranking chart purpose
Ranking charts are designed to show items sorted by their values, usually from highest to lowest.
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.
Final Answer:
To display items ordered by their values from highest to lowest -> Option A
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
Step 1: Identify sorting method for ranking
Ranking charts require sorting values in descending order to rank from highest to lowest.
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.
Final Answer:
data_sorted = data.sort_values(ascending=False) -> Option A
Quick Check:
Sort values descending = correct sorting [OK]
Hint: Use sort_values(ascending=False) to rank highest first [OK]