Challenge - 5 Problems
Tick Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Count the ticks starting from 0 to 4 with step 1.
✗ Incorrect
The major locator is set to place ticks every 1 unit on the x-axis from 0 to 4, so ticks appear at 0,1,2,3,4 totaling 5 ticks.
❓ data_output
intermediate2: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)
Attempts:
2 left
💡 Hint
Minor ticks start at 10 and go up to 30 in steps of 5.
✗ Incorrect
Minor ticks are placed every 5 units on y-axis. The y limits range approximately from 10 to 30, so minor ticks appear at 10,15,20,25,30.
❓ visualization
advanced3: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()
Attempts:
2 left
💡 Hint
Check the locator settings for both axes carefully.
✗ Incorrect
The code sets x major ticks every 2 units (0,2,4) and minor ticks every 0.5 units; y major ticks every 5 units (0,5,10,15) and minor ticks every 1 unit.
🔧 Debug
advanced2: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()
Attempts:
2 left
💡 Hint
Check the argument passed to MultipleLocator.
✗ Incorrect
MultipleLocator requires a positive interval. Zero interval causes ValueError.
🚀 Application
expert3: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()
Attempts:
2 left
💡 Hint
Use FixedLocator with dates converted to numbers and exclude Mondays from minor ticks.
✗ Incorrect
Option D correctly sets major ticks on Mondays and minor ticks on all other days by using FixedLocator with date numbers and numpy setdiff1d to exclude Mondays from minor ticks.