Seaborn Scatterplot
Dendi Handian
Posted on September 30, 2021
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
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)
horizontal = (1, 2, 3, 4, 5, 2, 4, 3)
vertical = (2, 4, 6, 8, 10, 6, 4, 9)
sns.scatterplot(x=horizontal, y=vertical)
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)
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)
Any of the above code will display the same plot like this:
Styling
Dots Color
Using Web Color Name:
sns.scatterplot(x='horizontal', y='vertical', data=df, c=['red'])
or Using Hex
sns.scatterplot(x='horizontal', y='vertical', data=df, c=['#c21b95'])
Dots Transparency
sns.scatterplot(
x='horizontal',
y='vertical',
data=df,
c=['#c21b95'],
alpha=0.4
)
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'])
💖 💪 🙅 🚩
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.