Seaborn countplot with normalized y axis per group
<p>I was wondering if it is possible to create a Seaborn count plot, but instead of actual counts on the y-axis, show the relative frequency (percentage) within its group (as specified with the <code>hue</code> parameter).</p>

<p>I sort of fixed this with the following approach, but I can't imagine this is the easiest approach:</p>

<pre><code># Plot percentage of occupation per income class
grouped = df.groupby(['income'], sort=False)
occupation_counts = grouped['occupation'].value_counts(normalize=True, sort=False)

occupation_data = [
    {'occupation': occupation, 'income': income, 'percentage': percentage*100} for 
    (income, occupation), percentage in dict(occupation_counts).items()
]

df_occupation = pd.DataFrame(occupation_data)

p = sns.barplot(x="occupation", y="percentage", hue="income", data=df_occupation)
_ = plt.setp(p.get_xticklabels(), rotation=90)  # Rotate labels
</code></pre>

<p>Result:</p>

<p><a href="https://i.stack.imgur.com/feVbB.png" rel="noreferrer"><img src="https://i.stack.imgur.com/feVbB.png" alt="Percentage plot with seaborn"></a></p>

<p>I'm using the well known adult data set from the <a href="http://archive.ics.uci.edu/ml/datasets/Adult" rel="noreferrer">UCI machine learning repository</a>. The pandas dataframe is created like this:</p>

<pre><code># Read the adult dataset
df = pd.read_csv(
    "data/adult.data",
    engine='c',
    lineterminator='\n',

    names=['age', 'workclass', 'fnlwgt', 'education', 'education_num',
           'marital_status', 'occupation', 'relationship', 'race', 'sex',
           'capital_gain', 'capital_loss', 'hours_per_week',
           'native_country', 'income'],
    header=None,
    skipinitialspace=True,
    na_values="?"
)
</code></pre>

<p><a href="https://stackoverflow.com/questions/33179122/seaborn-countplot-with-frequencies">This question</a> is sort of related, but does not make use of the <code>hue</code> parameter. And in my case I cannot just change the labels on the y-axis, because the height of the bar must depend on the group.</p>