0
0
Matplotlibdata~15 mins

Horizontal bar chart with plt.barh in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Horizontal bar chart with plt.barh
📖 Scenario: You work in a small bakery and want to show how many cakes of each type you sold last week. You want to create a simple horizontal bar chart to share with your team.
🎯 Goal: Create a horizontal bar chart using plt.barh to display the number of cakes sold for each cake type.
📋 What You'll Learn
Create a list called cake_types with the cake names exactly as: 'Chocolate', 'Vanilla', 'Strawberry', 'Lemon', 'Carrot'
Create a list called cakes_sold with the numbers exactly as: 50, 40, 30, 20, 10
Use plt.barh to create a horizontal bar chart with cake_types on the y-axis and cakes_sold on the x-axis
Add labels for the x-axis as 'Number of Cakes Sold' and y-axis as 'Cake Type'
Display the chart using plt.show()
💡 Why This Matters
🌍 Real World
Horizontal bar charts are useful to compare categories when category names are long or when you want to display data horizontally for better readability.
💼 Career
Data analysts and scientists often use horizontal bar charts to present categorical data clearly in reports and presentations.
Progress0 / 4 steps
1
Create the data lists
Create a list called cake_types with these exact values: 'Chocolate', 'Vanilla', 'Strawberry', 'Lemon', 'Carrot'. Also create a list called cakes_sold with these exact numbers: 50, 40, 30, 20, 10.
Matplotlib
Need a hint?

Use square brackets [] to create lists. Separate items with commas.

2
Import matplotlib and set up the plot
Import matplotlib.pyplot as plt. This will allow you to create the chart.
Matplotlib
Need a hint?

Use import matplotlib.pyplot as plt to import the plotting library.

3
Create the horizontal bar chart
Use plt.barh with cake_types as the y-values and cakes_sold as the x-values to create a horizontal bar chart. Then add x-axis label 'Number of Cakes Sold' and y-axis label 'Cake Type'.
Matplotlib
Need a hint?

Use plt.barh(y, x) where y is the list of categories and x is the list of values.

4
Display the chart
Use plt.show() to display the horizontal bar chart.
Matplotlib
Need a hint?

Call plt.show() to open the chart window.