Challenge - 5 Problems
Axis Limits Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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())
Attempts:
2 left
💡 Hint
The xlim function sets the visible range of the x-axis.
✗ Incorrect
The plt.xlim(2, 4) command sets the x-axis limits to start at 2 and end at 4. The print statement confirms this by outputting (2.0, 4.0).
❓ data_output
intermediate2: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())
Attempts:
2 left
💡 Hint
Setting ylim with the first value larger than the second reverses the y-axis direction.
✗ Incorrect
When ylim is set as (20, 0), the y-axis is reversed, so plt.ylim() returns (20.0, 0.0).
❓ visualization
advanced3: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()
Attempts:
2 left
💡 Hint
Check both xlim and ylim values for each plot.
✗ Incorrect
Only Plot A has x-axis limits from 0 to 10 and y-axis limits from -5 to 5 as set by set_xlim(0, 10) and set_ylim(-5, 5).
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Axis limits control the visible range regardless of data points.
✗ Incorrect
Setting xlim to (-10, 10) forces the x-axis to show that range, even if data points only exist between 0 and 5. This creates empty space on the plot.
🔧 Debug
expert3: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())
Attempts:
2 left
💡 Hint
plt.show() displays the plot immediately and freezes the current figure state.
✗ Incorrect
Calling plt.xlim and plt.ylim after plt.show() does not affect the already displayed plot. Axis limits must be set before plt.show().