# pip install seaborn
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import numpy as np
x = np.linspace(0, 14, 100) # 0~14 사이에 등간격으로 100개의 데이터가 들어가도록!
y1 = np.sin(x) # sin 그래프
y2 = 2*np.sin(x + 0.5) # y1 보다 진폭 2배, 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()
# 뒷 배경 바꿀 수 있음 (white / whitegrid 등)
sns.set_style('dark')
plt.figure(figsize=(10,6))
plt.plot(x,y1, x,y2, x,y3, x,y4)
# 필요없는 axis border 제거
sns.despine()
plt.show()
sns.set_style('whitegrid')
plt.figure(figsize=(10,6))
plt.plot(x,y1, x,y2, x,y3, x,y4)
sns.despine(offset=10) # offset x축, y축과의 거리 띄우기 옵션
plt.show()
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
sns.set_style("whitegrid")
%matplotlib inline
# tips : 요일별 점심, 저녁, 흡연 여부 식사 금액과 팁을 정리한 데이터
tips = sns.load_dataset('tips')
tips.head(5)
sns.set_style("whitegrid")
plt.figure(figsize=(8,6))
# total_bill 컬럼의 이상치 확인
sns.boxplot(x=tips["total_bill"])
plt.show()
plt.figure(figsize=(8,6))
sns.boxplot(x="day", y="total_bill", data=tips)
plt.show()
# 주말에 총 식사 금액이 높은 것을 알 수 있다
plt.figure(figsize=(8,6))
# hue : 그릅 지어줌
sns.boxplot(x="day", y="total_bill", hue="smoker", data=tips, palette="Set3")
plt.show()
plt.figure(figsize=(8,6))
# swarmplot은 stripplot과 비슷하지만 데이터를 나타내는 점이 겹치지 않도록 옆으로 이동
sns.swarmplot(x="day", y="total_bill", data=tips, color=".5")
plt.show()
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()
# 회귀분석하기
sns.set_style('darkgrid')
sns.lmplot(x='total_bill', y='tip', data=tips, size=7)
plt.show()
# hue 그룹별로 묶는 옵션
sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips, size=7)
plt.show()
# palette : 준비된 색상 사용
sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips, palette="Set1", size=7)
plt.show()
# 렌덤 데이터 뽑기
uniform_data = np.random.rand(10, 12)
uniform_data
sns.heatmap(uniform_data)
plt.show()
sns.heatmap(uniform_data, vmin=0, vmax=1) # vmin, vmax 색상 막대 범위
plt.show()
# 연도별 월병 항공기 승객수 flights 데이터 로드
flights = sns.load_dataset('flights')
flights.head(5)
# pivot 기능으로 간편하게 월별, 연도별로 구분할 수 있다.
flights = flights.pivot("month", "year", "passengers")
flights.head(5)
plt.figure(figsize=(10,8))
sns.heatmap(flights)
plt.show()
plt.figure(figsize=(10,8))
# annot=True argument :각 셀에 숫자 입력해(annotate each cell with numeric value)
# fmt='d' : 정수 형태(integer format)로 숫자 입력
sns.heatmap(flights, annot=True, fmt='d')
plt.show()
# 데이터 로드
sns.set(style='ticks') # ticks : grid 제거
iris = sns.load_dataset('iris')
iris.head(10)
# pairplot()
sns.pairplot(iris)
plt.show()
# hue 옵션 : 그룹별로 묶기
sns.pairplot(iris, hue='species')
plt.show()
# 컬럼별로 관계 분석할 수 있도록 그래프 그리기
sns.pairplot(iris, vars=['sepal_width', 'sepal_length'])
plt.show()
sns.pairplot(iris, x_vars=["sepal_width", "sepal_length"],
y_vars=["petal_width", "petal_length"])
plt.show()