Plot countplot for two or more column on single plot

Question:

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.

Asked By: sukhwant prafullit

||

Answers:

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()

enter image description here

Equally you can interchange x and hue:

sns.countplot(x="value", hue="variable", data=pd.melt(df))

enter image description here

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.