In Matplotlib, what axis attribute specifies the spacing between ticks? [duplicate]

A more recent answer to the related question illustrates using the matplotlib.ticker.MultipleLocator object. The axis ticks are this type of matplotlib object. Here is an example of it’s use.

ax.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(5))

will place ticks 5 units apart on the x-axis, and

ax.xaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(1))

will place minor ticks 1 unit apart on the x-axis.

Here is an example from the matplotlib Plotting Cookbook

import numpy as np
import matplotlib.pyplot as plt
import matplotlib

X = np.linspace(-15, 15, 1024)
Y = np.sinc(X)

ax = plt.axes()
ax.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(5))
ax.xaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(1))

plt.plot(X, Y, c="k")
plt.show()

enter image description here

Leave a Comment