Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: Plot countplot for two or more column on single plot

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.

like image 204
sukhwant prafullit Avatar asked Sep 14 '17 15:09

sukhwant prafullit


1 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

like image 198
ImportanceOfBeingErnest Avatar answered Nov 15 '22 07:11

ImportanceOfBeingErnest