Shift module usage

mpl_axes_aligner.shift expands or shifts the plotting range of a matplotlib axis to align the origin with the given position.

  • shift.xasis() for x-axis
  • shift.yasis() for y-axis

Shift y axis

In this section, shift.yaxis() is demonstrated by using the following plot:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0.0, 12, 0.1)
y = 0.1 * x * np.sin(x)

fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(x, y, color='blue', label='plot 1')
ax.hlines(y=0, xmin=0, xmax=30, linewidth=1)

ax.set_ylabel('Plot 1', color='blue')
ax.set_xlim(0.0, 12.0)

plt.tight_layout()
plt.show()
_images/shift_plt1.png

When you want to shift y = 0 to the center of figure, use shift.yaxis():

import numpy as np
import matplotlib.pyplot as plt
from mpl_axes_aligner import shift

x = np.arange(0.0, 12, 0.1)
y = 0.1 * x * np.sin(x)

fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(x, y, color='blue', label='plot 1')
ax.hlines(y=0, xmin=0, xmax=30, linewidth=1)

ax.set_ylabel('Plot 1', color='blue')
ax.set_xlim(0.0, 12.0)

# Adjust the plotting range of two y axes
org = 0.0  # Origin
pos = 0.5  # Position the origins is aligned
expand = True  # Switch the aligning method
shift.yaxis(ax, org, pos, expand)

plt.tight_layout()
plt.show()
_images/shift_plt2.png

Argument 1: Axis (ax)

The first argument of shift.yaxis() (ax) is the matplotlib.axes.Axes object which you want to change.

Argument 2: Origins (org)

The second argument of shift.yaxis() (org) is the origin which you want to align.

_images/shift_plt3.png

Argument 3: Relative position (pos)

The third argument of shift.yaxis() (pos) is the relative position which the origin is aligned. When expand = True, pos should satisfy the condition \(0 < pos < 1\), otherwise, pos should satisfy the condition \(0 \le pos \le 1\). When pos is nearly 0, the origin is shifted to bottom, when pos is nearly 1, on the other hand, the origin is shifted to top. When pos is 0.5, the origin is aligned at the center of figure.

_images/shift_plt4.png

Argument 4: Shifting method (expand)

The last argument of shift.yaxis() (expand) toggle the origin shift method. When expand = True, the plotting range is adjusted without reducing plotting range, otherwise, the plotting range is simply shifted:

_images/shift_plt5.png

The mathematical details are explained here. \(o\) and \(p\) are the given origin and relative position, respectively. And, \(y_b\) and \(y_t\) are the initial values of bottom and top of the axis. First, we calculate the relative position of the origin which represented by \(\bar{o}\):

\[\bar{o} = \frac{o - y_b}{y_t - y_b}\]

When expand = True, the new values of bottom and top, \(y'_b\) and \(y'_t\), are calculated to satisfy the condition \(\bar{o} = p\). When \(\bar{o} > p\):

\[\begin{split}y'_t &= \frac{o + (p - 1) \times y_b}{p} \\ y'_b &= y_b\end{split}\]

When \(\bar{o} < p\):

\[\begin{split}y'_t &= y_t \\ y'_b &= \frac{o - p \times y_t}{1 - p}\end{split}\]

When expand = False, the plotting range is simply shifted:

\[\begin{split}y'_t &= y_t + (\bar{o} - p) * (y_t - y_b) \\ y'_b &= y_b + (\bar{o} - p) * (y_t - y_b)\end{split}\]

Shift x axis

shift.xaxis() adjust the plotting range of x axis. The usage is same as y axis.