Seaborn Scatterplot

dendihandian

Dendi Handian

Posted on September 30, 2021

Seaborn Scatterplot

Prerequisite and How to Code Along

It's recommended to use Google Colab or Jupyter Notebook or Jupyter Lab in Anaconda.

Importing The Libraries

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
Enter fullscreen mode Exit fullscreen mode

Loading Data

We will try to load data from various types like list, tuple, dict or dataframe.

Loading Data From List or Tuple

horizontal = [1, 2, 3, 4, 5, 2, 4, 3]
vertical = [2, 4, 6, 8, 10, 6, 4, 9]

sns.scatterplot(x=horizontal, y=vertical)
Enter fullscreen mode Exit fullscreen mode
horizontal = (1, 2, 3, 4, 5, 2, 4, 3)
vertical = (2, 4, 6, 8, 10, 6, 4, 9)

sns.scatterplot(x=horizontal, y=vertical)
Enter fullscreen mode Exit fullscreen mode

Loading Data From Dictionary

dict_data = {
    'horizontal': (1, 2, 3, 4, 5, 2, 4, 3),
    'vertical': (2, 4, 6, 8, 10, 6, 4, 9),
}

sns.scatterplot(x='horizontal', y='vertical', data=dict_data)
Enter fullscreen mode Exit fullscreen mode

Loading Data From Pandas DataFrame

dict_data = {
    'horizontal': [1, 2, 3, 4, 5, 2, 4, 3],
    'vertical': [2, 4, 6, 8, 10, 6, 4, 9],
}

df = pd.DataFrame(dict_data)

sns.scatterplot(x='horizontal', y='vertical', data=df)
Enter fullscreen mode Exit fullscreen mode

Any of the above code will display the same plot like this:
Alt Text

Styling

Dots Color

Using Web Color Name:

sns.scatterplot(x='horizontal', y='vertical', data=df, c=['red'])
Enter fullscreen mode Exit fullscreen mode

or Using Hex

sns.scatterplot(x='horizontal', y='vertical', data=df, c=['#c21b95'])
Enter fullscreen mode Exit fullscreen mode

Alt Text

Dots Transparency

sns.scatterplot(
    x='horizontal',
    y='vertical',
    data=df,
    c=['#c21b95'],
    alpha=0.4
)
Enter fullscreen mode Exit fullscreen mode

Alt Text

Hue

We can classify each dot depends on another feature. We add new feature named class that has category name for each dots.

df = pd.DataFrame({
    'horizontal': [1, 2, 3, 4, 5, 2, 4, 3],
    'vertical': [2, 4, 6, 8, 10, 6, 4, 9],
    'class': ['category 2', 'category 1', 'category 3', 'category 2', 'category 3', 'category 1', 'category 3', 'category 2']
})

sns.scatterplot(x='horizontal', y='vertical', data=df, hue='class', hue_order=['category 3', 'category 1', 'category 2'])
Enter fullscreen mode Exit fullscreen mode

Alt Text

💖 💪 🙅 🚩
dendihandian
Dendi Handian

Posted on September 30, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Seaborn Scatterplot
seaborn Seaborn Scatterplot

September 30, 2021