Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Blog
- Visualization
- Google Analytics
- 텐서플로
- SQL
- Python
- 통계학
- MySQL
- 월간결산
- 리눅스
- MATLAB
- Pandas
- 딥러닝
- 시각화
- Linux
- 매틀랩
- 독후감
- Tistory
- 서평
- matplotlib
- 블로그
- 서평단
- 한빛미디어서평단
- 파이썬
- 파이썬 시각화
- python visualization
- tensorflow
- Ga
- 티스토리
- 한빛미디어
Archives
- Today
- Total
pbj0812의 코딩 일기
[PYTHON] interpolate 로 violinplot 구현하기 본문
0. 목표
- interpolate 로 violinplot 구현하기
1. 실습
1) library 호출
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.patches as patches
from scipy.interpolate import interp1d
import numpy as np
2) 데이터 로드
tips = sns.load_dataset("tips")
3) 구간별 그룹화
bins = list(range(-5, 65, 5))
tips['level'] = pd.cut(tips['total_bill'], bins, labels=bins[:-1])
df = tips[['total_bill', 'level']]
group = df.groupby(by = ['level']).count()
group.reset_index(inplace = True)

4) interpolate 를 이용한 보간
- 데이터 양을 늘려 스무스하게 데이터를 채우기
- 0 미만인 것은 0으로 바꾸기
xnew = np.linspace(list(group['level'])[0], list(group['level'])[-1], num=100, endpoint=True)
f = interp1d(group['level'], group['total_bill'], kind = 'quadratic')
ynew = f(xnew)
ynew = np.where(ynew < 0, 0, ynew)
5) 그림 그리기
fig, ax = plt.subplots()
fig.set_size_inches(15, 15)
ax.set_xticks([])
# 그래프 그리기
ax.plot(ynew, xnew, 'k')
ax.plot(-ynew, xnew, 'k')
# fill
ax.fill_between(ynew, xnew, -ynew, xnew, facecolor='green', interpolate=True)
ax.fill_between(-ynew, xnew, ynew, xnew, facecolor='green', interpolate=True)
# y축 꾸미기
ax.set_ylabel('total_bill', fontsize = 30)
ax.tick_params(axis = 'y', labelsize = 20)
ax.set_ylim([min(group['level']), max(group['level'])])
- 결과

2. 참고
'ComputerLanguage_Program > PYTHON' 카테고리의 다른 글
[PYTHON] floweaver 를 이용한 sankey 그래프 그리기 (0) | 2021.08.10 |
---|---|
[PYTHON] fill_between 을 이용한 신뢰구간을 포함한 lineplot 구현하기 (0) | 2021.08.06 |
[PYTHON] add_patch 를 이용한 violinplot 구현하기 (0) | 2021.08.04 |
[PYTHON] 상자 그림(box plot) 구현하기 (0) | 2021.08.03 |
[PYTHON] matplotlib 으로 FacetGrid 함수 구현하기 (0) | 2021.08.02 |
Comments