In [3]:
import plotly.plotly as py
import cufflinks as cf
import pandas as pd
import numpy as np
print(cf.__version__)
In [8]:
cf.go_offline()
In [14]:
cf.set_config_file(offline=True)
In [15]:
cf.get_config_file()
Out[15]:
In [9]:
import plotly.tools as tls
tls.embed('https://plot.ly/~cufflinks/8')
Out[9]:
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]:
In [25]:
fig=cf.datagen.histogram(3).figure(kind='histogram')
cf.iplot(fig)
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]:
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'])
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')
In [40]:
cf.datagen.lines(2).iplot(kind='spread',xTitle='Dates',yTitle='Return',title='Cufflinks - Spread 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')
In [44]:
cf.datagen.box(6).iplot(kind='box',xTitle='Stocks',yTitle='Returns Distribution',title='Cufflinks - Box Plot')
In [45]:
cf.datagen.histogram(2).iplot(kind='histogram',opacity=.75,title='Cufflinks - Histogram')
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')
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)
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)
In [49]:
cf.datagen.sinwave(10,.25).iplot(kind='surface',theme='solar',colorscale='brbg',title='Cufflinks - Surface Plot',
margin=(0,0,0,0))
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]:
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]:
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')
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]:
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')))
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)
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')
In [84]:
df = pd.DataFrame(np.random.randn(1000, 4), columns=['a', 'b', 'c', 'd'])
In [85]:
df.scatter_matrix()
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)
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 [ ]: