📖 Scenario: You are analyzing sales data for a small store. You want to create a simple bar chart to show the sales of different products. To make the chart clear, you will add labels to the axes, a title, and a legend.
🎯 Goal: Create a bar chart using Python's matplotlib library that shows product sales. Add an x-axis label, a y-axis label, a chart title, and a legend to explain the colors.
📋 What You'll Learn
Create a dictionary called sales with product names as keys and sales numbers as values.
Create a variable called colors that assigns a color to each product.
Use matplotlib to plot a bar chart with the sales data.
Add an x-axis label called Product.
Add a y-axis label called Sales.
Add a chart title called Sales by Product.
Add a legend that shows which color belongs to which product.
💡 Why This Matters
🌍 Real World
Adding labels, titles, and legends to charts helps people understand data clearly. This is important when sharing reports or presentations.
💼 Career
Data scientists and analysts often create charts to explain their findings. Knowing how to label charts properly is a key skill for clear communication.
Progress0 / 4 steps
1
Create the sales data dictionary
Create a dictionary called sales with these exact entries: 'Apples': 30, 'Bananas': 45, 'Cherries': 25, 'Dates': 40.
Data Analysis Python
Hint
Use curly braces {} to create the dictionary with the exact keys and values.
2
Create the colors list
Create a list called colors with these exact values in order: 'red', 'yellow', 'pink', 'brown'.
Data Analysis Python
Hint
Use square brackets [] to create the list with the exact colors in the order of the products.
3
Plot the bar chart with labels, title, and legend
Import matplotlib.pyplot as plt. Use plt.bar() to plot the sales data with product names on the x-axis and sales numbers on the y-axis. Use the colors list for the bar colors. Add an x-axis label 'Product', a y-axis label 'Sales', and a title 'Sales by Product'. Add a legend that shows each product with its color.
Data Analysis Python
Hint
Use plt.bar() with color=colors. Use plt.xlabel(), plt.ylabel(), and plt.title() to add labels and title. Use plt.legend() with the bars and product names.
4
Display the chart
Use plt.show() to display the bar chart with labels, title, and legend.
Data Analysis Python
Hint
Call plt.show() to display the chart window.
Practice
(1/5)
1. What is the main purpose of adding a title to a chart in data visualization?
easy
A. To give the chart a clear heading that explains what it shows
B. To label the x-axis with numbers
C. To add colors to the chart
D. To describe the data source
Solution
Step 1: Understand the role of a title in charts
A title provides a clear heading that tells the viewer what the chart is about.
Step 2: Compare options with the title's purpose
Only To give the chart a clear heading that explains what it shows correctly describes the purpose of a title as a clear heading.
Final Answer:
To give the chart a clear heading that explains what it shows -> Option A
Quick Check:
Title = Clear heading [OK]
Hint: Titles are headings that explain the whole chart [OK]
Common Mistakes:
Confusing titles with axis labels
Thinking titles add colors
Mixing titles with legends
2. Which of the following is the correct way to add a legend to a Matplotlib plot in Python?
easy
A. plt.xlabel('Legend')
B. plt.title('Legend')
C. plt.legend()
D. plt.legend('show')
Solution
Step 1: Recall the Matplotlib function for legends
The function plt.legend() adds a legend to the plot.
Step 2: Check other options for correctness
Options A and B set x-axis label and title, not legend. plt.legend('show') uses incorrect syntax.
Final Answer:
plt.legend() -> Option C
Quick Check:
Legend function = plt.legend() [OK]
Hint: Use plt.legend() to show legends in Matplotlib [OK]
Both plot calls lack the label argument, so legend has no text to display.
Step 2: Verify legend and show usage
Calling plt.legend() without labels results in an empty legend. plt.show() is present and plt.title() is not required.
Final Answer:
Missing labels in plot() calls for legend -> Option B
Quick Check:
Labels needed for legend [OK]
Hint: Add label='name' in plot() to show legend [OK]
Common Mistakes:
Calling plt.legend() before plot()
Thinking plt.title() affects legend
Forgetting to call plt.show()
5. You have a DataFrame with sales data for two products over 3 months. You want to plot both lines on the same chart with a title, axis labels, and a legend. Which code snippet correctly adds all these elements?
B. Swaps x and y axes and labels, causing incorrect plot
C. Missing legend call, so no legend shown
D. Correctly plots both products with labels, title, axis labels, and legend
Solution
Step 1: Check axis data and labels in each option
Correctly plots both products with labels, title, axis labels, and legend uses Month on x-axis and sales on y-axis correctly with labels and title.
Step 2: Verify legend and title presence
Correctly plots both products with labels, title, axis labels, and legend includes label in plot calls and calls plt.legend() and plt.title(). Others miss legend or swap axes.
Final Answer:
Correctly plots both products with labels, title, axis labels, and legend -> Option D
Quick Check:
Labels + legend + title = Correctly plots both products with labels, title, axis labels, and legend [OK]
Hint: Match x, y data and add labels + legend + title [OK]
Common Mistakes:
Swapping x and y axes
Forgetting plt.legend() call
Missing title or axis labels
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.