Menambah variabel ketiga dengan hue

Pengantar Visualisasi Data dengan Seaborn

Content Team

DataCamp

Dataset 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/
Pengantar Visualisasi Data dengan Seaborn

Scatter plot dasar

import matplotlib.pyplot as plt
import seaborn as sns

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

plt.show()

Scatter plot total tagihan vs. jumlah tip

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Pengantar Visualisasi Data dengan Seaborn

Scatter plot dengan 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 dengan titik diwarnai berdasarkan status merokok

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Pengantar Visualisasi Data dengan Seaborn

Mengatur urutan 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 dengan perokok sebelum non-perokok di legenda

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Pengantar Visualisasi Data dengan Seaborn

Menentukan warna 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 dengan warna hue hitam dan merah

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Pengantar Visualisasi Data dengan Seaborn

Tabel nama warna dan kode hex

Pengantar Visualisasi Data dengan Seaborn

Menggunakan kode warna heksadesimal HTML dengan 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 dengan warna hex

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Pengantar Visualisasi Data dengan Seaborn

Menggunakan hue pada count plot

import matplotlib.pyplot as plt
import seaborn as sns

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

plt.show()

Count plot status merokok dengan subkelompok pria vs wanita

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Pengantar Visualisasi Data dengan Seaborn

Ayo berlatih!

Pengantar Visualisasi Data dengan Seaborn

Preparing Video For Download...