jboy
jboy

Reputation: 150

Subplots with Seaborn misaligned

I'm trying to produce a heatmap and a barplot side by side using the same y-axis.

I've come quite far using matplotlib's subplots, but unfortunately the two graphs are misaligned. Whichever one I draw first ends up appearing a bit lower than the first.

%pylab inline

import pandas as pd
import seaborn

d = pd.DataFrame({'names': ['foo', 'bar', 'baz', 'spam', 'eggs'],
                  'v1': pd.np.random.power(.1, 5),
                  'v2': pd.np.random.normal(size=5)})

fig, axes = plt.subplots(nrows=1, ncols=2, sharey=True, squeeze=True)

fig.tight_layout(pad=0, h_pad=0, w_pad=0)

bar = seaborn.barplot(y='names', x='v1', data=d, ax=axes[1])
bar.set(ylabel='')
heat = seaborn.heatmap(d.set_index('names'), cbar=False, linewidths=0.1, ax=axes[0])

misaligned subplots

If I'd reversed the order of the two seaborn calls, the plot would look like this:

more misaligned subplots

I've tried just about everything, including fig.subplots_adjust, but I can't get the rows to line up properly.

Any suggestions?

Upvotes: 0

Views: 935

Answers (1)

jboy
jboy

Reputation: 150

mwaskom's suggestion to drop sharey helped me find a solution. Now I can produce properly aligned side-by-side plots that still effectively share the same y-axis using the following code:

%pylab inline

import pandas as pd
import seaborn

d = pd.DataFrame({'names': ['foo', 'bar', 'baz', 'spam', 'eggs'],
                  'v1': list(range(5)),
                  'v2': pd.np.random.normal(size=5)})

fig, axes = plt.subplots(nrows=1, ncols=2, squeeze=True)
fig.tight_layout(pad=0, h_pad=0, w_pad=0)
fig.subplots_adjust(wspace=0.1)

bar = seaborn.barplot(y='names', x='v1', data=d, ax=axes[1])
bar.set(ylabel='', yticks=[])
heat = seaborn.heatmap(d.set_index('names'), cbar=False, linewidths=0.1, ax=axes[0])

enter image description here

Upvotes: 1

Related Questions