Share second y-axis

Hello,

I create a 2x2 grid of subplots:

f, sp = plt.subplots(2, 2, sharex='col', sharey='row')
sp_lin = [ sp[0][0], sp[0][1], sp[1][0], sp[1][1] ]

with two y-axis each

ax1 = sp_lin[i]
ax1.semilogy(...)
ax1.set_ylabel("RMSE")

ax2 = ax1.twinx()
ax2.semilogy(...)
ax2.set_ylabel("Condition")

ax1.set_xlabel("m")

The x-axis (m) is shared just nicely, as well as the first y-axis (RMSE). But the second y-axis (Condition) is not shared.

How can I make it being shared as well?

1 2
3 4

Only plots 1 and 3 have the RMSE axis on their left. I want that only plots 2 and 4 have the Condition axis on the right.

Thanks!
Florian

1 Like

Florian,

twinx restricts the parameters you can pass to the Axes constructor in
return for setting up the new axis properly. If you are willing to execute
most of that setup code "manually", you can replace `ax2 = ax1.twinx()`
with the following, keeping everything else the same:

        ax2 = f.add_axes(ax1.get_position(True), sharex=ax1, sharey=ax4)
        ax2.yaxis.tick_right()
        ax2.yaxis.set_label_position('right')
        ax2.yaxis.set_offset_position('right')
        ax2.set_autoscalex_on(ax1.get_autoscalex_on())
        ax1.yaxis.tick_left()
        ax2.xaxis.set_visible(False)
        ax2.patch.set_visible(False)

This code is basically a copy of `Axes.twinx` with the modifications you
wanted. You may not actually need all of the setup steps. I have not tested
this code, but it should at least start you in the right direction.

    -Joe

···

On Mon, Jan 30, 2017 at 5:06 AM, Florian Lindner <mailinglists at xgm.de> wrote:

Hello,

I create a 2x2 grid of subplots:

f, sp = plt.subplots(2, 2, sharex='col', sharey='row')
sp_lin = [ sp[0][0], sp[0][1], sp[1][0], sp[1][1] ]

with two y-axis each

ax1 = sp_lin[i]
ax1.semilogy(...)
ax1.set_ylabel("RMSE")

ax2 = ax1.twinx()
ax2.semilogy(...)
ax2.set_ylabel("Condition")

ax1.set_xlabel("m")

The x-axis (m) is shared just nicely, as well as the first y-axis (RMSE).
But the second y-axis (Condition) is not shared.

How can I make it being shared as well?

1 2
3 4

Only plots 1 and 3 have the RMSE axis on their left. I want that only
plots 2 and 4 have the Condition axis on the right.

Thanks!
Florian

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
Matplotlib-users Info Page

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20170130/3c4ad4ed/attachment.html&gt;