라일락 꽃이 피는 날

[Seaborn] matplotlib 그래프를 seaborn으로 그리기 본문

데이터 분석/Python

[Seaborn] matplotlib 그래프를 seaborn으로 그리기

eunki 2021. 5. 11. 15:58
728x90

seaborn

matplotlib을 기반으로 다양한 색상과 차트를 지원하는 라이브러리

컬러 팔레트를 이용한 아름다운 디자인과 쉬운 사용성 보유

seaborn에서만 제공되는 통계 기능 기반의 plot

pandas, matplotlib와 호환 가능

https://seaborn.pydata.org/

 

seaborn: statistical data visualization — seaborn 0.11.1 documentation

Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. For a brief introduction to the ideas behind the library, you can read the introductory note

seaborn.pydata.org

 

import seaborn as sns

 

 

 


1. Scatterplot

sizes 옵션에서는 사이즈의 min, max를 명시

hue 옵션에 비교할 그룹 명시

palette 옵션을 통해 seaborn이 제공하는 아름다운 palette를 이용

x = np.random.rand(50)
y = np.random.rand(50)

colors = np.arange(50)
area = x * y * 1000

 

 

 

 

2. Barplot, Barhplot

그래프를 임의로 그려야 하는 경우 → matplotlib

DataFrame을 가지고 그리는 경우 → seaborn

x = ['Math', 'Programming', 'Data Science', 'Art', 'English', 'Physics']
y = [66, 80, 60, 50, 80, 10]

 

 

 

 

기본 Barhplot 그리기

x와 y를 바꾸면 barhplot이 그려진다.

 

 

 

비교 그래프 그리기

hue 옵션으로 매우 쉽게 비교 barplot을 그릴 수 있다.

 

 

 

 

3. Line Plot

x = np.arange(0, 10, 0.1)
y = 1 + np.sin(x)

 

 

 

 

4. Histogram

N = 100000
bins = 30
x = np.random.randn(N)

 

 

 

kde = True 로 설정하면 Density가 Y축에 표기 된다.

vertical = True 로 설정하면 가로 그래프가 된다.

 

 

 

 

5. Box Plot

그래프를 임의로 그려야 하는 경우 → matplotlib

DataFrame을 가지고 그리는 경우 → seaborn

spread = np.random.rand(50) * 100
center = np.ones(25) * 50
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * -100
data = np.concatenate((spread, center, flier_high, flier_low))

 

 

 

 

다중 박스플롯 생성

hue 옵션으로 매우 쉽게 비교 boxplot을 그릴 수 있다.

 

728x90