Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated to the data structures from pandas.
seaborn.countplot() method
seaborn.countplot() method is used to Show the counts of observations in each categorical bin using bars.
Syntax : seaborn.countplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, dodge=True, ax=None, **kwargs)
Parameters : This method is accepting the following parameters that are described below:
- x, y: This parameter take names of variables in data or vector data, optional, Inputs for plotting long-form data.
- hue : (optional) This parameter take column name for colour encoding.
- data : (optional) This parameter take DataFrame, array, or list of arrays, Dataset for plotting. If x and y are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form.
- order, hue_order : (optional) This parameter take lists of strings. Order to plot the categorical levels in, otherwise the levels are inferred from the data objects.
- orient : (optional)This parameter take “v” | “h”, Orientation of the plot (vertical or horizontal). This is usually inferred from the dtype of the input variables but can be used to specify when the “categorical” variable is a numeric or when plotting wide-form data.
- color : (optional) This parameter take matplotlib color, Color for all of the elements, or seed for a gradient palette.
- palette : (optional) This parameter take palette name, list, or dict, Colors to use for the different levels of the hue variable. Should be something that can be interpreted by color_palette(), or a dictionary mapping hue levels to matplotlib colors.
- saturation : (optional) This parameter take float value, Proportion of the original saturation to draw colors at. Large patches often look better with slightly desaturated colors, but set this to 1 if you want the plot colors to perfectly match the input color spec.
- dodge : (optional) This parameter take bool value, When hue nesting is used, whether elements should be shifted along the categorical axis.
- ax : (optional) This parameter take matplotlib Axes, Axes object to draw the plot onto, otherwise uses the current Axes.
- kwargs : This parameter take key, value mappings, Other keyword arguments are passed through to matplotlib.axes.Axes.bar().
Returns: Returns the Axes object with the plot drawn onto it.
Below examples illustrate the countplot() method of the seaborn library.
Example 1 : Show value counts for a single categorical variable.
# importing the required library import seaborn as sns import matplotlib.pyplot as plt # read a tips.csv file from seaborn libraray df = sns.load_dataset('tips') # count plot on single categorical variable sns.countplot(x ='sex', data = df) # Show the plot plt.show() |
Output :

Example 2 : Show value counts for two categorical variables.
# importing the required library import seaborn as sns import matplotlib.pyplot as plt # read a tips.csv file from seaborn libraray df = sns.load_dataset('tips') # count plot on two categorical variable sns.countplot(x ='sex', hue = "smoker", data = df) # Show the plot plt.show() |
Output :

Example 3 : Plot the bars horizontally.
# importing the required library import seaborn as sns import matplotlib.pyplot as plt # read a tips.csv file from seaborn libraray df = sns.load_dataset('tips') # count plot along y axis sns.countplot(y ='sex', hue = "smoker", data = df) # Show the plot plt.show() |
Output :

Example 4 : Use a different color palette.
# importing the required library import seaborn as sns import matplotlib.pyplot as plt # read a tips.csv file from seaborn libraray df = sns.load_dataset('tips') # use a different colour palette in count plot sns.countplot(x ='sex', data = df, palette = "Set2") # Show the plot plt.show() |
Output :

Recommended Posts:
- Introduction to Seaborn - Python
- Violinplot using Seaborn in Python
- Scatterplot using Seaborn in Python
- Barplot using seaborn in Python
- Swarmplot using Seaborn in Python
- Boxplot using Seaborn in Python
- Lineplot using Seaborn in Python
- Python - seaborn.FacetGrid() method
- Plotting graph using Seaborn | Python
- Seaborn.barplot() method in Python
- Python - seaborn.pairplot() method
- Python - seaborn.lmplot() method
- Grid Plot in Python using Seaborn
- Python - seaborn.factorplot() method
- Python - seaborn.PairGrid() method
- Python | Titanic Data EDA using Seaborn
- Python - seaborn.pointplot() method
- Python - seaborn.regplot() method
- Python - seaborn.jointplot() method
- seaborn.lineplot() method in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

