Plotting Multiple boolean cols in seaborn

Solution 1:

Seaborn's works easiest with a dataframe in "long form". Pandas' melt() can combine the columns. Using kind='count', seaborn does the counting. To have names for the 0s and 1s in the legend, .replace() can change them to strings. Matplotlib's new bar_label function labels the bars with their value.

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

df = pd.DataFrame({'Moroso': np.random.randint(0, 2, 100),
                   'Morosoant': np.random.randint(0, 2, 100)})
df_long = df.melt(value_vars=['Moroso', 'Morosoant'], value_name='Legend').replace({'Legend': {0: 'No', 1: 'Yes'}})
sns.set_style('whitegrid')
g = sns.catplot(kind='count', data=df_long,
                x='variable', hue='Legend', palette='Set1', height=4, aspect=2)
g.set(xlabel='')
for ax in g.axes.flat:
    for bars in ax.containers:
        ax.bar_label(bars)
plt.subplots_adjust(left=0.07, bottom=0.15)
plt.show()

sns.catplot with kind='count'

PS: If df looks like:

   Moroso  Morosoant
0       0          1
1       1          1
2       1          0
3       1          1
4       0          1

Then df_long (before the replace) will look like:

    variable  Legend
0     Moroso       0
1     Moroso       1
2     Moroso       1
3     Moroso       1
4     Moroso       0
5  Morosoant       1
6  Morosoant       1
7  Morosoant       0
8  Morosoant       1
9  Morosoant       1