Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib - single axis label for multi-panel plot?

Tags:

matplotlib

I want to create a multi-panel plot:

import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax1.scatter(1,1)
ax2 = fig.add_subplot(2,1,2,sharex=ax1)
ax2.scatter(1,1)

and then create a separate axes object just for the label...

dummy = fig.add_subplot(1,1,1)
dummy.set_visible(False)
dummy.yaxis.set_label_text('y label')
dummy.yaxis.label.set_visible(True)

But does not work. I wonder why?

like image 570
hatmatrix Avatar asked Apr 16 '26 18:04

hatmatrix


1 Answers

When I had a similar problem yesterday, I just used the text() function to achieve the result. Using your example, this would read something like

import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax1.scatter(1,1)
ax2 = fig.add_subplot(2,1,2,sharex=ax1)
ax2.scatter(1,1)

ax = fig.add_axes( [0., 0., 1, 1] )
ax.set_axis_off()
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.text( 
    .05, 0.5, "Y Label", rotation='vertical',
    horizontalalignment='center', verticalalignment='center'
)

You might also want to use plt.subplots_adjust() to make space for the labels.

like image 89
David Zwicker Avatar answered Apr 22 '26 17:04

David Zwicker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!