Seaborn

yeoni·2023년 5월 3일
0
post-custom-banner

기본 설정

import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import rc

plt.rcParams["axes.unicode_minus"] = False 
rc("font", family="Arial Unicode MS")
%matplotlib inline
#get_ipython().run_line_magic("matplotlib","inline")

예제1: seabron 기초

np.linspace(0, 14, 100)

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)

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

# sns.set_style: white, whitegrid, dark, darkgrid, ticks
# white
sns.set_style("white")
plt.figure(figsize=(10, 6))
plt.plot(x, y1, x, y2, x, y3, x, y4)
plt.show()

# dark
sns.set_style("dark")
plt.figure(figsize=(10, 6))
plt.plot(x, y1, x, y2, x, y3, x, y4)
plt.show()

# whitegrid
sns.set_style("whitegrid")
plt.figure(figsize=(10, 6))
plt.plot(x, y1, x, y2, x, y3, x, y4)
plt.show()

# darkgrid
sns.set_style("darkgrid")
plt.figure(figsize=(10, 6))
plt.plot(x, y1, x, y2, x, y3, x, y4)
plt.show()

예제2: seaborn tips data(boxplot, swarmplot, lmplot)

1. boxplot

  • hue: 카테고리 표시 옵션
  • palette option: 색 옵션(set1 ~ 3)
  • 박스플롯 이해하기: 참고1, 참고2
tips= sns.load_dataset("tips")

#boxplot
plt.figure(figsize=(10, 6))
sns.boxplot(x=tips["total_bill"])
plt.show()

#boxplot
plt.figure(figsize=(10, 6))
sns.boxplot(x="day", y="total_bill", data=tips)
plt.show()

#hue, palette option
plt.figure(figsize=(10, 6))
sns.boxplot(x="day", y="total_bill", data=tips, hue="smoker", palette="Set3")
plt.show()

2. swarmplot

  • color 옵션=0 ~ 1 → 검은색 ~ 흰색
#color
plt.figure(figsize=(10, 6))
sns.swarmplot(x="day", y="total_bill", data=tips, color="0.5")
plt.show()

#boxplot & swarmplot
plt.figure(figsize=(10, 6))
sns.boxplot(x="day", y="total_bill", data=tips)
sns.swarmplot(x="day", y="total_bill", data=tips, color="0.25")
plt.show()

3. lmplot

  • height: matplotlib에서 figsize와 같은 의미
#total_bill과 tip 사이의 관계 파악
sns.set_style("darkgrid")
sns.lmplot(x="total_bill", y="tip", data=tips, height=7)
plt.show()

#hue
sns.set_style("darkgrid")
sns.lmplot(x="total_bill", y="tip", data=tips, height=7, hue="smoker")
plt.show()

예제3: flight data(heatmap)

heatmap

  • annot:True → 숫자표현
  • fmt: 'd' → 정수표현
  • colormap
flights = sns.load_dataset("flights")

#pivot
flights = flights.pivot(index="month", columns="year", values="passengers")
flights.head()

#heatmap 
plt.figure(figsize=(10, 8))
sns.heatmap(data=flights, annot=True, fmt="d")
plt.show()

#colormap
plt.figure(figsize=(10, 8))
sns.heatmap(flights, annot=True, fmt="d", cmap="YlGnBu")
plt.show()

예제4: iris data(pariplot)

pariplot

iris = sns.load_dataset("iris")
iris.tail()

#모든 경우 표현
sns.set_style("ticks")
sns.pairplot(iris)
plt.show() 

#hue option
sns.pairplot(iris, hue="species")
plt.show()

#원하는 컬럼만 pairplot
sns.pairplot(iris, 
             x_vars=["sepal_width", "sepal_length"],
             y_vars=["petal_width", "petal_length"]
            )
plt.show()

예제5: anscombe data(lmplot)

lmplot

  • ci: 신뢰구간 선택
  • scatter_kws={"s":50} → 점 크기
  • order
  • outlier
  • robust
anscombe = sns.load_dataset("anscombe")

#data=anscombe.query("dataset == 'I'")-> 값이 같을 때를 의미
sns.set_style("darkgrid")
sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'I'"), ci=None, height=7)
plt.show()

#scatter_kws
sns.set_style("darkgrid")
sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'I'"), ci=None, scatter_kws={"s":50}, height=7)
plt.show()

#order
sns.set_style("darkgrid")
sns.lmplot(
    x="x", 
    y="y", 
    data=anscombe.query("dataset == 'II'"), 
    ci=None,
    order=2,
    height=7,
    scatter_kws={"s":80}
)
plt.show()

#outlier
sns.set_style("darkgrid")
sns.lmplot(
    x="x", 
    y="y", 
    data=anscombe.query("dataset == 'III'"), 
    ci=None,
    height=7,
    scatter_kws={"s":80}
)
plt.show()

!pip install statsmodels

#robust
sns.set_style("darkgrid")
sns.lmplot(
    x="x", 
    y="y", 
    data=anscombe.query("dataset == 'III'"),
    robust=True,
    ci=None,
    height=7,
    scatter_kws={"s":80}
)
plt.show()


Reference
1) 제로베이스 데이터스쿨 강의자료

profile
데이터 사이언스 / just do it
post-custom-banner

0개의 댓글