학습 자료는 " 처음하는 파이썬 데이터 분석[전처리, pandas, 시각화까지 전과정 기본 기술 쉽게 익히기] (Dave Lee, 인프런) " 를 바탕으로 작성했습니다¶
1. EDA를 위한 데이터 시각화¶
시각화 라이브러리
- matplotlib, seaborn, plotly (핫한 라이브러리)
matplotlib은 오래된 전통적인 라이브러리
- 최신 시각화 라이브러리 : plotly
- pandas + plotly를 조합해서 최신/가장 빠르게 시각화 가능
- pandas df.plotly(), 형태로 그래프를 바로 그릴 수 있음
- https://plotly.com/python/
2. iplot() 으로 시각화 사용하기¶
- 데이터프레임.iplot(kind=그래프종류) 만으로 그래프를 그릴 수 있으므로, 매우 쉬움
- 단, 관련 자료가 부족하고, 세부 기능 조정에 한계가 있음
!pip install plotly chart_studio --upgrade !jupyter notebook에서 바로 실행
!pip install chufflinks --upgrade
- cufflink
- pandas로 그래프를 그리기 위한 기능을 plotly와 연결시키기 위한 라이브러리
In [1]:
# 라이브러리 로드
import chart_studio.plotly as py
import cufflinks as cf
cf.go_offline(connected = True)
In [2]:
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.rand(10,2), columns = ['A','B'])
# np.random.rand(n, m) m개 열에 n개의 난수 생성
df.head()
Out[2]:
A | B | |
---|---|---|
0 | 0.489108 | 0.508586 |
1 | 0.713648 | 0.566870 |
2 | 0.783082 | 0.259393 |
3 | 0.207452 | 0.600358 |
4 | 0.803935 | 0.288099 |
In [3]:
df.iplot(kind = 'bar')
# 사용법 잘 모르겠으면, cf.help('bar')
In [4]:
# matplotlib 통해 그래프 생성해보기
# !pip install matplotlib # matplotlib 설치
df.plot(kind = 'bar')
Out[4]:
<AxesSubplot:>
In [5]:
df.iplot(kind = 'bar', barmode = 'stack', orientation = 'h')
# bar chart, stackmode, 방향은 수평으로 그리기
In [6]:
# iplot으로 선그래프 그리기 (Scatter)
df.iplot(kind = 'scatter', mode = 'lines + markers', fill = True)
# plot 종류는 scatter
# 그래프 mode는 선 + 마커
# 그래프 아래 색칠하기
In [7]:
# 그래프 테마 변경하기
theme = cf.getThemes()
print(theme)
for theme_item in theme:
df.iplot(kind = 'scatter',
theme = theme_item,
fill = True,
xTitle = 'x title',
yTitle = 'y title',
# layout = layout,
title = theme_item
)
['ggplot', 'pearl', 'solar', 'space', 'white', 'polar', 'henanigans']
그래프 주요 세부항목 변경¶
In [8]:
# 그래프 layout (폰트, title, ticks 지정)
# layout은 dict 타입으로 선언
layout = {
'title' :
{
'text' : '<b>Test Graph<b>', # <b>를 text 시작과 끝에 써서 boldic으로
'font' : {
'family' : 'consolas',
'size' : 20,
'color' : '#01579B'
}, # dict 형태니까 key, vale 매칭 후 comma(,) 써주기
'x' : 0.5, # 가장 좌측이 0, 가장 우측이 1
'y' : 0.88 # 가장 하단이 0, 가장 상단이 1
},
'plot_bgcolor': '#FAFAFA',
'xaxis': {
'showticklabels':True,
'dtick': 2, # tick 기입 간격
'title': {
'text':'X axis',
'font': {
'size': 10,
'color': '#37474F'
}
}
},
'yaxis': { # x 축 범례?
'showticklabels':True,
'dtick': 0.1,
'title': {
'text':'Y axis',
'font': {
'size': 10,
'color': '#37474F'
}
}
}
}
df.iplot(kind = 'scatter', mode = 'lines + markers', layout = layout)
In [ ]:
'데이터분석 > Pandas' 카테고리의 다른 글
[Pandas] 데이터 Feature 파악 기본 (그래프) (0) | 2021.07.23 |
---|---|
[Pandas] plotly 사용해서 시각화 해보기 2 (0) | 2021.07.18 |
[Pandas] 데이터 처리 연습2 결과물 시각화 (0) | 2021.07.17 |
[Pandas] 데이터 처리 연습 2 (0) | 2021.07.17 |
[Pandas] 데이터 처리 연습 (0) | 2021.07.15 |