TOP
class="layout-aside-left paging-number">
본문 바로가기
데이터분석 만능열쇠 [파이썬]/<파이썬 기초>

[파이썬] 파이썬 시각화 도구 - Seaborn

by 기록자_Recordian 2024. 4. 15.
728x90
반응형
시작에 앞서
해당 내용은 <파이썬으로 데이터 주무르기> -민형기 저, BJPUBLIC 출판사 의 내용을 토대로 작성되었습니다.
보다 자세한 내용은 해당 교재를 확인하여 주시기 바랍니다.

파이썬 시각화 도구 기초 관련 글 모음

 

[파이썬] 지도 시각화 도구 - Folium

[파이썬] 파이썬 시각화 도구 - Seaborn

[파이썬] 파이썬 시각화 도구 - Matplotlib


Seaborn 을 통해 데이터 시각화 하기

 
Seaborn을 import 할 때는 matplotlib 도 같이 import 되어 있어야 함

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

matplotlib import 하여 그래프 그려보기

 
"whitegrid" Style 옵션 주기

white grid 옵션 주기

 
연습할 데이터셋 불러오기 (tips - 요일별 점심, 저녁, 흡연 여부 / 식사 금액과 팁 정리한 데이터)

데이터 셋 불러오기

 

박스플롯 만들어보기
plt.figure(figsize=(8,6))
sns.boxplot(x="day", y="total_bill", data=tips)  # x축은 요일, y축은 전체 금액
plt.show()

 

hue 옵션: 흡연 여부로 구분하기
plt.figure(figsize=(8,6))
sns.boxplot(x="day", y="total_bill", hue="smoker", data=tips, palette="Set3")
plt.show()

boxplot에서 hue로 구분하기

lmplot 그려보기
sns.set_style("darkgrid")
sns.lmplot(x="total_bill", y="tip", data=tips)
plt.show()

limplot 그리기

palette로 색상 지정

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

palette로 색상 지정


flights 데이터셋 불러오기
flights = sns.load_dataset("flights")
flights.head()

fliight 데이터를 피벗하기
flights = flights.pivot_table(index="month", columns="year", values="passengers")
flights.head(5)

 

heatmap 그리기

 

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

책에서 나온대로 썼더니,
'unknown format code 'd' for object of type 'float'' 이라는 에러 코드가 자꾸 떴다

 
구글링을 통해, 해당 먼저 flights 데이터를 숫자로 바꿔주고 flight_new 라는 데이터로 만든 후에 다시 했더니

flights_new = flights.round(0).astype(int)

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

맨 윗줄 수치는 나온다. (그런데 왜 두번째부터 나머지 줄까지는 안 나오는지 모르겠다.. 아시는 분 댓글 좀요ㅠ)
 


아이리스 꽃 데이터 셋 불러오기
sns.set(style="ticks")
iris = sns.load_dataset("iris")
iris.head(10)

꽃잎, 꽃받침의 너비와 폭을 가지고 종 구분하는 시각화하기
sns.pairplot(iris, hue="species")
plt.show()

 
 

728x90
반응형