DEV Community

Dendi Handian
Dendi Handian

Posted on

3 2

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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay