This library binds the power of plotly with the flexibility of pandas 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 guide.
import pandas as pd
import cufflinks as cf
import numpy as np
We create a set of timeseries
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
help(df.iplot)
Help on method _iplot in module cufflinks.plotlytools: _iplot(self, data=None, layout=None, filename='Plotly Playground', world_readable=False, kind='scatter', title='', xTitle='', yTitle='', theme='pearl', colors=None, fill=False, barmode='', sortbars=False, annotations=None, asDates=False, asFigure=False, asImage=False, dimensions=(1116, 587), keys=False, bestfit=False, bestfit_colors=None, asPlot=False, **kwargs) method of pandas.core.frame.DataFrame instance Returns a plotly chart either as inline chart, image of Figure object Parameters: ----------- data : Data Plotly Data Object. If not entered then the Data object will be automatically generated from the DataFrame. layout : Layout Plotly layout Object If not entered then the Layout objet will be automatically generated from the DataFrame. filename : string Filename to be saved as in plotly account world_readable : bool If False then it will be saved as a private file kind : string Kind of chart scatter bar spread ratio title : string Chart Title xTitle : string X Axis Title yTitle : string Y Axis Title theme : string Layout Theme solar pearl white colors : list or dict {key:color} to specify the color for each column [colors] to use the colors in the defined order fill : bool Filled Traces barmode : string Mode when displaying bars group stack overlay * Only valid when kind='bar' sortbars : bool Sort bars in descending order * Only valid when kind='bar' annotations : dictionary Dictionary of annotations {x_point : text} asDates : bool If true it forces truncates times from a DatetimeIndex asFigure : bool If True returns plotly Figure asImage : bool If True it returns Image * Only valid when asImage=True dimensions : tuple(int,int) Dimensions for image (width,height) keys : list of columns List of columns to chart. Also can be usded for custom sorting. bestfit : boolean or list If True then a best fit line will be generated for all columns. If list then a best fit line will be generated for each key on the list. bestfit_colors : list or dict {key:color} to specify the color for each column [colors] to use the colors in the defined order
df.iplot(filename='Tutorial 1',world_readable=True)
We can pass a theme to the iplot function. 3 themes are available, but you can create your own
df[['APPL','IBM','VERZ']].iplot(theme='white',filename='Tutorial White',world_readable=True)
We can also pass common metadata for the chart
df.iplot(theme='pearl',filename='Tutorial Metadata',title='Stock Returns',xTitle='Dates',yTitle='Returns',world_readable=True)
We can easily add a bestfit line to any Series
This will automatically add a best fit approximation and the equation as the legend.
df['IBM'].iplot(filename='IBM Returns',bestfit=True,world_readable=True)
We can pass any color (either by Hex, RGB or Text *)
*Text values are specified in the cufflinks.colors modules
df['IBM'].iplot(filename='IBM Returns - colors',bestfit=True,colors=['pink'],bestfit_colors=['blue'],world_readable=True)
We can add a fill to a trace with fill=True
df['MSFT'].iplot(filename='Tutorial Microsoft',fill=True,colors=['green'],world_readable=True)
We can easily create a bar chart with the parameter kind
df.sum().iplot(kind='bar',filename='Tutorial Barchart',world_readable=True)
Bars can also be stacked by a given dimension
df.resample('M').iplot(kind='bar',barmode='stacked',world_readable=True,filename='Tutorial Bar Stacked')
We can also create spread and ratio charts on the fly with kind='spread' and kind='ratio'
df[['IBM','GOOG']].iplot(filename='Tutorial Spread',kind='spread',world_readable=True)
df[['IBM','GOOG']].iplot(filename='Tutorial Ratio',kind='ratio',colors=['green','red'],world_readable=True)
Annotations can be added to the chart and these are automatically positioned correctly.
Annotations should be specified in a dictionary form
annotations={'2015-01-15':'Dividends','2015-03-31':'Split Announced'}
df['MSFT'].iplot(filename='Tutorial Annotations',annotations=annotations,world_readable=True)
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)
df[['VERZ','MSFT']].iplot(filename='Tutorial Image',theme='white',colors=['pink','blue'],asImage=True,dimensions=(800,500),world_readable=True)
It is also possible to get the Plotly Figure as an output to tweak it manually
We can achieve this with asFigure=True
df['GOOG'].iplot(asFigure=True)
{'data': [{'line': {'color': 'rgba(255, 153, 51, 1.0)', 'width': 3}, 'name': 'Series1', 'type': 'scatter', 'x': ['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04', '2015-01-05', '2015-01-06', '2015-01-07', '2015-01-08', '2015-01-09', '2015-01-10', '2015-01-11', '2015-01-12', '2015-01-13', '2015-01-14', '2015-01-15', '2015-01-16', '2015-01-17', '2015-01-18', '2015-01-19', '2015-01-20', '2015-01-21', '2015-01-22', '2015-01-23', '2015-01-24', '2015-01-25', '2015-01-26', '2015-01-27', '2015-01-28', '2015-01-29', '2015-01-30', '2015-01-31', '2015-02-01', '2015-02-02', '2015-02-03', '2015-02-04', '2015-02-05', '2015-02-06', '2015-02-07', '2015-02-08', '2015-02-09', '2015-02-10', '2015-02-11', '2015-02-12', '2015-02-13', '2015-02-14', '2015-02-15', '2015-02-16', '2015-02-17', '2015-02-18', '2015-02-19', '2015-02-20', '2015-02-21', '2015-02-22', '2015-02-23', '2015-02-24', '2015-02-25', '2015-02-26', '2015-02-27', '2015-02-28', '2015-03-01', '2015-03-02', '2015-03-03', '2015-03-04', '2015-03-05', '2015-03-06', '2015-03-07', '2015-03-08', '2015-03-09', '2015-03-10', '2015-03-11', '2015-03-12', '2015-03-13', '2015-03-14', '2015-03-15', '2015-03-16', '2015-03-17', '2015-03-18', '2015-03-19', '2015-03-20', '2015-03-21', '2015-03-22', '2015-03-23', '2015-03-24', '2015-03-25', '2015-03-26', '2015-03-27', '2015-03-28', '2015-03-29', '2015-03-30', '2015-03-31', '2015-04-01', '2015-04-02', '2015-04-03', '2015-04-04', '2015-04-05', '2015-04-06', '2015-04-07', '2015-04-08', '2015-04-09', '2015-04-10'], 'y': array([ -0.84307038, -0.23988 , -0.24712782, -0.206079 , 0.30260076, -0.26149622, 0.94923657, -0.87086298, -2.43899363, -4.5461539 , -2.19105018, -0.84264861, 0.19721508, -1.25244663, -3.16441614, -3.25387951, -3.05291923, -2.39344244, -3.31717231, -3.65656848, -2.95241868, -4.96186753, -5.30133756, -3.63216678, -2.79089787, -1.89480703, 0.32726061, 0.43040928, -1.05105314, -0.8553633 , -0.44494275, -1.32405419, -1.92419779, -2.52025252, -1.45987333, -2.74300755, -3.47547168, -3.75958061, -3.1895065 , -3.59781194, -3.28875999, -3.99438545, -4.62704904, -5.29802596, -4.28896384, -6.22376128, -5.09315456, -5.16465128, -4.23655114, -5.44272708, -6.27102022, -7.43151687, -8.22540843, -7.33610555, -8.12491502, -7.84965714, -8.18624738, -8.60799973, -10.25876129, -9.23755701, -10.25764616, -10.94542388, -10.67984648, -10.26743451, -9.8174951 , -8.00168017, -7.3501401 , -7.46845272, -8.1986733 , -8.1524798 , -7.38348542, -7.49959634, -7.64304046, -6.80338978, -6.34034309, -7.41695506, -7.33499249, -6.05771665, -4.87605754, -4.83880523, -5.2846709 , -4.96422559, -4.50301637, -5.01684426, -5.04600144, -3.57822826, -3.31837615, -3.28824504, -2.13557335, -2.90014856, -1.15360841, -0.9619333 , -0.65587568, -1.21665718, -1.46641048, -0.3098288 , 0.65747591, 0.74768965, 1.044256 , 0.22346763])}], 'layout': {'legend': {'bgcolor': '#F5F6F9', 'font': {'color': '#4D5663'}}, 'paper_bgcolor': '#F5F6F9', 'plot_bgcolor': '#F5F6F9', 'xaxis': {'gridcolor': '#FFFFFF', 'tickfont': {'color': '#4D5663'}, 'title': ''}, 'yaxis': {'gridcolor': '#FFFFFF', 'tickfont': {'color': '#4D5663'}, 'title': '', 'titlefont': {'color': '#4D5663'}, 'zeroline': False}}}
We can also get the Data object directly
data=df.to_iplot()
data[0]['name']='My Custom Name'
And pass this directly to iplot
df.iplot(data=data,filename='Tutorial Custom Name',world_readable=True)