python(28) Seaborn (시각화 도구)

hyukstory 혁스토리·2020년 9월 4일
0

python

목록 보기
34/35

시각화 도구 Seaborn

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

# sin 함수 그려보기
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()


# seaborn 함수로 그래프 그리기
sns.set_style("white")
plt.figure(figsize=(10,6))
plt.plot(x,y1, x,y2, x,y3, x,y4)
sns.despine()
plt.show()


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



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


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



# < Seaborn 모듈 속 Tips 라는 데이터셋으로 연습해보기 >
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

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


# boxplot
sns.set_style("whitegrid")
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()


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", data=tips, color=".5")
plt.show()



# boxplot + swarmplot
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()


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


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





# < flights 데이터로 연습해보기 >
flights = sns.load_dataset("flights")
flights.head(5)

flights = flights.pivot("month", "year", "passengers") # pivot 으로 데이터 편집
flights.head(5)


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


plt.figure(figsize=(10,8))
sns.heatmap(flights, annot=True, fmt="d") # heatmap 에 숫자 기입
plt.show()



# < iris 데이터로 연습해보기 >
sns.set(style="ticks")
iris = sns.load_dataset("iris")
iris.head(10)


# pairplot
sns.pairplot(iris)
plt.show()

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

profile
문돌이의 고군분투 개발 공부

0개의 댓글