SciPy: Using GridSpec to Display Box Plots Under Histograms

Here's how to get output like this using matplotlib and its GridSpec class:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gs

%matplotlib inline

df = pd.DataFrame([
        np.random.random_integers(1, 2, 1000)
df.columns = ["value", "group"]

fig = plt.figure(figsize=(10, 6))
grid = gs.GridSpec(2, 2, height_ratios=[3, 1]) 
for i, g in enumerate([1, 2]):
    subset = df['value'][df['group'] == g]
    # Histogram
    ax = plt.subplot(grid[i])
    ax.hist(list(subset), color='k', alpha=0.4)
    ax.set_title("Group %s" % g)
    # Box plot
    ax2 = plt.subplot(grid[i+2])
    pd.DataFrame(subset).boxplot(vert=False, return_type='axes')

fig.suptitle('Example', fontsize=20)

None # Don't display the last thing -- `%matplotlib inline` will display the graphs no matter what.

It looks like Gist embed is broken here on PostHaven for IPython notebooks, so the embed below may look strange: