0
0
Matplotlibdata~30 mins

Ranking charts in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Remember to reverse the lists to show the highest sales at the top in the horizontal bar chart.