Bird
Raised Fist0
Matplotlibdata~20 mins

Major and minor ticks in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Tick Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Identify the number of major ticks on the x-axis
Given the following matplotlib code, how many major ticks will appear on the x-axis?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0, 1, 2, 3, 4], [10, 20, 25, 30, 40])
ax.xaxis.set_major_locator(plt.MultipleLocator(1))
plt.show()
A3
B5
C4
D6
Attempts:
2 left
💡 Hint
Count the ticks starting from 0 to 4 with step 1.
❓ data_output
intermediate
2:00remaining
Output of minor tick positions on y-axis
What are the positions of the minor ticks on the y-axis after running this code?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0, 1, 2], [10, 20, 30])
ax.yaxis.set_minor_locator(plt.MultipleLocator(5))
minor_ticks = ax.yaxis.get_minorticklocs()
print(minor_ticks)
A[5. 10. 15. 20. 25. 30.]
B[0. 5. 10. 15. 20. 25. 30.]
C[10. 15. 20. 25. 30.]
D[12.5 17.5 22.5 27.5]
Attempts:
2 left
💡 Hint
Minor ticks start at 10 and go up to 30 in steps of 5.
❓ visualization
advanced
3:00remaining
Visualize major and minor ticks on both axes
Which option shows the correct plot with major ticks every 2 units and minor ticks every 0.5 units on both axes?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0, 1, 2, 3, 4], [0, 1, 4, 9, 16])
ax.xaxis.set_major_locator(plt.MultipleLocator(2))
ax.xaxis.set_minor_locator(plt.MultipleLocator(0.5))
ax.yaxis.set_major_locator(plt.MultipleLocator(5))
ax.yaxis.set_minor_locator(plt.MultipleLocator(1))
plt.show()
APlot with x-axis major ticks at 0,2,4 and minor ticks every 0.5; y-axis major ticks at 0,5,10,15 and minor ticks every 1
BPlot with x-axis major ticks at 0,1,2,3,4 and minor ticks every 0.2; y-axis major ticks at 0,4,8,12 and minor ticks every 0.5
CPlot with x-axis major ticks at 1,3 and minor ticks every 1; y-axis major ticks at 0,10,20 and minor ticks every 2
DPlot with x-axis major ticks at 0,2,4 and no minor ticks; y-axis major ticks at 0,5,10,15 and minor ticks every 0.5
Attempts:
2 left
💡 Hint
Check the locator settings for both axes carefully.
🔧 Debug
advanced
2:00remaining
Identify the error in minor tick setting
What error will this code raise when setting minor ticks?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0, 1, 2], [10, 20, 30])
ax.xaxis.set_minor_locator(plt.MultipleLocator(0))
plt.show()
AValueError: interval must be > 0
BTypeError: 'MultipleLocator' object is not callable
CNo error, plot shows with default minor ticks
DAttributeError: 'AxesSubplot' object has no attribute 'set_minor_locator'
Attempts:
2 left
💡 Hint
Check the argument passed to MultipleLocator.
🚀 Application
expert
3:00remaining
Customize ticks to highlight weekends on a time series plot
You have daily data for 14 days starting from Monday. You want to set major ticks on Mondays and minor ticks on other days. Which code snippet correctly sets this using matplotlib's FixedLocator?
Matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd
import numpy as np

dates = pd.date_range('2023-01-02', periods=14)  # Monday start
values = np.random.rand(14)
fig, ax = plt.subplots()
ax.plot(dates, values)

# Your code here to set major ticks on Mondays and minor ticks on other days

plt.show()
A
mondays = mdates.date2num(dates[dates.weekday == 0])
ax.xaxis.set_major_locator(mdates.FixedLocator(mondays))
ax.xaxis.set_minor_locator(mdates.FixedLocator(mondays))
B
mondays = mdates.date2num(dates[dates.weekday == 1])
all_days = mdates.date2num(dates)
ax.xaxis.set_major_locator(mdates.FixedLocator(mondays))
ax.xaxis.set_minor_locator(mdates.FixedLocator(all_days))
C
mondays = mdates.date2num(dates[dates.weekday == 0])
ax.xaxis.set_major_locator(mdates.DayLocator(interval=7))
ax.xaxis.set_minor_locator(mdates.DayLocator(interval=1))
D
mondays = mdates.date2num(dates[dates.weekday == 0])
all_days = mdates.date2num(dates)
ax.xaxis.set_major_locator(mdates.FixedLocator(mondays))
ax.xaxis.set_minor_locator(mdates.FixedLocator(np.setdiff1d(all_days, mondays)))
Attempts:
2 left
💡 Hint
Use FixedLocator with dates converted to numbers and exclude Mondays from minor ticks.

Practice

(1/5)
1. What is the main purpose of major ticks in a matplotlib plot?
easy
A. To change the color of the plot
B. To add grid lines to the plot
C. To label the plot title
D. To mark the main points on the axes

Solution

  1. Step 1: Understand the role of major ticks

    Major ticks are the primary markers on the axes that show key values.
  2. Step 2: Differentiate from other elements

    Grid lines and titles are different plot features, not related to major ticks.
  3. Final Answer:

    To mark the main points on the axes -> Option D
  4. Quick Check:

    Major ticks = main axis points [OK]
Hint: Major ticks show main axis points clearly [OK]
Common Mistakes:
  • Confusing major ticks with grid lines
  • Thinking major ticks change plot colors
  • Mixing up ticks with plot titles
2. Which of the following is the correct way to set minor ticks on the x-axis using matplotlib?
easy
A. plt.minorticks_on()
B. plt.set_minor_ticks()
C. plt.minor_ticks()
D. plt.set_minor()

Solution

  1. Step 1: Recall matplotlib minor ticks method

    The correct method to enable minor ticks is plt.minorticks_on().
  2. Step 2: Check other options

    Other options are not valid matplotlib functions and will cause errors.
  3. Final Answer:

    plt.minorticks_on() -> Option A
  4. Quick Check:

    Enable minor ticks = plt.minorticks_on() [OK]
Hint: Use plt.minorticks_on() to enable minor ticks [OK]
Common Mistakes:
  • Using non-existent functions like plt.set_minor_ticks()
  • Confusing minor ticks enabling with setting labels
  • Forgetting to call the function
3. What will be the effect of this code snippet?
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4])
ax.xaxis.set_major_locator(MultipleLocator(2))
ax.xaxis.set_minor_locator(MultipleLocator(0.5))
ax.minorticks_on()
plt.show()
medium
A. Major ticks every 2 units, minor ticks every 0.5 units on x-axis
B. Major ticks every 0.5 units, minor ticks every 2 units on x-axis
C. Only major ticks at 1 and 3, no minor ticks
D. Syntax error due to wrong locator usage

Solution

  1. Step 1: Analyze major locator setting

    Major ticks are set every 2 units on the x-axis using MultipleLocator(2).
  2. Step 2: Analyze minor locator setting

    Minor ticks are set every 0.5 units using MultipleLocator(0.5).
  3. Final Answer:

    Major ticks every 2 units, minor ticks every 0.5 units on x-axis -> Option A
  4. Quick Check:

    Major=2, Minor=0.5 units [OK]
Hint: Major locator sets big steps, minor locator sets small steps [OK]
Common Mistakes:
  • Swapping major and minor locator values
  • Assuming no minor ticks appear without plt.minorticks_on()
  • Thinking locator causes syntax error
4. Identify the error in this code that tries to set minor ticks:
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig, ax = plt.subplots()
ax.plot([10, 20, 30])
ax.xaxis.set_minor_locator(MultipleLocator(1))
plt.minorticks_on()
plt.show()
medium
A. plt.minorticks_on() should be called on ax, not plt
B. No error, code runs fine
C. Missing plt.show() call
D. MultipleLocator cannot be used for minor ticks

Solution

  1. Step 1: Check locator and import

    MultipleLocator is correctly used for minor ticks with proper import from matplotlib.ticker.
  2. Step 2: Verify minorticks_on()

    plt.minorticks_on() works after plt.subplots() as it applies to the current axes object.
  3. Final Answer:

    No error, code runs fine -> Option B
  4. Quick Check:

    plt.minorticks_on() works with ax [OK]
Hint: plt.minorticks_on() works even with explicit axes objects [OK]
Common Mistakes:
  • Not importing MultipleLocator from matplotlib.ticker
  • Thinking MultipleLocator cannot be used for minor ticks
  • Forgetting to call plt.show()
5. You want to create a plot with major ticks every 5 units and minor ticks every 1 unit on the y-axis only. Which code snippet correctly achieves this?
hard
A. ax.yaxis.set_major_locator(MultipleLocator(1)) ax.yaxis.set_minor_locator(MultipleLocator(5)) plt.minorticks_on()
B. ax.xaxis.set_major_locator(MultipleLocator(5)) ax.xaxis.set_minor_locator(MultipleLocator(1)) ax.minorticks_on()
C. ax.yaxis.set_major_locator(MultipleLocator(5)) ax.yaxis.set_minor_locator(MultipleLocator(1)) ax.minorticks_on()
D. ax.yaxis.set_major_locator(MultipleLocator(5)) ax.yaxis.set_minor_locator(MultipleLocator(1)) plt.minorticks_off()

Solution

  1. Step 1: Set major and minor locators on y-axis

    Major ticks every 5 units and minor ticks every 1 unit require ax.yaxis.set_major_locator(MultipleLocator(5)) and ax.yaxis.set_minor_locator(MultipleLocator(1)).
  2. Step 2: Enable minor ticks on the axes object

    Use ax.minorticks_on() to show minor ticks on the axes.
  3. Final Answer:

    ax.yaxis.set_major_locator(MultipleLocator(5)) ax.yaxis.set_minor_locator(MultipleLocator(1)) ax.minorticks_on() -> Option C
  4. Quick Check:

    Major=5, Minor=1 on y-axis with ax.minorticks_on() [OK]
Hint: Set locators on yaxis and call ax.minorticks_on() [OK]
Common Mistakes:
  • Setting locators on x-axis instead of y-axis
  • Calling plt.minorticks_on() instead of ax.minorticks_on()
  • Swapping major and minor locator values
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