(Package) Seaborn

์ž„๊ฒฝ๋ฏผยท2023๋…„ 10์›” 9์ผ
1
post-thumbnail
post-custom-banner

๐Ÿ”‘Summarization


  • Python package - Seaborn basic

๐Ÿ“—Contents


์„ค์น˜

pip install seaborn

Module Load

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

# matplotlib inline
get_ipython().run_line_magic('matplotlib', 'inline')

๊ธฐ๋ณธ ํ˜•ํƒœ

  • ๋ฐ์ดํ„ฐ ์ƒ์„ฑ
sns.set_style('white')
plt.figure(figsize = (10, 6))
plt.plot(x, y1, x, y2, x, y3, x, y4)
sns.despine()
plt.show()
  • Plot
x = np.linspace(0, 14, 100)
y1 = np.sin(x)
y2 = 2 * np.sin(x + 0.5)
y3 = 3 * np.sin(x + 1.0)
y4 = 4 * np.sin(x + 1.5)

Set style

plt.figure(figsize = (10, 6))
plt.plot(x, y1, x, y2, x, y3, x, y4)
plt.show()

Boxplot

data : seaborn ๋‚ด์žฅ ์‹ค์Šต ๋ฐ์ดํ„ฐ

tips = sns.load_dataset('tips')
tips.head()
plt.figure(figsize = (8, 6))
sns.boxplot(x = tips['total_bill'])
plt.show()

plt.figure(figsize = (8, 6))
sns.boxplot(x = 'day', y = 'total_bill', data = tips)
plt.show()

  • palette : ์ƒ‰์ƒ์ง€์ •
  • hue : ๋ฒ”๋ก€
plt.figure(figsize = (8, 6))
sns.boxplot(x = 'day', y = 'total_bill', hue = 'smoker', data = tips, palette= 'Set3')
plt.show()

swarmplot

plt.figure(figsize = (8, 6))
sns.swarmplot(x = 'day', y = 'total_bill', hue = 'smoker', data = tips)
plt.show()

lmplot

  • ํ๋ฆฐ ์˜์—ญ์ด ์ข์„์ˆ˜๋ก ๊ฐ•ํ•œ ์ƒ๊ด€๊ด€๊ณ„
sns.lmplot(x = 'total_bill', y = 'tip', data = tips)
plt.show()

sns.lmplot(x = 'total_bill', y = 'tip', hue = 'smoker', data = tips)
plt.show()

heatmap

Data

flights = sns.load_dataset('flights')
flights.head()

flights = flights.pivot(index = ['month'], columns=['year'], values=['passengers'])
flights.head()
  • annot : ๋‚ด๋ถ€์— ๊ฐ’ ๋„ฃ์„์ง€ ์—ฌ๋ถ€ # True : ๋„ฃ์–ด์คŒ
  • fmt : ํฌ๋งท # d : ์ •์ˆ˜ํ˜•
  • cmap : ์ปฌ๋Ÿฌ๋งต

plt.figure(figsize = (10, 6))
sns.heatmap(flights, annot = True, fmt = 'd')
plt.show()

plt.figure(figsize = (10, 6))
sns.heatmap(flights, annot = True, fmt = 'd', cmap = 'YlGnBu')
plt.show()

IRIS data

data

sns.set(style = 'ticks')
iris = sns.load_dataset('iris')
iris.head()

  • ํŠน์„ฑ๋ณ„ ์ƒ๊ด€๊ด€๊ณ„ ์ฆ‰์‹œ ํŒŒ์•…
sns.pairplot(iris)
plt.show()

sns.pairplot(iris, hue = 'species') # ํŠน์„ฑ๋ณ„ ์ƒ๊ด€๊ด€๊ณ„ ์ฆ‰์‹œ ํŒŒ์•…
plt.show()

  • ์›ํ•˜๋Š” ์ปฌ๋Ÿผ๋งŒ pairplot ๊ฐ€๋Šฅ
sns.pairplot(iris, x_vars = ['sepal_width', 'sepal_length'], y_vars = ['petal_width', 'petal_length'], hue = 'species') # ํŠน์„ฑ๋ณ„ ์ƒ๊ด€๊ด€๊ณ„ ์ฆ‰์‹œ ํŒŒ์•…
plt.show()

anscombe

Data

anscombe = sns.load_dataset('anscombe')
anscombe.head()

sns.set_style('darkgrid')
sns.lmplot(x = 'x', y='y', data = anscombe.query('dataset == "I"'), ci = None)

sns.set_style('darkgrid')
sns.lmplot(x = 'x', y='y', data = anscombe.query('dataset == "I"'), ci = None, scatter_kws={'s':80}) # scatter_kws : ๋งˆ์ปค์‚ฌ์ด์ฆˆ ๋ณ€๊ฒฝ

sns.set_style('darkgrid')
sns.lmplot(x = 'x', y='y', data = anscombe.query('dataset == "II"'), ci = None, order = 2, scatter_kws={'s':80}) # order : n์ฐจ์‹ ๋ณ€๊ฒฝ

sns.set_style('darkgrid')
sns.lmplot(x = 'x', y='y', data = anscombe.query('dataset == "III"'), ci = None, order = 2, scatter_kws={'s':80}) # order : n์ฐจ์‹ ๋ณ€๊ฒฝ

post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€