This is an archival dump of old wiki content --- see scipy.org for current material.
Please see http://scipy-cookbook.readthedocs.org/

Using a single axis label to annotate multiple subplot axes

When using multiple subplots with the same axis units, it is redundant to label each axis individually, and makes the graph overly complex. You can use a single axis label, centered in the plot frame, to label multiple subplot axes. Here is how to do it:

```   1 # note that this a code fragment...you will have to define your own data to plot
2 # Set up a whole-figure axes, with invisible axis, ticks, and ticklabels,
3 # which we use to get the xlabel and ylabel in the right place
4 bigAxes = pylab.axes(frameon=False)     # hide frame
5 pylab.xticks([])                        # don't want to see any ticks on this axis
6 pylab.yticks([])
7 # I'm using TeX for typesetting the labels--not necessary
8 pylab.ylabel(r'\textbf{Surface Concentration \$(nmol/m^2)\$}', size='medium')
9 pylab.xlabel(r'\textbf{Time (hours)}', size='medium')
10 # Create subplots and shift them up and to the right to keep tick labels
11 # from overlapping the axis labels defined above
12 topSubplot = pylab.subplot(2,1,1)
13 position = topSubplot.get_position()
14 position[0] = 0.15
15 position[1] = position[1] + 0.01
16 topSubplot.set_position(position)
17 pylab.errorbar(times150, average150)
18 bottomSubplot = pylab.subplot(2,1,2)
19 position = bottomSubplot.get_position()
20 position[0] = 0.15
21 position[1] = position[1] + 0.03
22 bottomSubplot.set_position(position)
23 pylab.errorbar(times300, average300)
```

Alternatively, you can use the following snippet to have shared ylabels on your subplots. Also see the attached figure output.

```   1 import pylab
2
3 figprops = dict(figsize=(8., 8. / 1.618), dpi=128)                                          # Figure properties
4 adjustprops = dict(left=0.1, bottom=0.1, right=0.97, top=0.93, wspace=0.2 hspace=0.2)       # Subplot properties
5
6 fig = pylab.figure(**figprops)                                                              # New figure
7 fig.subplots_adjust(**adjustprops)                                                          # Tunes the subplot layout
8
9 ax = fig.add_subplot(3, 1, 1)
10 bx = fig.add_subplot(3, 1, 2, sharex=ax, sharey=ax)
11 cx = fig.add_subplot(3, 1, 3, sharex=ax, sharey=ax)
12
13 ax.plot([0,1,2], [2,3,4], 'k-')
14 bx.plot([0,1,2], [2,3,4], 'k-')
15 cx.plot([0,1,2], [2,3,4], 'k-')
16
17 pylab.setp(ax.get_xticklabels(), visible=False)
18 pylab.setp(bx.get_xticklabels(), visible=False)
19
20 bx.set_ylabel('This is a long label shared among more axes', fontsize=14)
21 cx.set_xlabel('And a shared x label', fontsize=14)
```

Thanks to Sebastian Krieger from matplotlib-users list for this trick.

Simple function to get rid of superfluous xticks but retain the ones on the bottom (works in pylab). Combine it with the above snippets to get a nice plot without too much redundance:

```   1 def rem_x():
2     '''Removes superfluous x ticks when multiple subplots  share
3     their axis works only in pylab mode but can easily be rewritten
4     for api use'''
5     nr_ax=len(gcf().get_axes())
6     count=0
7     for z in gcf().get_axes():
8         if count == nr_ax-1: break
9             setp(z.get_xticklabels(),visible=False)
10             count+=1
```

The first one above doesn't work for me. The subplot command overwrites the bigaxes. However, I found a much simpler solution to do a decent job for two axes and one ylabel:

yyl=plt.ylabel(r'My longish label that I want vertically centred')

yyl.set_position((yyl.get_position()[0],1)) # This says use the top of the bottom axis as the reference point.

yyl.set_verticalalignment('center')

SciPy: Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label (last edited 2015-10-24 17:48:26 by anonymous)