0
0
Matplotlibdata~15 mins

Inverted axes in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Inverted axes
📖 Scenario: You are analyzing sales data for a small store. You want to visualize the sales over a week using a bar chart. To better understand the data, you want to invert the axes so that the days appear on the vertical axis and the sales values on the horizontal axis.
🎯 Goal: Create a bar chart using matplotlib with inverted axes. The days of the week should be on the vertical axis, and the sales numbers on the horizontal axis.
📋 What You'll Learn
Create a dictionary called sales with days of the week as keys and sales numbers as values.
Create a list called days containing the days of the week in order.
Create a list called values containing the sales numbers in the same order as days.
Use matplotlib.pyplot to create a bar chart with inverted axes.
Invert the axes so that days appear on the vertical axis and sales on the horizontal axis.
Display the plot.
💡 Why This Matters
🌍 Real World
Inverting axes is useful when you want to display categories on the vertical axis for better readability, such as days of the week or product names.
💼 Career
Data analysts and scientists often customize plots to make data easier to understand and communicate insights clearly to others.
Progress0 / 4 steps
1
Create the sales data dictionary
Create a dictionary called sales with these exact entries: 'Monday': 150, 'Tuesday': 200, 'Wednesday': 170, 'Thursday': 220, 'Friday': 180.
Matplotlib
Need a hint?

Use curly braces {} to create the dictionary with the exact keys and values.

2
Create lists for days and sales values
Create a list called days containing 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday' in this exact order. Then create a list called values containing the sales numbers from the sales dictionary in the same order as days.
Matplotlib
Need a hint?

Use a list comprehension to get the sales values in the order of the days list.

3
Create the bar chart with inverted axes
Import matplotlib.pyplot as plt. Use plt.barh(days, values) to create a horizontal bar chart with days on the vertical axis and values on the horizontal axis.
Matplotlib
Need a hint?

Use plt.barh() to create a horizontal bar chart which inverts the axes.

4
Display the plot
Use plt.show() to display the bar chart with inverted axes.
Matplotlib
Need a hint?

Call plt.show() to open the window with the plot.