Seaborn PairGrid with matplotlib.pyplot.hist and hue

2k views Asked by At

Plotting histogram on a seaborn PairGrid with hue leads to stacking by default. Is there a way to avoid this ? (stacked=False is inefficient.)

I tried with seaborn.distplot, kde=False but the bars are too wide in my case and decreasing rwidth kind of shifts the bars away from the corresponding variable values (which does not happen with plt.hist).

EDIT code to illustrate so-called 'shifting away from the corresponding variable values' (actually plt.hist does it too but it is less obvious).

import pandas as pd
import numpy as np    
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame()
for n in ['a', 'b']:
    tmp = pd.DataFrame({'name': [n] * 100,
                        'prior': [1, 10] * 50,
                        'post': [1, 10] * 50})
    df = df.append(tmp)

g = sns.PairGrid(df, hue='name', diag_sharey=False)
g.map_offdiag(sns.regplot, fit_reg=False, x_jitter=.1)
g.map_diag(plt.hist, rwidth=0.2, stacked=False)

g = sns.PairGrid(df, hue='name', diag_sharey=False)
g.map_offdiag(sns.regplot, fit_reg=False, x_jitter=.1)
g.map_diag(sns.distplot, kde=False, hist_kws={'rwidth':0.2})
0

There are 0 answers