Bird
Raised Fist0
Matplotlibdata~20 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand the function purpose

    plt.xlim() sets the limits of the x-axis in a plot.
  2. Step 2: Analyze the arguments

    The arguments (0, 10) specify the minimum and maximum values for the x-axis.
  3. Final Answer:

    Sets the x-axis limits from 0 to 10 -> Option C
  4. Quick Check:

    plt.xlim(0, 10) = x-axis limits 0 to 10 [OK]
Hint: Remember xlim controls x-axis range, ylim controls y-axis [OK]
Common Mistakes:
  • Confusing xlim with ylim
  • Thinking it plots data points
  • Assuming it clears the plot
2. Which of the following is the correct syntax to set y-axis limits from -5 to 5 in matplotlib?
easy
A. plt.ylim(-5, 5)
B. plt.xlim(-5, 5)
C. plt.axis(ymin=-5, ymax=5)
D. plt.set_ylim(-5, 5)

Solution

  1. Step 1: Identify the function for y-axis limits

    The correct function to set y-axis limits is plt.ylim().
  2. Step 2: Check the syntax

    The syntax plt.ylim(-5, 5) correctly sets the y-axis from -5 to 5.
  3. Final Answer:

    plt.ylim(-5, 5) -> Option A
  4. Quick Check:

    ylim(-5, 5) sets y-axis limits correctly [OK]
Hint: Use plt.ylim() for y-axis limits, not plt.xlim() [OK]
Common Mistakes:
  • Using plt.xlim() for y-axis limits
  • Using non-existent plt.set_ylim()
  • Confusing plt.axis() parameters
3. What will be the x-axis limits after running this code?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.xlim(2, 3)
plt.show()
medium
A. [1, 4]
B. [2, 3]
C. [0, 1]
D. [3, 4]

Solution

  1. Step 1: Understand the plot and xlim call

    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.
  2. Step 2: Confirm the axis limits

    Because of plt.xlim(2, 3), the x-axis will only show values between 2 and 3.
  3. Final Answer:

    [2, 3] -> Option B
  4. 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

  1. 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.
  2. Step 2: Understand axis limit rules

    Axis limits must have the minimum value first and maximum second. Reversed limits cause unexpected behavior or warnings.
  3. Final Answer:

    xlim limits are reversed; min should be less than max -> Option D
  4. 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

  1. Step 1: Identify correct axis limits for x and y

    The question states x between 20 and 40, y between 10 and 30.
  2. Step 2: Match limits to correct functions

    plt.xlim(20, 40) sets x-axis limits, plt.ylim(10, 30) sets y-axis limits correctly.
  3. Final Answer:

    plt.xlim(20, 40) plt.ylim(10, 30) -> Option A
  4. Quick Check:

    Use xlim for x-axis, ylim for y-axis limits [OK]
Hint: Match xlim to x-axis range, ylim to y-axis range [OK]
Common Mistakes:
  • Swapping x and y limits
  • Using list brackets in xlim/ylim
  • Using plt.axis with wrong order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard