In the previous part of the Matplotlib series we were talking about text annotations. In this part we’ll be talking about axis ticks.
An important element of each graph are axis ticks. There are major ticks and minor ticks. By default, major ticks are labeled, as opposed to minor ticks. Minor ticks fill the spaces between major ticks.
Let’s start by plotting a simple graph with default ticks:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1, 100, 300)
y = np.sin(x) / x
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(x, y)
As you can see, in the example above we did nothing about the ticks. But sometimes we may want to manipulate the ticks. There are a couple ways we can do it depending on what we need. We use the mpl.ticker module which contains classes for manipulating tick placement. In particular, we can use, among others, the following classes:
mpl.ticker.MaxNLocator – used to set the maximum number of ticks at unspecified locations, mpl.ticker.MultipleLocator – used to set the ticks at locations that are multiples of a given base, mpl.ticker.FixedLocator – used to set the ticks at explicitly specified locations
To choose a tick placement strategy we use the set_major_locator and set_minor_locator methods in Axes.xaxis and Axes.yaxis respectively. We just pass an instance of one of the ticker classes described above, so MaxNLocator, MultiplrLocator or FixedLocator. Have a look at the following examples:
# MaxNLocator
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(x, y)
# Set 4 major ticks at unspecified locations.
ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(4))
# MaxNLocator
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(x, y)
# Set 6 major ticks and 30 minor ticks at unspecified locations.
ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(6))
ax.xaxis.set_minor_locator(mpl.ticker.MaxNLocator(30))
# MultipleLocator
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(x, y)
# Set major ticks at multiples of 28 and minor ticks at multiples of 7.
ax.xaxis.set_major_locator(mpl.ticker.MultipleLocator(28))
ax.xaxis.set_minor_locator(mpl.ticker.MultipleLocator(7))
# FixedLocator
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(x, y)
# Set major ticks at locations specified in a list.
ax.xaxis.set_major_locator(mpl.ticker.FixedLocator([4, 17, 21, 36, 54, 82, 91]))
Naturally, you can also set the ticks for the Y axis. You can also mix all the different classes together. Here’s another example:
# FixedLocator
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(x, y)
# Set major ticks on the X axis at locations specified in a list.
ax.xaxis.set_major_locator(mpl.ticker.FixedLocator([6, 18, 44, 50, 82]))
# Set minor ticks on the X axis at locations that are multiples of 2.
ax.xaxis.set_minor_locator(mpl.ticker.MultipleLocator(2))
# Set 3 major ticks at unspecified locations on the Y axis.
ax.yaxis.set_major_locator(mpl.ticker.MaxNLocator(3))
# Set minor ticks on the Y axis at locations that are multiples of 0.1.
ax.yaxis.set_minor_locator(mpl.ticker.MultipleLocator(.1))
You can also explicitly specify tick locations using the set_xticks and set_yticks methods with lists of coordinates. Have a look:
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(x, y)
ax.set_xticks([10, 20, 50, 90])
ax.set_yticks([.2, .5, .7])