2
votes

By default, tick-labels are aligned on the subplot axis they belong to. Is it possible to align the labels so they are centered between two subplots, instead?

import numpy as np
import matplotlib.pyplot as plt

data = [7, 2, 3, 0]
diff = [d - data[0] for d in data]
y = np.arange(len(data))

ax1 = plt.subplot(1, 2, 1)
ax1.barh(y, diff)
ax1.set_yticks(y + 0.4)
ax1.yaxis.set_major_formatter(matplotlib.ticker.NullFormatter())

ax2 = plt.subplot(1, 2, 2)
ax2.barh(y, data)
ax2.set_yticks(y + 0.4)
ax2.set_yticklabels(['reference', 'something', 'something else', 'nothing', ])

plt.tight_layout()
plt.show()

labels aligned to the right subplot

1

1 Answers

3
votes

Here is a working, but not very convenient way of doing so. You can provide a position keyword when setting the xticklabels. This allows you to use a negative offset in axes coordinates. If you set the position of the axes, and the spacing between them manually, you can calculate what this negative offset needs to be for the labels to be exactly in the center between the two axes.

Given your example data:

fig = plt.figure(figsize=(10, 2), facecolor='w')
fig.subplots_adjust(wspace=0.2)

ax1 = fig.add_axes([0.0, 0, 0.4, 1])
ax2 = fig.add_axes([0.6, 0, 0.4, 1])

ax1.barh(y, diff, align='center')
ax1.set_yticks(y)
ax1.yaxis.set_major_formatter(matplotlib.ticker.NullFormatter())

ax2.barh(y, data, align='center')
ax2.set_yticks(y)
ax2.set_yticklabels(['reference', 'something', 'something else', 'nothing', ], 
                    ha='center', position=(-0.25, 0))

enter image description here

The axes both have a width of 0.4 in figure coordinates, and they are spaced with 0.2. That means the labels would have to be at 0.5 in figure coordinates. Since the second axes starts at 0.6, it would need an offset in figure coordinates of -0.1. Unfortunately the position should be given in axes coordinates. The axes is 0.4 wide, so a quarter of the axes width is 0.1 in figure coordinates. That means specifying an offset of a negative quarter, -0.25, would place the labels right between the two axes. I hope that makes sense.....

Note that i have center the yticklabels with ha='center'. And also centered your bars, so you dont have to specify the offset anymore when setting the ticks.

edit:

You could do it automatically by reading the position of both axes.

def center_ylabels(ax1, ax2):

    pos2 = ax2.get_position()
    right = pos2.bounds[0]    

    pos1 = ax1.get_position()
    left = pos1.bounds[0] + pos1.bounds[2]

    offset = ((right - left) / pos2.bounds[2]) * -0.5

    for yt in ax2.get_yticklabels():        
        yt.set_position((offset, yt.get_position()[1]))
        yt.set_ha('center')

        plt.setp(ax2.yaxis.get_major_ticks(), pad=0)

fig, (ax1, ax2) = plt.subplots(1,2, figsize=(10,2))
fig.subplots_adjust(wspace=0.5)

ax1.barh(y, diff, align='center')
ax1.set_yticks(y)
ax1.yaxis.set_major_formatter(matplotlib.ticker.NullFormatter())

ax2.barh(y, data, align='center')
ax2.set_yticks(y)
ax2.set_yticklabels(['reference', 'something', 'something else', 'nothing'])

center_ylabels(ax1, ax2)

enter image description here