Complete the code to set major ticks on the x-axis every 2 units.
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()
The MultipleLocator sets ticks at multiples of the given base, here 2.
Complete the code to set minor ticks on the y-axis every 0.5 units.
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()
MultipleLocator is used to set ticks at fixed intervals, here 0.5 for minor ticks.
Fix the error in the code to correctly set major ticks at 1, 3, and 5 on the x-axis.
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()
FixedLocator is used to set ticks at specific positions given as a list.
Fill both blanks to set major ticks every 1 unit and minor ticks every 0.2 units on the y-axis.
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()
MultipleLocator is used for both major and minor ticks to set fixed intervals.
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.
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)
The dictionary comprehension uses tick as the key and tick**2 as the value, iterating over minor tick positions.