📖 Scenario: You are working as a data analyst and want to create a simple line chart to visualize sales data over a week. You will use the matplotlib.pyplot interface to plot the data.
🎯 Goal: Build a simple line plot using matplotlib.pyplot to show sales numbers for each day of the week.
📋 What You'll Learn
Create a list of sales numbers for 7 days
Create a list of day names for the x-axis
Use matplotlib.pyplot to plot the sales data
Add labels for x-axis and y-axis
Display the plot
💡 Why This Matters
🌍 Real World
Line charts are common in business to track sales, website visits, or any data changing over time.
💼 Career
Data analysts and scientists use matplotlib.pyplot to quickly visualize data and communicate insights.
Progress0 / 4 steps
1
Create sales data and days list
Create a list called sales with these exact values: [150, 200, 170, 220, 180, 190, 210]. Also create a list called days with these exact values: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"].
Matplotlib
Hint
Use square brackets to create lists. Put the exact numbers and day names as shown.
2
Import pyplot and set up plot
Import matplotlib.pyplot as plt. This will allow you to use plotting functions easily.
Matplotlib
Hint
Use the syntax import matplotlib.pyplot as plt to import pyplot.
3
Plot sales data with labels
Use plt.plot(days, sales) to create a line plot. Then add x-axis label "Days" using plt.xlabel("Days") and y-axis label "Sales" using plt.ylabel("Sales").
Matplotlib
Hint
Use plt.plot() to draw the line. Use plt.xlabel() and plt.ylabel() to add labels.
4
Display the plot
Use plt.show() to display the plot window with your line chart.
Matplotlib
Hint
Call plt.show() to open the plot window.
Practice
(1/5)
1. What is the main purpose of the plt.show() function in matplotlib's Pyplot interface?
easy
A. To create a new figure object
B. To save the plot as an image file
C. To clear the current plot
D. To display the created plot on the screen
Solution
Step 1: Understand the role of plt.show()
This function is used to display all the figures and plots that have been created so far.
Step 2: Differentiate from other functions
Functions like plt.savefig() save images, and plt.clf() clears plots, but plt.show() actually opens the window to view the plot.
Final Answer:
To display the created plot on the screen -> Option D
Quick Check:
Display plot = plt.show() [OK]
Hint: Remember: plt.show() always displays your plot [OK]
Common Mistakes:
Confusing plt.show() with plt.savefig()
Thinking plt.show() clears the plot
Assuming plt.show() creates a new plot
2. Which of the following is the correct way to import the Pyplot interface from matplotlib?
easy
A. import matplotlib.pyplot as plt
B. import pyplot.matplotlib as plt
C. from matplotlib import plot as plt
D. import matplotlib.pyplot
Solution
Step 1: Recall the standard import syntax
The common and recommended way is to import pyplot from matplotlib and alias it as plt for easy use.
Step 2: Check each option
Only import matplotlib.pyplot as plt is syntactically correct and widely used.
Final Answer:
import matplotlib.pyplot as plt -> Option A
Quick Check:
Standard import = import matplotlib.pyplot as plt [OK]
Hint: Always import pyplot as plt for convenience [OK]
A. A line plot with points (1,4), (2,5), (3,6) and title 'Sample Plot' displayed
B. A scatter plot with points (1,4), (2,5), (3,6) without any title
C. No plot will be shown because plt.show() is missing
D. An error because plt.title() is not a valid function
Solution
Step 1: Analyze the plot function
plt.plot() with two lists creates a line plot connecting points (1,4), (2,5), and (3,6).
Step 2: Check the title and show functions
plt.title('Sample Plot') adds the title, and plt.show() displays the plot window.
Final Answer:
A line plot with points (1,4), (2,5), (3,6) and title 'Sample Plot' displayed -> Option A
Quick Check:
plt.plot + plt.title + plt.show = line plot with title [OK]
Hint: plt.plot draws lines; plt.show displays them [OK]
Common Mistakes:
Confusing plt.plot with scatter plot
Forgetting plt.show() to display plot
Assuming plt.title() is invalid
4. Identify the error in the following code snippet:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5])
plt.show()
medium
A. plt.show() is missing, so plot won't display
B. plt.plot() requires only one list, so two lists cause a syntax error
C. The x and y data lists have different lengths causing a ValueError
D. No error; the plot will display correctly
Solution
Step 1: Check the lengths of x and y data
The x list has 3 elements, but the y list has only 2 elements, which is invalid for plotting.
Step 2: Understand the error raised
This mismatch causes a ValueError because each x must have a corresponding y value.
Final Answer:
The x and y data lists have different lengths causing a ValueError -> Option C
Quick Check:
Length mismatch in plt.plot = ValueError [OK]
Hint: x and y lists must be same length in plt.plot [OK]
Common Mistakes:
Assuming plt.plot accepts different length lists
Thinking plt.show() is missing
Believing two lists cause syntax error
5. You want to create a plot with two lines on the same figure using Pyplot. Which code snippet correctly does this?
hard
A. import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.clear()
plt.plot([1, 2, 3], [6, 5, 4])
plt.show()
B. import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.plot([1, 2, 3], [6, 5, 4])
plt.show()
C. import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
plt.show()
D. import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
plt.plot([1, 2, 3], [6, 5, 4])
Solution
Step 1: Understand how to plot multiple lines
Calling plt.plot() multiple times before plt.show() adds multiple lines to the same figure.
Step 2: Check each option for correct sequence
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.plot([1, 2, 3], [6, 5, 4])
plt.show() calls both plot commands before a single show, so both lines appear together. Others either call show too early or clear the plot.
Final Answer:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.plot([1, 2, 3], [6, 5, 4])
plt.show() -> Option B
Quick Check:
Multiple plt.plot calls before plt.show = multiple lines [OK]
Hint: Plot all lines before plt.show() to display together [OK]
Common Mistakes:
Calling plt.show() too early clears previous plots
Using plt.clear() removes existing plots
Expecting multiple plt.show() calls to overlay plots
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.