I want to align my y-axis labels like in this example. However, I don't want to use a fixed value like ax4.yaxis.set_label_coords(labelx, 0.5)
, but rather something like this:
x = np.linspace(-np.pi, np.pi, 100)
y1 = np.sin(x)**2
y2 = np.sin(x)
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
ax1.plot(x, y1)
ax2.plot(x, y2)
ax1.set_ylabel('y1')
ax2.set_ylabel('y2')
The position of the lower label is "correct". I want to determine its x-coordinate and apply it to the upper label. How do I do this?
I tried different variations of:
transform = ax2.yaxis.get_transform()
ax1.yaxis.set_transform(transform)
But that didn't lead to a solution.