Skip to content

Pythoneo

Online How to Python stuff

How to Make a Countplot in Seaborn

Posted on April 21, 2022June 1, 2022 By Luke K

Let’s check how to make a countplot in Seaborn. I’ll load taxis built-in data to show you the countplot in detail.
sns basic countplot

The full seaborn.countplot consist of several parameters:

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

I will share my knowledge to explain to you you parameters of the countplot Seaborn method. It will help you to create a move advanced Seaborn countplot.

This is the completed code to create a counterplot in Python using the Seaborn module.

import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('taxis')

sns.countplot(x='passengers', data=df)
plt.show()

sns basic countplot

Countplot ratation

To flip the chart, just change the x argument to y.

sns.countplot(y='passengers', data=df)

sns y countplot

You can also use a orient parameter and set “v” for a vertical alignment or “h” for a horizontal one.

See also  How to create bar chart in matplotlib?

Countplot hue

To increase the amount of data in the chart you may use the hue parameter. It is adding an additional sets of data to your counterplot graph. I decided to visualize payments with color of the taxi to check the relationship between them.

sns.countplot(x='payment', data=df, hue='color')

sns countplot hue

Countplot hue_order

The hue_order parameter gives you the possibility of changing the order of a hue. I used simply df[‘color’].value_counts().index[::-1] structure to change the order by index in the reverse order.

sns.countplot(x='payment', data=df, hue='color',
              hue_order = df['color'].value_counts().index[::-1])

sns countplot hue order

Countplot order

To sort the data series use the order parameter. I picked df[‘passengers’].value_counts().index to sort counts.

sns.countplot(x='passengers', data=df, 
              order=df['passengers'].value_counts().index)

sns countplot order

Countplot ascending order

To sort the data series in ascending order, add the reverse indexing using index[::-1].

sns.countplot(x='passengers', data=df, 
              order=df['passengers'].value_counts().index[::-1])

sns countplot order ascending

Countplot color

It is also possible to change the color of your python countplot. I set the color parameter to blue.

sns.countplot(x='passengers', data=df, color='blue')

sns countplot color blue

Countplot palette

If one color is not enough for you or you want to make your chart look more attractive, use the palette parameter. I dediced to use the inferno.

sns.countplot(x='passengers', data=df, palette='inferno')

sns countplot palette inforno

Countplot saturation

For those who care about details, you can also change the color saturation. The default is 0.75. Watch the saturation change as you lower it to 0.2.

sns.countplot(x='passengers', data=df, saturation=0.2)

sns countplot saturation

Countplot dodge

To stack data in a countplot, use a dodge. By default, dodge is set to true. If you change this parameter to false, the count graph will change like this. The data has been accumulated.

sns.countplot(x='payment', data=df, hue='color', dodge=False)

sns countplot dodge

These are all the parameters of the countplot method that I wanted to discuss for you. I hope you will improve your charting skills at Seaborn with this python course.

See also  How to create Seaborn Heatmap?
Seaborn Tags:chart, plot

Post navigation

Previous Post: AttributeError: partially initialized module ‘cv2’ has no attribute ‘img’ (most likely due to a circular import)
Next Post: How to calculate the factorial of an array in Numpy?

Categories

  • bokeh (1)
  • datetime (3)
  • Django (1)
  • glob (1)
  • io (1)
  • json (1)
  • math (4)
  • matplotlib (9)
  • numpy (84)
  • OpenCV (1)
  • os (3)
  • Pandas (2)
  • pathlib (2)
  • Pillow (3)
  • Plotly (1)
  • Python (25)
  • random (7)
  • requests (1)
  • Scipy (4)
  • Seaborn (3)
  • shutil (1)
  • sqlite3 (1)
  • statistics (16)
  • sys (1)
  • Tkinter (9)
  • turtle (2)
  • urllib (1)
  • webbrowser (1)

RSS RSS

  • How to use curdoc in Bokeh
  • How to solve TypeError: ‘set’ object is not subscriptable
  • How to calculate the factorial of an array in Numpy?
  • How to Make a Countplot in Seaborn
  • AttributeError: partially initialized module ‘cv2’ has no attribute ‘img’ (most likely due to a circular import)
  • How to enumerate dictionary in Python?
  • How to compare two arrays in Numpy?
  • How to square a matrix in Numpy?
  • Exploding out slices of a Pie Chart in Plotly
  • How to uninstall Numpy?

Tags

arithmetic mean array axis button calculations chart conversion copy count counter data type dictionary dimension draw error files fill float generate grid GUI image index integer list matrix max mean median min normal distribution plot random reshape rotate round size standard deviation string sum test text time variance zero

Copyright © 2023 Pythoneo.

Powered by PressBook WordPress theme

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Cookie settingsACCEPT
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT