47

If you have multiple subplots containing a secondary y-axis (created using twinx), how can you share these secondary y-axes between the subplots? I want them to scale equally in an automatic way (so not set the y-limits afterwards by hand). For the primary y-axis, this is possible by using the keyword sharey in the call of subplot.

Below example shows my attempt, but it fails to share the secondary y-axis of both subplots. I'm using Matplotlib:

ax = []
#create upper subplot
ax.append(subplot(211))
plot(rand(1) * rand(10),'r')
#create plot on secondary y-axis of upper subplot
ax.append(ax[0].twinx())
plot(10*rand(1) * rand(10),'b')
#create lower subplot and share y-axis with primary y-axis of upper subplot
ax.append(subplot(212, sharey = ax[0]))
plot(3*rand(1) * rand(10),'g')
#create plot on secondary y-axis of lower subplot
ax.append(ax[2].twinx())
#set twinxed axes as the current axes again,
#but now attempt to share the secondary y-axis
axes(ax[3], sharey = ax[1])
plot(10*rand(1) * rand(10),'y')

This gets me something like:

Example of two subplots with failed sharing of secondary y-axis

The reason I used the axes() function to set the shared y-axis is that twinx doesn't accept the sharey keyword.

I am using Python 3.2 on Win7 x64. Matplotlib version is 1.2.0rc2.

cottontail
25k25 gold badges178 silver badges173 bronze badges
asked Oct 16, 2012 at 16:25

2 Answers 2

58

You can use Axes.get_shared_y_axes() like so:

from numpy.random import rand
import matplotlib
matplotlib.use('gtkagg')
import matplotlib.pyplot as plt
# create all axes we need
ax0 = plt.subplot(211)
ax1 = ax0.twinx()
ax2 = plt.subplot(212)
ax3 = ax2.twinx()
# share the secondary axes
ax1.get_shared_y_axes().join(ax1, ax3)
ax0.plot(rand(1) * rand(10),'r')
ax1.plot(10*rand(1) * rand(10),'b')
ax2.plot(3*rand(1) * rand(10),'g')
ax3.plot(10*rand(1) * rand(10),'y')
plt.show()

Here we're just joining the secondary axes together.

starball
58.1k49 gold badges295 silver badges1k bronze badges
answered Oct 16, 2012 at 22:49
5
  • Yes, that helped me further, thanks. I also joined the primary y-axes so that they have a shared y-axis as well. Commented Oct 17, 2012 at 11:25
  • 3
    It doesn't work, the second twin scale get updated, but the second twin plot stays were it is. Commented Mar 22, 2017 at 14:57
  • 1
    Just to not have people get the wrong impression, the code as it is in this answer works fine. Commented Jan 17, 2018 at 15:49
  • 3
    The problem @mattia might have encountered: If you call a function like ax1.set_ylim() before Axes.get_shared_y_axes().join() then the two axes will NOT have the same scales (until you shift the axes interactively). Call get_shared_y_axes().join() first. BTW it works fine if you call it after calling plot(), etc. Commented Jul 19, 2018 at 8:53
  • 3
    join is deprecated in matplotlib 3.6. Commented Aug 21, 2023 at 10:34
5

@dmcdougall's solution doesn't work anymore because GrouperView doesn't define join method anymore. However, it defines joined, so replacing join by joined in their code makes it run as expected.

However, a better solution is to use the method recommended in the docs to share y-axis: Axes.sharey(Axes). A working example goes as follows:

import numpy as np
import matplotlib.pyplot as plt
# create all Axes
fig, (ax0, ax2) = plt.subplots(2, sharey=True) # share the primary y-axis
ax1 = ax0.twinx()
ax3 = ax2.twinx()
ax1.sharey(ax3) # share the secondary y-axis
ax0.plot(np.random.rand(10)*1., 'r')
ax1.plot(np.random.rand(10)*10, 'b')
ax2.plot(np.random.rand(10)*3., 'g')
ax3.plot(np.random.rand(10)*10, 'y')

which plots a figure like the following:

result

answered Nov 20, 2023 at 7:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.