plotly는 분석/시각화의 라이브러리 이름이다. 그래프가 다른 시각화 라이브러리 대비 이쁘고 웹 상에 publish 해서 interactive visualization 용도로 사용하는데 매우 훌륭하다고 한다.
특히 plotly dash 는 웹 기반 분석 어플리케이션 개발을 위한 오픈소스 파이썬 프레임워크인데 자바스크립트를 사용하지 않고도 데이터 사이언티스트가 파이썬 코드 몇 백줄로 디자이너의 도움없이 매우 이쁘고 완성도 높은 interactive analytics web application 을 짧은 시간안에 만들 수 있어서 매우 매력적이라고 한다.
사용방법은 크게 iplot, plotly.express, plotly.graph_objects 가 있다.
< plotly를 오프라인에서 jupyter notebook 에서 사용하여 시각화 하기 >
1. 설치 또는 업그레이드를 한다.
plotly를 처음 사용한다면 설치를 하고 자주 버전이 업그레이드가 되므로 이미 설치가 되어있다면 업그레이드를 진행한다.
•
설치 (프롬프트 창에서)
$ pip install plotly # idle 에서
$ conda install -c plotly # anaconda 프롬프트에서 설치
Python
복사
•
plotly 를 오프라인에서 사용하기 위한 라이브러리 import 및 환경 설정
# import plotly standard
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as ff
# cufflinks wrapper on plotly
import cufflinks as cf
# display all cell outputs
from IPython.core.interactiveshell import interactiveshell
# plotly + cufflinks in offline mode
from plotly.offline import iplot
cf.go_offline()
# set the global theme
cf.set_config_file(world_readable=True, theme='pearl', offline=True)
Python
복사
iplot
# cf.help() 를 통해 어떤 그래프를 그릴 수 있는지 확인한다.
Use 'cufflinks.help(figure)' to see the list of available parameters for the given figure.
Use 'DataFrame.iplot(kind=figure)' to plot the respective figure
Figures:
bar
box
bubble
bubble3d
candle
choroplet
distplot
heatmap
histogram
ohlc
pie
ratio
scatter
scatter3d
scattergeo
spread
surface
violin
Python
복사
•
barplot
df = pd.DataFrame(np.random.rand(10, 4), columns=['A', 'B', 'C', 'D'])
df.iplot(kind='bar')
# barmode - stacked bar chart를 표현할 수 있다
df.iplot(kind='bar', barmode='stack')
# 시리즈도 차트로 그릴 수 있다.
df['A'].iplot(kind='bar')
# orientation - 차트가 그려지는 축을 변경할 수 있다('v', 'h')
df.iplot(kind='bar',
barmode='stack',
orientation='h')
Python
복사
•
box plot
df.iplot(kind='box')
Python
복사
•
line chart
df.iplot(kind='line')
Python
복사
•
bubble
# 머신러닝용 데이터 저장소의 'wine' 데이터를 이용해보자.
# https://archive.ics.uci.edu/ml/index.php
wine = pd.read_csv('../../data/winequality-red.csv',sep=':')
wine.groupby('quality').mean()
wine.groupby('quality').mean().iplot(kind='bubble',
size='pH',
x='fixed acidity',
y='volatile acidity')
Python
복사
•
bubble3d
wine.groupby('quality').mean().iplot(kind='bubble3d',
size='chlorides',
x='fixed acidity',
y='volatile acidity',
z='citric acid')
Python
복사
•
area chart
df.iplot(kind='area',
fill=True)
df.iplot(kind='scatter',
fill=True)
# scatter with mode
df.iplot(kind='scatter',
mode='lines+markers+text')
Python
복사
•
themes
themes = cf.getThemes()
themes
['ggplot', 'pearl', 'solar', 'space', 'white', 'polar', 'henanigans']
for theme_item in themes:
df.iplot(kind='scatter',
theme=theme_item,
fill=True)
Python
복사
•
layout
css 를 작성하는 것과 유사하다.
Python
복사