Drawing a scatter plot using seaborn

The function scatterplot() from the Python Visualization library Seaborn accepts parameters to distinguish subsets within the data through color, style and the size of the markers.

Seaborn Updated 13 Jun 2020 2 min read

Overview

  • A scatter plot illustrates if there is any relationship between two variables.
  • While both seaborn and matplotlib can be used to draw a scatter plot, the seaborn visualization is rich in terms of highlighting the subsets of the data points.
  • The Seaborn data visualisation framework provides the function scatterplot() to draw a scatter plot. A basic scatter plot can be drawn using the scatter() function of the matplotlib library as well.
  • The scatterplot() function from seaborn has parameters to distinguish datapoints using color(hue semantics), style and the size of the markers.

Example: A basic scatter plot using seaborn

python
# Example Python program that plots a scatter plot

            # using the seaborn visualization library

            import pandas as pds

            import seaborn as sns

            import matplotlib.pyplot as plt

sns.set(style="whitegrid")

            tempAndRain = {"temperature": [60, 65, 70, 75, 80, 85, 90, 95, 100],

                           "rainfall":    [20, 22, 35, 30, 25, 37, 39, 20, 25]};

df = pds.DataFrame(data=tempAndRain);

            print(df);

sns.scatterplot(x="temperature", y="rainfall", data=df);

            plt.show();

Output

Drawing a basic scatter plot using Python visualization library - seaborn

Hue semantics for numeric Data

The color of the scatter plot markers can be used to distinguish subsets of the data using
the hue parameter. In the above example Python code, changing of the scatterplot() function invocation with the hue parameter makes the scatterplot to appear as below.

Code Change

python
sns.scatterplot(x="temperature", y="rainfall", data=df, hue="rainfall");

Output

Scatter plot drawn using seaborn with hue parameter used to distinguish numeric data

Using the style parameter to distinguish the data

The marker style in the scatter plot can be used to further distinguish the data points. The example code below uses the style parameter to differentiate between night time data vs date time data.

Code Change

python
sns.scatterplot(x="temperature", y="rainfall", data=df, hue="rainfall", style="dayornight");

Output

A scatter plot drawn using seaborn using the hue and style parameters