Align module usage

Align two y axes

In this section, align.yaxes() is demonstrated by using the following plot:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0.0, 30, 0.1)
y1 = 0.1 * x * np.sin(x)
y2 = 0.001*x**3 - 0.03*x**2 + 0.12*x

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()

ax1.plot(x, y1, color='blue', label='plot 1')
ax2.plot(x, y2, color='red', label='plot 2')
ax1.hlines(y=0, xmin=0, xmax=30, linewidth=1)

ax1.set_ylabel('Plot 1', color='blue')
ax2.set_ylabel('Plot 2', color='red')
ax1.set_xlim(0.0, 30.0)

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

When you want to align y = 0 point of left and right y axes, use align.yaxes():

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

x = np.arange(0.0, 30, 0.1)
y1 = 0.1 * x * np.sin(x)
y2 = 0.001*x**3 - 0.03*x**2 + 0.12*x

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()

ax1.plot(x, y1, color='blue', label='plot 1')
ax2.plot(x, y2, color='red', label='plot 2')
ax1.hlines(y=0, xmin=0, xmax=30, linewidth=1)

ax1.set_ylabel('Plot 1', color='blue')
ax2.set_ylabel('Plot 2', color='red')
ax1.set_xlim(0.0, 30.0)

# Adjust the plotting range of two y axes
org1 = 0.0  # Origin of first axis
org2 = 0.0  # Origin of second axis
pos = 0.5  # Position the two origins are aligned
align.yaxes(ax1, org1, ax2, org2, pos)

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

shift.yaxis() is called in align.yaxes(). The argument expand is always True (Shifting method (expand)).

Origins (org1 and org2)

The second and fourth arguments of align.yaxes() (org1 and org2) are the origins which you want to align.

_images/align_plt5.png

Relative position (pos)

The last argument of align.yaxes() (pos) is the relative position which the two origins are aligned. pos should satisfy the condition (See also Relative position (pos)).

_images/align_plt3.png

pos is optional argument, therefore, when pos is None, the two origins are aligned with the middle of them.

_images/align_plt4.png

Align two x axes

align.xaxes() adjust the plotting range of two x axes. The usage is same as y axes.