📖 Scenario: You are analyzing the sales data of a small store over a week. You want to visualize the sales numbers clearly by setting the limits of the x-axis and y-axis on the graph.
🎯 Goal: Create a simple line plot of daily sales and set the x-axis limits to show only days 1 to 7 and the y-axis limits to show sales from 0 to 100.
📋 What You'll Learn
Create a list called days with values from 1 to 7 representing days of the week.
Create a list called sales with the exact sales numbers: 20, 35, 30, 50, 70, 65, 80.
Create two variables called x_min and x_max with values 1 and 7 respectively.
Create two variables called y_min and y_max with values 0 and 100 respectively.
Use plt.plot(days, sales) to plot the sales data.
Use plt.xlim(x_min, x_max) and plt.ylim(y_min, y_max) to set the axis limits.
Use plt.show() to display the plot.
💡 Why This Matters
🌍 Real World
Setting axis limits helps focus on important parts of data in graphs, making trends easier to see.
💼 Career
Data scientists and analysts often adjust axis limits to create clear and informative visualizations for reports and presentations.
Progress0 / 4 steps
1
Create the sales data lists
Create a list called days with values [1, 2, 3, 4, 5, 6, 7] and a list called sales with values [20, 35, 30, 50, 70, 65, 80].
Matplotlib
Hint
Use square brackets to create lists. Put the exact numbers inside the lists separated by commas.
2
Set the axis limit variables
Create variables x_min and x_max with values 1 and 7 respectively. Also create variables y_min and y_max with values 0 and 100 respectively.
Matplotlib
Hint
Assign the numbers directly to the variable names using the equals sign.
3
Plot the sales data and set axis limits
Import matplotlib.pyplot as plt. Use plt.plot(days, sales) to plot the sales data. Then use plt.xlim(x_min, x_max) and plt.ylim(y_min, y_max) to set the x-axis and y-axis limits.
Matplotlib
Hint
Use import matplotlib.pyplot as plt at the top. Then call plt.plot() with the two lists. Use plt.xlim() and plt.ylim() with the variables to set limits.
4
Display the plot
Use plt.show() to display the plot with the set axis limits.
Matplotlib
Hint
Call plt.show() to open the graph window and see your plot.
Practice
(1/5)
1. What does the function plt.xlim(0, 10) do in a matplotlib plot?
easy
A. Clears the current plot
B. Sets the y-axis limits from 0 to 10
C. Sets the x-axis limits from 0 to 10
D. Plots data points between 0 and 10 on x-axis
Solution
Step 1: Understand the function purpose
plt.xlim() sets the limits of the x-axis in a plot.
Step 2: Analyze the arguments
The arguments (0, 10) specify the minimum and maximum values for the x-axis.
The plot has x values 0 to 3 by default (index of list). The call plt.xlim(2, 3) sets the x-axis limits explicitly from 2 to 3.
Step 2: Confirm the axis limits
Because of plt.xlim(2, 3), the x-axis will only show values between 2 and 3.
Final Answer:
[2, 3] -> Option B
Quick Check:
plt.xlim(2, 3) sets x-axis limits to [2, 3] [OK]
Hint: xlim sets exact x-axis range regardless of data [OK]
Common Mistakes:
Assuming default axis limits remain
Confusing data values with axis limits
Ignoring that x-axis is index by default
4. Identify the error in this code snippet:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.xlim(5, 1)
plt.show()
medium
A. plt.show() is missing parentheses
B. plt.plot() missing x values
C. plt.xlim() requires three arguments
D. xlim limits are reversed; min should be less than max
Solution
Step 1: Check the xlim arguments
The call plt.xlim(5, 1) sets the lower limit to 5 and upper limit to 1, which is reversed.
Step 2: Understand axis limit rules
Axis limits must have the minimum value first and maximum second. Reversed limits cause unexpected behavior or warnings.
Final Answer:
xlim limits are reversed; min should be less than max -> Option D
Quick Check:
Lower limit must be less than upper limit in xlim [OK]
Hint: Always put smaller number first in xlim and ylim [OK]
Common Mistakes:
Reversing min and max values in limits
Thinking xlim needs three arguments
Ignoring function call syntax
5. You have a scatter plot with x values ranging from 0 to 100 and y values from 0 to 50. You want to zoom in on the region where x is between 20 and 40 and y is between 10 and 30. Which code correctly sets these axis limits?
hard
A. plt.xlim(20, 40)
plt.ylim(10, 30)
B. plt.xlim(10, 30)
plt.ylim(20, 40)
C. plt.xlim([10, 30])
plt.ylim([20, 40])
D. plt.axis([10, 30, 20, 40])
Solution
Step 1: Identify correct axis limits for x and y
The question states x between 20 and 40, y between 10 and 30.