Skip to content
Home » matplotlib Part 11 – Axis Ticks

matplotlib Part 11 – Axis Ticks

Spread the love

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:

In [1]:
%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)
Out[1]:
[<matplotlib.lines.Line2D at 0x241a1f23c48>]

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:

In [2]:
# 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))
In [3]:
# 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))
In [4]:
# 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))
In [5]:
# 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:

In [6]:
# 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:

In [7]:
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(x, y)

ax.set_xticks([10, 20, 50, 90])
ax.set_yticks([.2, .5, .7])
Out[7]:
[<matplotlib.axis.YTick at 0x241a2dd57c8>,
 <matplotlib.axis.YTick at 0x241a2dd4e88>,
 <matplotlib.axis.YTick at 0x241a2dd3b48>]

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply