0
0
Matplotlibdata~20 mins

Axis limits (xlim, ylim) in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Axis Limits Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the x-axis range after setting xlim?
Consider the following code that plots a simple line and sets the x-axis limits. What will be the x-axis limits shown in the plot?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4, 5], [10, 20, 25, 30, 40])
plt.xlim(2, 4)
plt.show()
print(plt.xlim())
A(2.0, 4.0)
B(1.0, 5.0)
C(0.0, 6.0)
D(10.0, 40.0)
Attempts:
2 left
💡 Hint
The xlim function sets the visible range of the x-axis.
data_output
intermediate
2:00remaining
What is the y-axis range after setting ylim with reversed values?
Given this code snippet, what will be the output of plt.ylim() after setting ylim with reversed values?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [5, 10, 15])
plt.ylim(20, 0)
plt.show()
print(plt.ylim())
A(15.0, 5.0)
B(20.0, 0.0)
C(5.0, 15.0)
D(0.0, 20.0)
Attempts:
2 left
💡 Hint
Setting ylim with the first value larger than the second reverses the y-axis direction.
visualization
advanced
3:00remaining
Identify the plot with correct axis limits applied
Four plots are generated with different xlim and ylim settings. Which plot correctly shows x-axis from 0 to 10 and y-axis from -5 to 5?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 20, 100)
y = np.sin(x)

fig, axs = plt.subplots(2, 2, figsize=(8, 6))

axs[0, 0].plot(x, y)
axs[0, 0].set_xlim(0, 10)
axs[0, 0].set_ylim(-5, 5)
axs[0, 0].set_title('Plot A')

axs[0, 1].plot(x, y)
axs[0, 1].set_xlim(-5, 15)
axs[0, 1].set_ylim(-5, 5)
axs[0, 1].set_title('Plot B')

axs[1, 0].plot(x, y)
axs[1, 0].set_xlim(0, 10)
axs[1, 0].set_ylim(-10, 10)
axs[1, 0].set_title('Plot C')

axs[1, 1].plot(x, y)
axs[1, 1].set_xlim(0, 10)
axs[1, 1].set_ylim(-5, 0)
axs[1, 1].set_title('Plot D')

plt.tight_layout()
plt.show()
APlot A
BPlot B
CPlot C
DPlot D
Attempts:
2 left
💡 Hint
Check both xlim and ylim values for each plot.
🧠 Conceptual
advanced
2:00remaining
What happens if you set xlim with values outside data range?
If you plot data points between 0 and 5 on the x-axis but set xlim to (-10, 10), what will be the visible x-axis range and why?
AThe x-axis will automatically adjust to data range 0 to 5 ignoring xlim.
BThe plot will raise an error because xlim is outside data range.
CThe x-axis will show from -10 to 10, including empty space outside data points.
DThe x-axis will show only the data points from 0 to 5, ignoring the negative limit.
Attempts:
2 left
💡 Hint
Axis limits control the visible range regardless of data points.
🔧 Debug
expert
3:00remaining
Why does this code not change the axis limits as expected?
Examine the code below. The user tries to set xlim and ylim but the plot still shows default axis limits. What is the reason?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
plt.xlim(0, 5)
plt.ylim(0, 10)
print(plt.xlim(), plt.ylim())
AThe plot command resets axis limits after xlim and ylim are called.
BThe axis limits are ignored because the data points are outside the limits.
CThe plt.xlim and plt.ylim functions require arguments as tuples, not separate values.
DThe axis limits are set after plt.show(), so they have no effect on the displayed plot.
Attempts:
2 left
💡 Hint
plt.show() displays the plot immediately and freezes the current figure state.