In [3]:
import plotly.plotly as py
import cufflinks as cf
import pandas as pd
import numpy as np
print(cf.__version__)


0.16

In [8]:
cf.go_offline()


글로벌 세팅

  • cf.set_config_file(offline=True)로 글로벌 설정을 할 수 있음

In [14]:
cf.set_config_file(offline=True)



In [15]:
cf.get_config_file()


Out[15]:
{'sharing': 'public',
 'theme': 'pearl',
 'colorscale': 'dflt',
 'offline': True,
 'offline_connected': True,
 'offline_url': '',
 'offline_show_link': True,
 'offline_link_text': 'Export to plot.ly',
 'datagen_mode': 'stocks',
 'dimensions': None,
 'margin': None,
 'offline_config': None}

Embedding


In [9]:
import plotly.tools as tls
tls.embed('https://plot.ly/~cufflinks/8')


Out[9]:

Box


In [16]:
cf.datagen.box(20).iplot(kind='box',legend=False)



In [22]:
cf.datagen.heatmap(10).iplot(kind='heatmap', legend=False)



In [11]:
df=cf.datagen.ohlc()
qf=cf.QuantFig(df,title='First Quant Figure',legend='top',name='GS')
qf.add_bollinger_bands()
qf.iplot()



In [24]:
df.head(5)


Out[24]:
UXY.XV XNU.CK OQR.GA UYC.VP VUP.GZ
2015-01-01 0.800771 -0.595017 0.125753 2.018796 0.700048
2015-01-02 -2.203117 -0.888146 0.037225 -0.135266 0.191668
2015-01-03 -3.451056 -1.230666 -1.528871 -1.184026 0.864135
2015-01-04 -3.883996 0.123842 -1.445799 -1.634540 0.852598
2015-01-05 -1.716885 -0.273773 -0.488687 -1.154332 0.636731

In [25]:
fig=cf.datagen.histogram(3).figure(kind='histogram')
cf.iplot(fig)


Line Charts


In [26]:
cf.datagen.lines().iplot(kind='scatter',xTitle='Dates',yTitle='Returns',title='Cufflinks - Line Chart')



In [28]:
cf.datagen.lines().head(5)


Out[28]:
QNO.EB LZC.PZ SLI.BJ HHK.DK UZJ.RW
2015-01-01 1.963811 -0.500526 0.291181 -0.815214 -0.287477
2015-01-02 2.222367 -1.137508 -0.549882 -0.129508 -1.547647
2015-01-03 0.640915 -1.244220 -0.331274 -0.042036 -0.956817
2015-01-04 1.030053 -0.901940 -1.672653 0.939636 -0.264577
2015-01-05 1.683832 -2.557083 -1.619537 1.491630 1.507606

In [33]:
cf.datagen.lines(3).iplot(kind='scatter',xTitle='Dates',yTitle='Returns',title='Cufflinks - Filled Line Chart',
                         colorscale='-blues',fill=True)



In [37]:
cf.datagen.lines(1).iplot(kind='scatter',xTitle='Dates',yTitle='Returns',title='Cufflinks - Besfit Line Chart',
                         filename='Cufflinks - Bestfit Line Chart',bestfit=True,colors=['blue'],
                         bestfit_colors=['pink'])


/usr/local/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning:

Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.

Scatter


In [38]:
cf.datagen.lines(2).iplot(kind='scatter',mode='markers',size=10,symbol='x',colorscale='paired',
                          xTitle='Dates',yTitle='EPS Growth',title='Cufflinks - Scatter Chart')


Spread Chart


In [40]:
cf.datagen.lines(2).iplot(kind='spread',xTitle='Dates',yTitle='Return',title='Cufflinks - Spread Chart')


Bar Chart


In [41]:
cf.datagen.lines(5).resample('M').mean().iplot(kind='bar',xTitle='Dates',yTitle='Return',title='Cufflinks - Bar Chart')



In [43]:
cf.datagen.lines(5).resample('M').mean().iplot(kind='bar',xTitle='Dates',yTitle='Return',title='Cufflinks - Grouped Bar Chart',
                          barmode='stack')


Box plot


In [44]:
cf.datagen.box(6).iplot(kind='box',xTitle='Stocks',yTitle='Returns Distribution',title='Cufflinks - Box Plot')


Histogram


In [45]:
cf.datagen.histogram(2).iplot(kind='histogram',opacity=.75,title='Cufflinks - Histogram')


Bubble Chart


In [46]:
cf.datagen.bubble(prefix='industry').iplot(kind='bubble',x='x',y='y',size='size',categories='categories',text='text',
                          xTitle='Returns',yTitle='Analyst Score',title='Cufflinks - Bubble Chart')


Scatter 3d


In [47]:
cf.datagen.scatter3d(2,150,mode='stocks').iplot(kind='scatter3d',x='x',y='y',z='z',size=15,categories='categories',text='text',
                             title='Cufflinks - Scatter 3D Chart',colors=['blue','pink'],width=0.5,margin=(0,0,0,0),
                             opacity=1)


Bubble 3d


In [48]:
cf.datagen.bubble3d(5,4,mode='stocks').iplot(kind='bubble3d',x='x',y='y',z='z',size='size',text='text',categories='categories',
                            title='Cufflinks - Bubble 3D Chart',colorscale='set1',
                            width=.5,opacity=.9)


Surface


In [49]:
cf.datagen.sinwave(10,.25).iplot(kind='surface',theme='solar',colorscale='brbg',title='Cufflinks - Surface Plot',
                                 margin=(0,0,0,0))


/usr/local/lib/python3.7/site-packages/cufflinks/datagen.py:380: RuntimeWarning:

invalid value encountered in true_divide

Color

Quantfig


In [50]:
df=cf.datagen.ohlcv()

In [51]:
qf=cf.QuantFig(df,title='First Quant Figure',legend='top',name='GS')
qf.add_bollinger_bands()
qf.iplot()



In [55]:
df=cf.datagen.lines(4,1000)
df.iplot()



In [56]:
df.head(5)


Out[56]:
QDC.OJ WSG.HA FTM.NB MTZ.MO
2015-01-01 0.186106 1.914718 -0.724818 -1.963038
2015-01-02 -2.340264 3.093468 0.706157 -3.453458
2015-01-03 -4.663880 4.034122 -0.540965 -5.108953
2015-01-04 -3.280572 4.381400 -0.731827 -4.050399
2015-01-05 -3.361790 2.688939 -1.473761 -4.662075

In [57]:
df3 = pd.DataFrame(np.random.randn(1000, 2), columns=['B', 'C']).cumsum()
df3['A'] = pd.Series(list(range(len(df3))))
df3.iplot(x='A', y='B')



In [58]:
df3.head(3)


Out[58]:
B C A
0 0.085600 -0.814234 0
1 -1.167779 -1.514877 1
2 -3.794677 -0.828077 2

In [59]:
df.iloc[3].iplot(kind='bar',bargap=.5)



In [60]:
df=pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df.iplot(kind='bar')



In [61]:
df.iplot(kind='bar',barmode='stack')



In [63]:
df.iplot(kind='barh',barmode='stack',bargap=.1)



In [64]:
df = pd.DataFrame({'a': np.random.randn(1000) + 1, 'b': np.random.randn(1000),
                    'c': np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])

In [65]:
df.iplot(kind='histogram')



In [66]:
df.iplot(kind='histogram',barmode='stack',bins=20)



In [67]:
df.iplot(kind='histogram',columns=['a'],orientation='h',histnorm='probability')



In [68]:
df_h=cf.datagen.histogram(4)
df_h.iplot(kind='histogram',subplots=True,bins=50)



In [70]:
df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
df.iplot(kind='box')


Grouping


In [71]:
df = pd.DataFrame(np.random.rand(10,2), columns=['Col1', 'Col2'] )
df['X'] = pd.Series(['A','A','A','A','A','B','B','B','B','B'])

In [72]:
df.head(5)


Out[72]:
Col1 Col2 X
0 0.325079 0.992380 A
1 0.904001 0.015011 A
2 0.731923 0.339355 A
3 0.925808 0.658968 A
4 0.208733 0.318850 A

In [73]:
figs=[df[df['X']==d][['Col1','Col2']].iplot(kind='box',asFigure=True) for d in pd.unique(df['X']) ]

In [74]:
cf.iplot(cf.subplots(figs))



In [75]:
def by(df,category):
    l=[]
    for cat in pd.unique(df[category]):
        _df=df[df[category]==cat]
        del _df[category]
        _df=_df.rename(columns=dict([(k,'{0}_{1}'.format(cat,k)) for k in _df.columns]))
        l.append(_df.iplot(kind='box',asFigure=True))
    return l

In [76]:
cf.iplot(cf.subplots(by(df,'X')))


Area Plots


In [77]:
df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])

In [78]:
df.iplot(kind='area',fill=True,opacity=1)



In [79]:
df.iplot(fill=True)


Scatter Plot


In [80]:
df = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])

In [81]:
df.iplot(kind='scatter',x='a',y='b',mode='markers')



In [82]:
df.iplot(kind='scatter',mode='markers',symbol='circle-dot',colors=['orange','teal','blue','yellow'],size=10)



In [83]:
df.iplot(kind='bubble',x='a',y='b',size='c')


Scatter Matrix


In [84]:
df = pd.DataFrame(np.random.randn(1000, 4), columns=['a', 'b', 'c', 'd'])

In [85]:
df.scatter_matrix()


Subplots


In [86]:
df=cf.datagen.lines(4)

In [87]:
df.iplot(subplots=True,shape=(4,1),shared_xaxes=True,vertical_spacing=.02,fill=True)



In [88]:
df.iplot(subplots=True,subplot_titles=True,legend=False)



In [89]:
df=cf.datagen.bubble(10,50,mode='stocks')

In [90]:
figs=cf.figures(df,[dict(kind='histogram',keys='x',color='blue'),
                    dict(kind='scatter',mode='markers',x='x',y='y',size=5),
                    dict(kind='scatter',mode='markers',x='x',y='y',size=5,color='teal')],asList=True)
figs.append(cf.datagen.lines(1).figure(bestfit=True,colors=['blue'],bestfit_colors=['pink']))
base_layout=cf.tools.get_base_layout(figs)
sp=cf.subplots(figs,shape=(3,2),base_layout=base_layout,vertical_spacing=.15,horizontal_spacing=.03,
               specs=[[{'rowspan':2},{}],[None,{}],[{'colspan':2},None]],
               subplot_titles=['Histogram','Scatter 1','Scatter 2','Bestfit Line'])
sp['layout'].update(showlegend=False)

In [91]:
cf.iplot(sp)


Shapes


In [92]:
df=cf.datagen.lines(3,columns=['a','b','c'])

In [93]:
df.iplot(hline=[2,4],vline=['2015-02-10'])



In [94]:
df.iplot(hline=[dict(y=-1,color='blue',width=3),dict(y=1,color='pink',dash='dash')])



In [95]:
df.iplot(hspan=[(-1,1),(2,5)])



In [96]:
df.iplot(vspan={'x0':'2015-02-15','x1':'2015-03-15','color':'teal','fill':True,'opacity':.4})



In [97]:
# Plotting resistance lines
max_vals=df.max().values.tolist()
resistance=[dict(kind='line',y=i,color=j,width=2) for i,j in zip(max_vals,['red','blue','pink'])]
df.iplot(hline=resistance)



In [98]:
df_a=df['a']
max_val=df_a.max()
min_val=df_a.min()
max_date=df_a[df_a==max_val].index[0].strftime('%Y-%m-%d')
min_date=df_a[df_a==min_val].index[0].strftime('%Y-%m-%d')
shape1=dict(kind='line',x0=max_date,y0=max_val,x1=min_date,y1=min_val,color='blue',width=2)
shape2=dict(kind='rect',x0=max_date,x1=min_date,fill=True,color='gray',opacity=.3)

In [99]:
df_a.iplot(shapes=[shape1,shape2])



In [100]:
x0 = np.random.normal(2, 0.45, 300)
y0 = np.random.normal(2, 0.45, 300)

x1 = np.random.normal(6, 0.4, 200)
y1 = np.random.normal(6, 0.4, 200)

x2 = np.random.normal(4, 0.3, 200)
y2 = np.random.normal(4, 0.3, 200)

distributions = [(x0,y0),(x1,y1),(x2,y2)]

In [101]:
dfs=[pd.DataFrame(dict(x=i,y=j)) for i,j in distributions]

In [102]:
d=[]
gen=cf.colorgen(scale='ggplot')

In [103]:
for df in dfs:
    d_=df.figure(kind='scatter',mode='markers',x='x',y='y',size=5,colors=gen)['data']
    for _ in d_:
        d.append(_)

In [104]:
gen=cf.colorgen(scale='ggplot')
shapes=[cf.tools.get_shape(kind='circle',x0=min(x),x1=max(x),
         y0=min(y),y1=max(y),color=next(gen),fill=True,
         opacity=.3,width=.4) for x,y in distributions]

In [105]:
fig=dict(data=d)
fig['layout']=cf.getLayout(shapes=shapes,legend=False,title='Distribution Comparison')
cf.iplot(fig,validate=False)



In [ ]: