hueで3つ目の変数を追加する

Seaborn を使ったデータ可視化入門

Content Team

DataCamp

tipsデータセット

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/
Seaborn を使ったデータ可視化入門

基本的な散布図

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/
Seaborn を使ったデータ可視化入門

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/
Seaborn を使ったデータ可視化入門

hueの順序を設定する

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/
Seaborn を使ったデータ可視化入門

hueの色を指定する

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/
Seaborn を使ったデータ可視化入門

Table of color names and hex codes

Seaborn を使ったデータ可視化入門

hueでHTMLの16進カラーコードを使用する

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/
Seaborn を使ったデータ可視化入門

カウントプロットでhueを使用する

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/
Seaborn を使ったデータ可視化入門

練習しましょう!

Seaborn を使ったデータ可視化入門

Preparing Video For Download...