My dataFrame, df:
Sno | Attribute_1 | Attribute_2 | Attribute_3
__________________________________________________
1 | option_1 | option_3 |option_2
2 | option_1 | option_1 |option_1
3 | option_2 | option_2 |option_2
4 | option_1 | option_1 |option_3
5 | option_3 | option_2 |option_2
6 | option_3 | option_3 |option_1
7 | option_1 | option_3 |option_2
Here Attribute_1, Attribute_2 and Attribute_3 contains categorical data - option_1 or option_2 or option_3 for each of the rows.
I want to create a count plot on the same plot for all the attributes. I am able to do it for one column by:
sns.countplot(x="Attribute_1", data=df);
I can individually create for each of the attributes, but what I am looking for it that on the same plot I can have count plot for all the attributes. i.e X-axis will have attributes, and each attribute will have three count plot.
Seaborn usually works best with long form datasets. I.e. instead of 3 columns with different options for each attribute you would have two columns, one for the options and one for the attributes. This can easily be created via pd.melt
. Then the hue
value can be used on the "options" column:
sns.countplot(x="variable", hue="value", data=pd.melt(df))
Complete example:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
a= np.random.choice(["option_{}".format(i) for i in [1,2,3]], size=(12,3))
df = pd.DataFrame(a, columns=["Attribute_{}".format(i) for i in list("ABC")])
sns.countplot(x="variable", hue="value", data=pd.melt(df))
plt.show()
Equally you can interchange x
and hue
:
sns.countplot(x="value", hue="variable", data=pd.melt(df))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With