📖 Scenario: You are analyzing sales data for a small store. You want to create a simple line chart to show how sales changed over a week.
🎯 Goal: Create a line chart using matplotlib with a title and labels for the x-axis and y-axis to clearly explain the chart.
📋 What You'll Learn
Create a list called days with the days of the week from Monday to Sunday.
Create a list called sales with the sales numbers for each day: 150, 200, 170, 220, 180, 190, 210.
Use matplotlib.pyplot to plot the sales data as a line chart.
Add a chart title using plt.title() with the text 'Weekly Sales'.
Add an x-axis label using plt.xlabel() with the text 'Day of the Week'.
Add a y-axis label using plt.ylabel() with the text 'Sales (units)'.
Display the chart using plt.show().
💡 Why This Matters
🌍 Real World
Adding titles and axis labels helps anyone looking at your charts understand what the data means quickly.
💼 Career
Data scientists and analysts always label their charts clearly to communicate insights effectively to others.
Progress0 / 4 steps
1
Create the sales data lists
Create a list called days with these exact values: 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'. Then create a list called sales with these exact values: 150, 200, 170, 220, 180, 190, 210.
Matplotlib
Hint
Use square brackets to create lists. Separate items with commas.
2
Import matplotlib and prepare to plot
Import matplotlib.pyplot as plt. This will let you create charts easily.
Matplotlib
Hint
Use the import statement to bring in matplotlib.pyplot and rename it as plt.
3
Plot the sales data with title and axis labels
Use plt.plot(days, sales) to create a line chart. Then add a title with plt.title('Weekly Sales'). Add an x-axis label with plt.xlabel('Day of the Week') and a y-axis label with plt.ylabel('Sales (units)').
Matplotlib
Hint
Use plt.plot() to draw the line. Use plt.title(), plt.xlabel(), and plt.ylabel() to add text.
4
Display the chart
Use plt.show() to display the chart with the title and axis labels.
Matplotlib
Hint
Call plt.show() to see the chart window pop up.
Practice
(1/5)
1. Which matplotlib function is used to add a title to a plot?
easy
A. plt.axis()
B. plt.label()
C. plt.legend()
D. plt.title()
Solution
Step 1: Understand the purpose of plt.title()
This function adds a title text at the top of the plot, describing what the plot shows.
Step 2: Compare with other functions
plt.label() does not exist, plt.legend() adds a legend, and plt.axis() controls axis limits, so they don't add a title.
Final Answer:
plt.title() -> Option D
Quick Check:
Title function = plt.title() [OK]
Hint: Title is always set with plt.title() [OK]
Common Mistakes:
Using plt.label() instead of plt.title()
Confusing legend with title
Trying plt.axis() for title
2. Which of the following is the correct syntax to label the x-axis as 'Time (seconds)' in matplotlib?
easy
A. plt.xlabel('Time (seconds)')
B. plt.xaxis('Time (seconds)')
C. plt.label('x', 'Time (seconds)')
D. plt.axislabel('Time (seconds)')
Solution
Step 1: Recall the function to label x-axis
The correct function is plt.xlabel() which takes the label text as argument.
Step 2: Check other options for syntax errors
plt.xaxis(), plt.label(), and plt.axislabel() are not valid matplotlib functions for axis labeling.
Final Answer:
plt.xlabel('Time (seconds)') -> Option A
Quick Check:
X-axis label = plt.xlabel() [OK]
Hint: Use plt.xlabel() for x-axis label text [OK]
Common Mistakes:
Using plt.xaxis() which doesn't exist
Trying plt.label() for axis
Confusing axis limits with labels
3. What will be the output of the following code?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.title('Sales Over Time')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.show()
medium
A. A plot with title but no axis labels
B. A plot with no title and no axis labels
C. A plot with title 'Sales Over Time', x-axis labeled 'Month', y-axis labeled 'Sales'
D. A plot with axis labels but no title
Solution
Step 1: Analyze the code lines for title and labels
The code uses plt.title('Sales Over Time') to add a title, plt.xlabel('Month') for x-axis label, and plt.ylabel('Sales') for y-axis label.
Step 2: Understand the plot display
The plot will show the line graph with all these labels visible as specified.
Final Answer:
A plot with title 'Sales Over Time', x-axis labeled 'Month', y-axis labeled 'Sales' -> Option C
Quick Check:
Title and axis labels set correctly [OK]
Hint: Check all label functions called before plt.show() [OK]
Common Mistakes:
Forgetting plt.title() call
Mixing up xlabel and ylabel
Not calling plt.show() to display
4. Identify the error in this code snippet that tries to add a title and axis labels:
A. Using assignment operator '=' instead of function call for plt.title
B. Missing plt.show() call
C. Incorrect order of xlabel and ylabel calls
D. plt.plot() arguments are invalid
Solution
Step 1: Check how title is set
The code uses plt.title = 'My Plot' which assigns a string to the function name, overwriting it instead of calling it.
Step 2: Understand correct usage
The correct way is to call the function: plt.title('My Plot') to set the title.
Final Answer:
Using assignment operator '=' instead of function call for plt.title -> Option A
Quick Check:
Title must be set by calling plt.title() [OK]
Hint: Use parentheses () to call plt.title() function [OK]
Common Mistakes:
Assigning string to plt.title instead of calling
Forgetting plt.show()
Swapping xlabel and ylabel calls
5. You want to create a plot with the title 'Temperature Trends', x-axis labeled 'Day', and y-axis labeled 'Temperature (°C)'. Which code snippet correctly achieves this?