Jan-Bert
Jan-Bert

Reputation: 1005

Shared y axis per row

I have a question about shared y axis per row using matplotlib's subplots. I have as example the following script. when i use sharey=True as argument in the subplots function and then ax[x, x].set_ylim(n, n) then it uses the last argument i used.

import matplotlib.pyplot as plt

fig, ax = plt.subplots(3, 2, figsize=(8, 10), sharey=True, gridspec_kw={'height_ratios': [1, 2, 2]})
ax[0, 0].set_ylim(-1.5, 1.5)
ax[1, 0].set_ylim(-40, 40)
ax[1, 1].set_ylim(-40, 40)
ax[2, 0].set_ylim(-40, 40)
ax[2, 1].set_ylim(-40, 40)

Is there a method that i can share the y axis per row?

Upvotes: 5

Views: 2326

Answers (1)

Thomas Kühn
Thomas Kühn

Reputation: 9810

You can do what you want with specifying sharey='row', as shown in this matplotlib demo. Here bit of code to show hot to do this:

from matplotlib import pyplot as plt
import numpy as np

x1 = np.linspace(0,2*np.pi,100)
x2 = np.linspace(-1,1,100)


f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharey='row')
ax1.plot(x1, np.sin(x1))
ax1.set_title('sin(x)')

ax2.plot(x1, 2*np.cos(x1)+1)
ax2.set_title('2*cos(x)+1')

ax3.plot(x2, np.sqrt(np.abs(x2)))
ax3.set_title('sqrt(abs(x))')

ax4.plot(x2, x2**3)
ax4.set_title('x**3')

f.tight_layout()

plt.show()

This gives the following result:

result of the above code

Upvotes: 8

Related Questions