터칭 데이터
시각화 라이브러리 Seaborn 본문
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
4-1. 시각화 라이브러리, Seaborn¶
- Seaborn을 이용해서 데이터를 요약해서 보여주는 시각화를 진행해봅시다.
Seaborn 라이브러리¶
seaborn
은 파이썬의 데이터 시각화 라이브러리입니다. 이를 기반으로 수려한 그래프를 그릴 수 있습니다.
우선, 이를 사용하기 위해서 pip
를 사용해 다운로드를 받아봅시다.
Tip:
%
를 이용해서 노트북(.ipynb) 환경에서 터미널 코드를 실행할 수 있습니다.
%pip install seaborn
Requirement already satisfied: seaborn in d:\python\anaconda3\lib\site-packages (0.12.2)
Requirement already satisfied: numpy!=1.24.0,>=1.17 in d:\python\anaconda3\lib\site-packages (from seaborn) (1.23.5)
Requirement already satisfied: matplotlib!=3.6.1,>=3.1 in d:\python\anaconda3\lib\site-packages (from seaborn) (3.7.0)
Requirement already satisfied: pandas>=0.25 in d:\python\anaconda3\lib\site-packages (from seaborn) (1.5.3)
Requirement already satisfied: python-dateutil>=2.7 in d:\python\anaconda3\lib\site-packages (from matplotlib!=3.6.1,>=3.1->seaborn) (2.8.2)
Requirement already satisfied: pillow>=6.2.0 in d:\python\anaconda3\lib\site-packages (from matplotlib!=3.6.1,>=3.1->seaborn) (9.4.0)
Requirement already satisfied: pyparsing>=2.3.1 in d:\python\anaconda3\lib\site-packages (from matplotlib!=3.6.1,>=3.1->seaborn) (3.0.9)
Requirement already satisfied: contourpy>=1.0.1 in d:\python\anaconda3\lib\site-packages (from matplotlib!=3.6.1,>=3.1->seaborn) (1.0.5)
Requirement already satisfied: cycler>=0.10 in d:\python\anaconda3\lib\site-packages (from matplotlib!=3.6.1,>=3.1->seaborn) (0.11.0)
Requirement already satisfied: packaging>=20.0 in d:\python\anaconda3\lib\site-packages (from matplotlib!=3.6.1,>=3.1->seaborn) (22.0)
Requirement already satisfied: kiwisolver>=1.0.1 in d:\python\anaconda3\lib\site-packages (from matplotlib!=3.6.1,>=3.1->seaborn) (1.4.4)
Requirement already satisfied: fonttools>=4.22.0 in d:\python\anaconda3\lib\site-packages (from matplotlib!=3.6.1,>=3.1->seaborn) (4.25.0)
Requirement already satisfied: pytz>=2020.1 in d:\python\anaconda3\lib\site-packages (from pandas>=0.25->seaborn) (2022.7)
Requirement already satisfied: six>=1.5 in d:\python\anaconda3\lib\site-packages (from python-dateutil>=2.7->matplotlib!=3.6.1,>=3.1->seaborn) (1.16.0)
Note: you may need to restart the kernel to use updated packages.
설치를 다 진행했다면, 이제 seaborn
모듈을 사용할 준비가 되었습니다.
Seaborn Essentials¶
seaborn
은 다양한 그래프를 그릴 수 있습니다.
이 링크를 통해 어떤 그래프를 그릴 수 있는지 확인할 수 있습니다.
저희는 이중에서 자주 사용되는 꺾은선 그래프와 막대 그래프를 그려보면서 사용 방법을 익혀보도록 하겠습니다.
# 시각화에 필요한 라이브러리를 불러와봅시다.
import seaborn as sns
꺾은선 그래프(Line Plot)¶
두 변수의 값에 따른 추이를 선으로 이은 그래프입니다. .lineplot()
를 이용해서 이를 그릴 수 있습니다.
# Scatterplot을 직접 그려봅시다
# 값 x=[1, 3, 2, 4]
# 값 y=[0.7,0.2,0.1,0.05]
sns.lineplot(x=[1, 3, 2, 4], y=[0.7, 0.2, 0.1, 0.05])
<Axes: >
막대 그래프(Bar Plot)¶
범주형 데이터의 "값"과 그 값의 크기를 직사각형으로 나타낸 그림입니다..bar()
를 이용해서 이를 그릴 수 있습니다.
지난 실습에서 다룬 마우스 실습을 통해 로그인 창에 접속하는 것에 성공했는데요,
여기에 이제 키보드 입력을 넣어서 로그인을 완료해봅시다.
# Barplot을 직접 그려봅시다
# 범주 x=[1,2,3,4]
# 값 y=[0.7,0.2,0.1,0.05]
sns.barplot(x=[1, 2, 3, 4],y=[0.7, 0.2, 0.1, 0.05])
<Axes: >
Plot의 속성¶
seaborn
은 파이썬의 시각화 라이브러리 matplotlib
을 기반으로 만들어졌습니다.matplotlib.pyplot
의 속성을 변경해서 그래프에 다양한 요소를 변경/추가할 수 있습니다.
# matplotlib.pyplot을 불러와봅시다.
import matplotlib.pyplot as plt
plt.title()
: 그래프에 제목 을 추가합니다
# 제목을 추가해봅시다.
sns.barplot(x=[1, 2, 3, 4], y=[0.7, 0.2, 0.1, 0.05])
plt.title("Bar plot")
plt.show()
<Axes: >
Text(0.5, 1.0, 'Bar plot')
plt._label()
: 그래프의 축에 설명 을 추가합니다
# xlabel과 ylabel을 추가해봅시다.
sns.barplot(x=[1, 2, 3, 4], y=[0.7, 0.2, 0.1, 0.05])
plt.xlabel("X Label")
plt.ylabel("Y Label")
plt.show()
<Axes: >
Text(0.5, 0, 'X Label')
Text(0, 0.5, 'Y Label')
plt._lim()
: 그래프의 축의 범위 를 지정합니다.
# lineplot에서 ylim을 2~3으로 제한해봅시다.
sns.lineplot(x=[1, 3, 2, 4], y=[4, 3, 2, 1])
plt.ylim(0, 10)
plt.show()
<Axes: >
(0.0, 10.0)
plt.figure(figsize = (x, y))
: 그래프의 크기 를 지정합니다.
# 크기를 (20, 10)으로 지정해봅시다.
plt.figure(figsize=(20, 10)) # 크기 설정을 먼저 적어줘야 크기 변경이 적용된다.
sns.lineplot(x=[1, 3, 2, 4], y=[4, 3, 2, 1])
plt.show()
<Figure size 2000x1000 with 0 Axes>
<Axes: >
간단한 사용법을 확인했으니, 이제 이를 활용해서 스크래핑과 함께 사용해봅시다!
Tip: 이 외에도 다양한 그래프를 그릴 수 있습니다.
이 링크를 통해 더 자세히 살펴보실 수 있습니다. :)
'데이터 시각화(Data Visualization)' 카테고리의 다른 글
스크래핑 결과 시각화 - 해시코드 + 워드클라우드 (0) | 2023.10.27 |
---|---|
워드클라우드(Wordcloud) (0) | 2023.10.27 |
스크래핑 결과 시각화 - Hashcode (0) | 2023.10.27 |
스크래핑 결과 시각화 - 기상청 데이터 (0) | 2023.10.27 |