Introduction
Ranking charts help us see the order of items based on their values. They make it easy to compare and find the top or bottom items.
Jump into concepts and practice - no test required
Ranking charts help us see the order of items based on their values. They make it easy to compare and find the top or bottom items.
import matplotlib.pyplot as plt # Data: items and their values items = ['A', 'B', 'C', 'D'] values = [10, 30, 20, 40] # Sort data by values descending sorted_items = [x for _, x in sorted(zip(values, items), reverse=True)] sorted_values = sorted(values, reverse=True) # Create bar chart plt.bar(sorted_items, sorted_values) plt.title('Ranking Chart') plt.xlabel('Items') plt.ylabel('Values') plt.show()
We sort the data so the highest values appear first.
Bar charts are a simple way to show rankings visually.
items = ['Apple', 'Banana', 'Cherry'] values = [50, 20, 30] # Sort descending sorted_items = [x for _, x in sorted(zip(values, items), reverse=True)] sorted_values = sorted(values, reverse=True) plt.bar(sorted_items, sorted_values) plt.show()
import matplotlib.pyplot as plt names = ['John', 'Anna', 'Mike'] scores = [88, 92, 85] # Sort by scores descending sorted_names = [n for _, n in sorted(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()
This program shows a ranking chart of the top 5 countries by population using a vertical bar chart.
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 in sorted(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()
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.
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.
matplotlib?matplotlib code snippets correctly sorts data for a ranking chart?sort_values(ascending=False) which sorts data correctly. Others either shuffle, sort by index, or drop missing values, which are unrelated.import matplotlib.pyplot as plt values = [50, 20, 30] labels = ['A', 'B', 'C'] plt.barh(labels, values) plt.gca().invert_yaxis() plt.show()
barh function plots horizontal bars with labels on y-axis. By default, y-axis starts from bottom.invert_yaxis()invert_yaxis() flips the y-axis so the first label 'A' appears at the top, making ranking easier to read.import matplotlib.pyplot as plt values = [10, 40, 30] labels = ['X', 'Y', 'Z'] plt.barh(labels, values) plt.show()
barh is valid, and plt.show() is present. So only sorting is missing.sales = {'Store A': 300, 'Store B': 450, 'Store C': 200, 'Store D': 450}matplotlib?barh and call invert_yaxis() to show highest rank at top.