Wednesday, December 25, 2024
Google search engine
HomeLanguagesHow to Make Countplot or barplot with Seaborn Catplot?

How to Make Countplot or barplot with Seaborn Catplot?

In Python, we can normally plot barcharts for numerical variables. But when it comes to the case of categorical variables then we cannot normally plot the count for each category. Here comes Seaborn Catplot in the picture. It allows you to plot the count of each category for non-numerical/categorical variables.

Creating the CountplotĀ 

Countplot gives a graphical visual for the count of observations in each category using bars. This cannot be used for quantitative variables. It can be created by passing the count value to the kind parameter.

Example: Letā€™s take an example of a titanic dataset.

Python3




# import libraries
import matplotlib.pyplot as plt
import seaborn as sns
Ā Ā 
# setting background style
sns.set_style('darkgrid')
Ā Ā 
# import dataset
data = sns.load_dataset('titanic')
data.head()


Output:

Example 1: Creating the countplot

Python3




# plot for count of passengers belonging
# to each gender
sns.catplot(x='sex', kind='count', data=data)
plt.xlabel("Gender")
plt.ylabel("Count")


Output:

Example 2: Creating the grouped countplot

Python3




# Grouped Countplot/Barplot
# Count of passengers who survived
# or didn't of each gender
sns.catplot(x='sex', hue='survived',
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā kind='count', data=data)
Ā Ā 
plt.xlabel("Gender")
plt.ylabel("Count")


Output:

Example 3: Creating the horizontal countplot

Python3




# Plotting horizontally
sns.catplot(y='sex', hue='survived',Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā kind='count', data=data)
Ā Ā 
plt.xlabel("Count")
plt.ylabel("Gender")


Output:

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?