How to arrange two ylabels using Python Matplotlib.pyplot


The result is:


This page shows how to strictly align two ylabels.
In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
In [2]:
x = np.arange(0,10,0.01)
y1 = np.abs(np.sin(x))
y2 = 100*np.sin(x)
In [3]:
plt.figure(figsize=(4.7,3.7),facecolor="w")
ax1 = plt.subplot(2,1,1)
ax2 = plt.subplot(2,1,2,sharex=ax1)
ax1.plot(x,y1)
ax2.plot(x,y2)
ax1.set_ylabel("y Axis 1")
ax2.set_ylabel("y Axis 2")
ax2.set_xlabel("x Axis")
plt.setp(ax1.get_xticklabels(),visible=False)
plt.savefig("./align_two_ylabels_1.png",dpi=250,bbox_inches="tight",pad_inches=0.02)
In [4]:
plt.figure(figsize=(4.7,3.7),facecolor="w")
ax1 = plt.subplot(2,1,1)
ax2 = plt.subplot(2,1,2,sharex=ax1)
ax1.plot(x,y1)
ax2.plot(x,y2)
ax1.set_ylabel("y Axis 1",x=-1)
ax2.set_ylabel("y Axis 2",x=-1)
ax2.set_xlabel("x Axis")
ax1.yaxis.set_label_coords(-0.1,0.5)
ax2.yaxis.set_label_coords(-0.1,0.5)
plt.setp(ax1.get_xticklabels(),visible=False)
plt.savefig("./align_two_ylabels_2.png",dpi=250,bbox_inches="tight",pad_inches=0.02)