본문 바로가기
Python/라이브러리

편리한 시각화 도구 Seaborn

by 지식광부키우기 2019. 9. 19.

아래의 모든 내용은 파이썬으로 데이터 주무르기(저자 민형기)의 예시를 사용했습니다.

 

 

▶ 몇 개의 사인 함수 그리기 

import matplotlib.pyplot as plt
%matplotlib inline

import seaborn as sns

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()

<결과1>

 

▶ Seaborn의 white 스타일 지원 

sns.set_style("white")

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

sns.despine()

plt.show()

<결과2>

 

 

 

▶ Seaborn의 dark 스타일 지원 

sns.set_style("dark")

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

<결과3>

 

▶ Seaborn의 whitegrid 스타일 지원 

sns.set_style("whitegrid")

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

<결과4>

 

▶ Seaborn의 despine옵션, 축을 살짝 벌린다 

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

sns.despine(offset=10)

plt.show()

<결과5>

 

▶ Seaborn 내 연습할 만한 데이터, Tips 데이터 셋 불러오기 

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

sns.set_style("whitegrid")
%matplotlib inline
tips = sns.load_dataset("tips")
tips.head(5)

<결과6>

 

 

 

▶ Seaborn boxplot 그리기

sns.set_style("whitegrid")

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

<결과7>

 

▶ Seaborn boxplot 그리기 2

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

<결과8>

 

▶ Seaborn hue 옵션 이용해 구분하기, 흡연 여부 구별 

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

<결과9>

 

▶ Seaborn swarmplot 그리기

plt.figure(figsize=(8,6))
sns.swarmplot(x="day", y="total_bill", data=tips, color=".5")
plt.show()

<결과10>

 

▶ Seaborn swarmplot, boxplot 동시에 그리기

plt.figure(figsize=(8,6))
sns.boxplot(x="day", y="total_bill", data=tips)
sns.swarmplot(x="day", y="total_bill", data=tips, color=".25")
plt.show()

<결과11>

 

▶ Seaborn lmplot 그리기 

# 데이터를 scatter처럼 그리고 직선으로 regression한 그림도 같이 그려주고 유효범위 ci로 잡아줌
sns.set_style("darkgrid")
sns.lmplot(x="total_bill", y="tip", data=tips, size=7)
plt.show()

<결과12>

 

 

▶ Seaborn lmplot hue 옵션 추가하기 

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

<결과13>

 

▶ Seaborn lmplot palette로 색상지 정하기

sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips, palette="Set1", size=7)
plt.show()

<결과14>

 

▶ Numpy 데이터 생성

uniform_data = np.random.rand(10, 12)
uniform_data

<결과15>

 

▶ Seaborn heatmap 도구 사용

sns.heatmap(uniform_data)
plt.show()

<결과16>

 

▶ Seaborn heatmap bar 편집

sns.heatmap(uniform_data, vmin=0, vmax=1)
plt.show()

<결과17>

 

▶ Seaborn flights 데이터 불러오기

flights = sns.load_dataset("flights")
flights.head(5)

<결과18>

 

▶ Pivot 기능으로 월별, 연도별 구별하기 

flights = flights.pivot("month", "year", "passengers")
flights.head(5)

<결과19>

 

▶ Seaborn heatmap으로 적용하기

plt.figure(figsize=(10,8))
sns.heatmap(flights)
plt.show()

<결과20>

 

▶ Seaborn heatmap annot, fmt 추가 옵션 적용하기

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

<결과21>

 

▶ Seaborn style ticks로 iris데이터 불러오기

sns.set(style="ticks")
iris = sns.load_dataset("iris")
iris.head(10)

<결과22>

 

▶ Seaborn pairplot 그리기

sns.pairplot(iris)
plt.show()

<그림23>

 

▶ Seaborn pairplot hue 옵션 추가

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

<그림24>

 

▶ Seaborn pairplot 몇 가지 변수만 적용하기

sns.pairplot(iris, vars=["sepal_width", "sepal_length"])
plt.show()

<그림25>

 

▶ Seaborn pairplot 변수 구체적으로 적용하기

sns.pairplot(iris, x_vars=["sepal_width", "sepal_length"], 
             y_vars=["petal_width", "petal_length"])
plt.show()

<그림26>

 

▶ Seaborn anscombe 데이터 불러오기

anscombe = sns.load_dataset("anscombe")
anscombe.head(5)

<그림27>

 

▶ Seaborn lmplot 적용하기

sns.set_style("darkgrid")

sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'I'"),  ci=None, size=7)
plt.show()

<그림28>

 

▶ Seaborn lmplot scatter_kws 옵션으로 포인트 키우기

sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'I'"),
           ci=None, scatter_kws={"s": 80}, size=7)
plt.show()

<그림29>

 

 

▶ Seaborn lmplot 데이터셋 변경 적용하기

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

<그림30>

 

▶ Seaborn lmplot order 옵션 변경 적용하기

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

<그림31>

 

▶ Seaborn lmplot 데이터 변경 3

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

<그림32>

 

▶ Seaborn lmplot robust 옵션 추가

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

<그림33>

 

여기까지가 Seaborn에 대한 간략한 예시였습니다. 감사합니다.

'Python > 라이브러리' 카테고리의 다른 글

Pandas 고급  (0) 2019.09.25
파이썬의 대표 시각화 도구 Matplotlib  (0) 2019.09.17
Pandas 기초 익히기  (0) 2019.09.16

댓글