0
0
Matplotlibdata~30 mins

Axis limits (xlim, ylim) in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Axis limits (xlim, ylim)
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Call plt.show() to open the graph window and see your plot.