Adding a third variable with hue

Introduzione alla visualizzazione dei dati con Seaborn

Content Team

DataCamp

Tips dataset

import pandas as pd
import seaborn as sns

tips = sns.load_dataset("tips")
tips.head()
   total_bill   tip     sex smoker  day    time  size
0       16.99  1.01  Female     No  Sun  Dinner     2
1       10.34  1.66    Male     No  Sun  Dinner     3
2       21.01  3.50    Male     No  Sun  Dinner     3
3       23.68  3.31    Male     No  Sun  Dinner     2
4       24.59  3.61  Female     No  Sun  Dinner     4
1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Introduzione alla visualizzazione dei dati con Seaborn

A basic scatter plot

import matplotlib.pyplot as plt
import seaborn as sns

sns.scatterplot(x="total_bill", 
                y="tip", 
                data=tips)

plt.show()

Scatter plot of total bill vs. tip amount

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Introduzione alla visualizzazione dei dati con Seaborn

A scatter plot with hue

import matplotlib.pyplot as plt
import seaborn as sns

sns.scatterplot(x="total_bill", 
                y="tip", 
                data=tips,
                hue="smoker")

plt.show()

Scatter plot with points colored by smoking status

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Introduzione alla visualizzazione dei dati con Seaborn

Setting hue order

import matplotlib.pyplot as plt
import seaborn as sns

sns.scatterplot(x="total_bill", 
                y="tip", 
                data=tips,
                hue="smoker",
                hue_order=["Yes", 
                           "No"])

plt.show()

Scatter plot with smokers before non-smokers in the legend

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Introduzione alla visualizzazione dei dati con Seaborn

Specifying hue colors

import matplotlib.pyplot as plt
import seaborn as sns

hue_colors = {"Yes": "black", "No": "red"}
sns.scatterplot(x="total_bill", y="tip", data=tips, hue="smoker", palette=hue_colors) plt.show()

Scatter plot with black and red hue colors

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Introduzione alla visualizzazione dei dati con Seaborn

Table of color names and hex codes

Introduzione alla visualizzazione dei dati con Seaborn

Using HTML hex color codes with hue

import matplotlib.pyplot as plt
import seaborn as sns

hue_colors = {"Yes": "#808080", 
              "No": "#00FF00"}

sns.scatterplot(x="total_bill", 
                y="tip", 
                data=tips,
                hue="smoker",
                palette=hue_colors)

plt.show()

Scatter plot with hex colors

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Introduzione alla visualizzazione dei dati con Seaborn

Using hue with count plots

import matplotlib.pyplot as plt
import seaborn as sns

sns.countplot(x="smoker", 
              data=tips, 
              hue="sex")

plt.show()

Count plot of smoking status with male versus female subgroups

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Introduzione alla visualizzazione dei dati con Seaborn

Let's practice!

Introduzione alla visualizzazione dei dati con Seaborn

Preparing Video For Download...