#!/usr/bin/env python # coding: utf-8 # ##Cufflinks # # This library binds the power of [plotly](http://www.plot.ly) with the flexibility of [pandas](http://pandas.pydata.org/) for easy plotting. # # This library is available on https://github.com/santosjorge/cufflinks # # This tutorial assumes that the plotly user credentials have already been configured as stated on the [getting started](https://plot.ly/python/getting-started/) guide. # In[1]: import pandas as pd import cufflinks as cf import numpy as np # In[2]: get_ipython().run_line_magic('reload_ext', 'autoreload') get_ipython().run_line_magic('autoreload', '2') # We make all charts public and set a global theme # In[3]: cf.set_config_file(world_readable=True,theme='pearl') # We create a set of timeseries # In[26]: df=pd.DataFrame(np.random.randn(100,5),index=pd.date_range('1/1/15',periods=100), columns=['IBM','MSFT','GOOG','VERZ','APPL']) df=df.cumsum() # **iplot** can be used on any DataFrame to plot on a plotly chart. # If no filename is specified then a generic *Plotly Playground* file is created. # # All the charts are created as private by default. To make them public you can use **world_readable=True** # # Let's look at the avilable parameters # In[5]: help(df.iplot) # In[27]: df.iplot(filename='Tutorial 1') # ### Customizing Themes # # We can pass a **theme** to the **iplot** function. # 3 themes are available, but you can create your own # * Solar # * Pearl (Default) # * White # In[28]: df[['APPL','IBM','VERZ']].iplot(theme='white',filename='Tutorial White') # We can also pass common metadata for the chart # In[29]: df.iplot(theme='pearl',filename='Tutorial Metadata',title='Stock Returns',xTitle='Dates',yTitle='Returns') # ### Bestfit Lines # # We can easily add a bestfit line to any Series # # This will automatically add a best fit approximation and the equation as the legend. # In[30]: df['IBM'].iplot(filename='IBM Returns',bestfit=True) # ### Customizing Colors # # We can pass any color (either by Hex, RGB or Text *) # # *Text values are specified in the cufflinks.colors modules # In[31]: df['IBM'].iplot(filename='IBM Returns - colors',bestfit=True,colors=['pink'],bestfit_colors=['blue']) # ### Filled Traces # # We can add a fill to a trace with **fill=True** # In[32]: df['IBM'].iplot(filename='Tutorial Microsoft',fill=True,colors=['green']) # ### Bar Charts # # We can easily create a bar chart with the parameter **kind** # In[33]: df.sum().iplot(kind='bar',filename='Tutorial Barchart') # Bars can also be stacked by a given dimension # In[34]: df.resample('M').iplot(kind='bar',barmode='stacked',filename='Tutorial Bar Stacked') # ### Spread and Ratio charts # # We can also create spread and ratio charts on the fly with **kind='spread'** and **kind='ratio'** # In[35]: df[['VERZ','IBM']].iplot(filename='Tutorial Spread',kind='spread') # In[55]: (df[['GOOG','MSFT']]+20).iplot(filename='Tutorial Ratio',kind='ratio',colors=['green','red']) # ### Annotations # # Annotations can be added to the chart and these are automatically positioned correctly. # # **Annotations** should be specified in a dictionary form # In[56]: annotations={'2015-01-15':'Dividends','2015-03-31':'Split Announced'} df['MSFT'].iplot(filename='Tutorial Annotations',annotations=annotations) # ### Output as Image # # The output of a chart can be in an image mode as well. # # For this we can use **asImage=True** # # We can also set the dimensions (optional) with **dimensions=(width,height)** # In[61]: df[['VERZ','MSFT']].iplot(filename='Tutorial Image',theme='white',colors=['pink','blue'],asImage=True,dimensions=(800,500)) # ### Advanced Use # # It is also possible to get the Plotly Figure as an output to tweak it manually # # We can achieve this with **asFigure=True** # In[62]: df['GOOG'].iplot(asFigure=True) # We can also get the **Data** object directly # In[63]: data=df.to_iplot() # In[64]: data[0]['name']='My Custom Name' # And pass this directly to **iplot** # In[66]: df.iplot(data=data,filename='Tutorial Custom Name') # In[ ]: