0
0
Matplotlibdata~10 mins

Major and minor ticks in Matplotlib - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set major ticks on the x-axis every 2 units.

Matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoLocator, FixedLocator, MaxNLocator, MultipleLocator
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4, 5])
ax.xaxis.set_major_locator([1](2))
plt.show()
Drag options to blanks, or click blank then click option'
AAutoLocator
BMaxNLocator
CFixedLocator
DMultipleLocator
Attempts:
3 left
💡 Hint
Common Mistakes
Using MaxNLocator which tries to choose a maximum number of ticks but not fixed intervals.
Using FixedLocator without specifying exact tick positions.
2fill in blank
medium

Complete the code to set minor ticks on the y-axis every 0.5 units.

Matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoLocator, FixedLocator, MaxNLocator, MultipleLocator
fig, ax = plt.subplots()
ax.plot([0.1, 0.4, 0.9, 1.2, 1.8])
ax.yaxis.set_minor_locator([1](0.5))
plt.show()
Drag options to blanks, or click blank then click option'
AMultipleLocator
BMaxNLocator
CFixedLocator
DAutoLocator
Attempts:
3 left
💡 Hint
Common Mistakes
Using MaxNLocator which does not guarantee fixed intervals.
Not setting minor ticks locator at all.
3fill in blank
hard

Fix the error in the code to correctly set major ticks at 1, 3, and 5 on the x-axis.

Matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoLocator, FixedLocator, MaxNLocator, MultipleLocator
fig, ax = plt.subplots()
ax.plot([10, 20, 30, 40, 50])
ax.xaxis.set_major_locator([1]([1, 3, 5]))
plt.show()
Drag options to blanks, or click blank then click option'
AMultipleLocator
BFixedLocator
CMaxNLocator
DAutoLocator
Attempts:
3 left
💡 Hint
Common Mistakes
Using MultipleLocator which expects a single base number, not a list.
Using MaxNLocator which chooses ticks automatically.
4fill in blank
hard

Fill both blanks to set major ticks every 1 unit and minor ticks every 0.2 units on the y-axis.

Matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoLocator, FixedLocator, MaxNLocator, MultipleLocator
fig, ax = plt.subplots()
ax.plot([0, 1, 2, 3, 4])
ax.yaxis.set_major_locator([1](1))
ax.yaxis.set_minor_locator([2](0.2))
plt.show()
Drag options to blanks, or click blank then click option'
AMultipleLocator
BMaxNLocator
CFixedLocator
DAutoLocator
Attempts:
3 left
💡 Hint
Common Mistakes
Using different locator types for major and minor ticks.
Using MaxNLocator which does not guarantee fixed intervals.
5fill in blank
hard

Fill all three blanks to create a dictionary of minor tick positions for x-axis where keys are the tick positions and values are their squares, but only for ticks greater than 2.

Matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoLocator, FixedLocator, MaxNLocator, MultipleLocator
fig, ax = plt.subplots()
ax.xaxis.set_minor_locator(MultipleLocator(1))
minor_ticks = { [1] : [2] for [3] in ax.xaxis.get_minor_locator().tick_values(0, 5) if [3] > 2 }
print(minor_ticks)
Drag options to blanks, or click blank then click option'
Atick
Btick**2
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not squaring the tick values for dictionary values.