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
Ranking Charts with Matplotlib
📖 Scenario: You work for a small company that wants to see how their products rank in sales compared to each other. They want a simple chart that shows the ranking of each product by sales numbers.
🎯 Goal: You will create a ranking chart using matplotlib to visualize product sales rankings clearly.
📋 What You'll Learn
Create a dictionary with product names and their sales numbers
Create a variable to hold the number of top products to show
Use sorting and slicing to get the top products by sales
Create a horizontal bar chart showing product names and their sales
Display the chart with clear labels and title
💡 Why This Matters
🌍 Real World
Ranking charts help businesses quickly see which products or items perform best, making it easier to focus on popular items.
💼 Career
Data analysts and business intelligence professionals often create ranking charts to present sales or performance data clearly to stakeholders.
Progress0 / 4 steps
1
Create the sales data dictionary
Create a dictionary called product_sales with these exact entries: 'Laptop': 120, 'Smartphone': 300, 'Tablet': 150, 'Headphones': 90, 'Smartwatch': 80.
Matplotlib
Hint
Use curly braces {} to create a dictionary with keys as product names and values as sales numbers.
2
Set the number of top products to display
Create a variable called top_n and set it to 3 to show the top 3 products.
Matplotlib
Hint
Just assign the number 3 to the variable top_n.
3
Get the top products by sales
Create a list called top_products that contains the top top_n products sorted by sales in descending order. Use sorted() with product_sales.items() and a lambda function to sort by sales.
Matplotlib
Hint
Use sorted() with key=lambda x: x[1] and reverse=True to sort by sales descending, then slice with [:top_n].
4
Plot the ranking chart and display it
Use matplotlib.pyplot to create a horizontal bar chart of the top products. Extract product names and sales from top_products. Use plt.barh() to plot, set the title to 'Top 3 Product Sales', label the x-axis as 'Sales', and show the plot with plt.show().
Matplotlib
Hint
Remember to reverse the lists to show the highest sales at the top in the horizontal bar chart.
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]