Skip to content
Home » matplotlib Part 13 – Sharing Axis Scale

matplotlib Part 13 – Sharing Axis Scale

Spread the love

Sometimes you may want to share axis scale across multiple axes. Let’s have a look at the following example:

In [3]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-10, 10, 100)
y1 = x ** 2 - 2 * x + 3
y2 = 10 * np.sin(x)
y3 = 2 * x + 1

fig, axes = plt.subplots(3, 1)

# Here are the axes in three rows and one column.
axes[0].plot(x, y1, color='red')
axes[1].plot(x, y2, color='green')
axes[2].plot(x, y3, color='blue')
Out[3]:
[<matplotlib.lines.Line2D at 0x2166b4d1dc8>]

As you can see, the coordinates are not very readable. You can see right away that we’re using the same scale for the X axis, so we don’t have to repeat the tick labels for each axis individually. In order to share the scale on the X axis we set the sharex argument to True:

In [4]:
# Let's share the axis scale on the X axis.
fig, axes = plt.subplots(3, 1, sharex=True)

# Here are the axes in three rows and one column.
axes[0].plot(x, y1, color='red')
axes[1].plot(x, y2, color='green')
axes[2].plot(x, y3, color='blue')
Out[4]:
[<matplotlib.lines.Line2D at 0x2166b5f5fc8>]

Now it looks much more readable. And now let’s have a look at the other example where the axes are in one row and three columns:

In [5]:
fig, axes = plt.subplots(1, 3)

# Here are the axes in one row and three columns.
axes[0].plot(x, y1, color='red')
axes[1].plot(x, y2, color='green')
axes[2].plot(x, y3, color='blue')
Out[5]:
[<matplotlib.lines.Line2D at 0x2166b71de88>]

This time the scales on the Y axis are not the same, but we can still share them. The plots will adjust themselves to the shared scale. This time we should set the sharey argument to True:

In [6]:
# Let's share the axis scale on the X axis.
fig, axes = plt.subplots(1, 3, sharey=True)

# Here are the axes in one row and three columns.
axes[0].plot(x, y1, color='red')
axes[1].plot(x, y2, color='green')
axes[2].plot(x, y3, color='blue')
Out[6]:
[<matplotlib.lines.Line2D at 0x2166b82cbc8>]

Now the tick labels are more readable, although the plots themselves not necessarily so. It all depends on what you need. You may also share axis scale on both axes. Let’s have a look at this simple example with just some empty axes:

In [12]:
fig, axes = plt.subplots(4, 4)

And now suppose that we want all the axes to share both the X and Y axis scales. Here’s how we can do it:

In [15]:
fig, axes = plt.subplots(4, 4, sharex=True, sharey=True)

This is much more readable now.

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