1

Given the subplots setup shown below, is there any possibility to add a super y label at the right side of the plot analogously to the 'Green label'?

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2, sharex=True)

axs[0,0].tick_params(axis ='y', labelcolor = 'g')
t = axs[0,0].twinx()
t.tick_params(axis ='y', labelcolor = 'b')

axs[0,1].tick_params(axis ='y', labelcolor = 'g')
axs[0,1].twinx().tick_params(axis ='y', labelcolor = 'b')

axs[1,0].tick_params(axis ='y', labelcolor = 'g')
axs[1,0].twinx().tick_params(axis ='y', labelcolor = 'b')

axs[1,1].tick_params(axis ='y', labelcolor = 'g')
axs[1,1].twinx().tick_params(axis ='y', labelcolor = 'b')


fig.supylabel('Green label', color='g')
plt.tight_layout()

enter image description here

Jose Manuel de Frutos
  • 1,040
  • 1
  • 7
  • 19
corinna
  • 629
  • 7
  • 18
  • Does this answer your question? [Shared secondary axes in matplotlib](https://stackoverflow.com/questions/53490960/shared-secondary-axes-in-matplotlib) – jared Jun 20 '23 at 16:25

1 Answers1

1

Considering the code in fig.supylabel I think there is no dedicated function and you will have to resort to the fig.text function (which is used internally by fig.supylabel).

You can reuse and adapt the defaults of the the fig.supylabel function.

Caveats are that you will not have autopositioning and you will have to adjust the rectangle for the tight_layout.

...

def supylabel2(fig, s, **kwargs):
    defaults = {
        "x": 0.98,
        "y": 0.5,
        "horizontalalignment": "center",
        "verticalalignment": "center",
        "rotation": "vertical",
        "rotation_mode": "anchor",
        "size": plt.rcParams["figure.labelsize"],  # matplotlib >= 3.6
        "weight": plt.rcParams["figure.labelweight"],  # matplotlib >= 3.6
    }
    kwargs["s"] = s
    # kwargs = defaults | kwargs  # python >= 3.9
    kwargs = {**defaults, **kwargs}
    fig.text(**kwargs)


fig.supylabel("Green label", color="g")
supylabel2(fig, "Blue label", color="b")

fig.tight_layout(rect=[0, 0, 0.96, 1])
plt.show()

Resulting plot

paime
  • 2,901
  • 1
  • 6
  • 17
  • Thank you! Too bad there seems to be no supylabel-based solution. But this will do. – corinna Jun 26 '23 at 10:17
  • Confirmation of answer withdrawn, because the code does not work: figure has no attributes labelsize and labelweight, and | does not work on dicts – corinna Jul 26 '23 at 12:37
  • You need [python 3.9 for dict union](https://docs.python.org/3/whatsnew/3.9.html#dictionary-merge-update-operators) (or change to `defaults.update(kwargs)`), and you need [matplotlib 3.6.0 for `figure.labelsize` and `figure.labelweight`](https://matplotlib.org/stable/users/prev_whats_new/whats_new_3.6.0.html#allow-setting-figure-label-size-and-weight-globally-and-separately-from-title) (or adapt given code). – paime Jul 27 '23 at 17:06
  • Rather `kwargs = {**defaults, **kwargs}` sorry – paime Jul 27 '23 at 17:52
  • Works now! Thank you! – corinna Jul 31 '23 at 10:12