Line Chart

Interactive line charts with plotly and cufflinks

dataset: cufflinks.datagen.lines()


In [1]:
import plotly.plotly as py
import cufflinks as cf
import pandas as pd
import numpy as np
import plotly.graph_objs as go
%matplotlib inline

In [2]:
df = cf.datagen.lines()
df.head()


Out[2]:
IGV.MQ TAE.HZ IIQ.AJ FQU.VL BJX.ZX
2015-01-01 -1.765827 -0.509427 -0.244604 -0.408546 -0.966016
2015-01-02 -1.616644 -1.765813 -1.722173 -0.091165 -0.759200
2015-01-03 -0.327747 -1.605062 -1.044393 -0.867000 -0.228630
2015-01-04 -2.109247 -1.283938 -0.857723 0.501894 2.111430
2015-01-05 0.060847 -0.736981 -3.233569 0.951044 2.424618

Plotting through the plotly interface


In [3]:
py.iplot([{
    'x': df.index,
    'y': df[col],
    'name': col
}  for col in df.columns], filename='simple-line')


Out[3]:

Alternatively, use cufflinks to access iplot through pandas


In [4]:
p = df.iplot(kind='scatter', filename='simple-line', asFigure=True)

In [5]:
print(p.to_string())


Figure(
    data=Data([
        Scatter(
            x=['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04', '..'  ],
            y=array([-1.76582727, -1.61664405, -0.32774733, -2.10924706,  0...,
            line=Line(
                color='rgba(255, 153, 51, 1.0)',
                dash='solid',
                width=1.3
            ),
            mode='lines',
            name='IGV.MQ',
            text=''
        ),
        Scatter(
            x=['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04', '..'  ],
            y=array([ -0.50942682,  -1.76581333,  -1.60506214,  -1.28393768,..,
            line=Line(
                color='rgba(55, 128, 191, 1.0)',
                dash='solid',
                width=1.3
            ),
            mode='lines',
            name='TAE.HZ',
            text=''
        ),
        Scatter(
            x=['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04', '..'  ],
            y=array([ -0.24460414,  -1.72217261,  -1.04439348,  -0.85772345,..,
            line=Line(
                color='rgba(50, 171, 96, 1.0)',
                dash='solid',
                width=1.3
            ),
            mode='lines',
            name='IIQ.AJ',
            text=''
        ),
        Scatter(
            x=['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04', '..'  ],
            y=array([-0.40854624, -0.09116463, -0.86700049,  0.50189359,  0...,
            line=Line(
                color='rgba(128, 0, 128, 1.0)',
                dash='solid',
                width=1.3
            ),
            mode='lines',
            name='FQU.VL',
            text=''
        ),
        Scatter(
            x=['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04', '..'  ],
            y=array([ -0.9660157 ,  -0.75920019,  -0.2286299 ,   2.11143014,..,
            line=Line(
                color='rgba(219, 64, 82, 1.0)',
                dash='solid',
                width=1.3
            ),
            mode='lines',
            name='BJX.ZX',
            text=''
        )
    ]),
    layout=Layout(
        legend=Legend(
            bgcolor='#F5F6F9',
            font=Font(
                color='#4D5663'
            )
        ),
        paper_bgcolor='#F5F6F9',
        plot_bgcolor='#F5F6F9',
        titlefont=dict(
            color='#4D5663'
        ),
        xaxis1=XAxis(
            gridcolor='#E1E5ED',
            showgrid=True,
            tickfont=dict(
                color='#4D5663'
            ),
            title='',
            titlefont=dict(
                color='#4D5663'
            ),
            zerolinecolor='#E1E5ED'
        ),
        yaxis1=YAxis(
            gridcolor='#E1E5ED',
            showgrid=True,
            tickfont=dict(
                color='#4D5663'
            ),
            title='',
            titlefont=dict(
                color='#4D5663'
            ),
            zerolinecolor='#E1E5ED'
        )
    )
)

In [6]:
dir(p)


Out[6]:
['data', 'frames', 'layout']

In [7]:
py.iplot(p)


Out[7]:

In [ ]:


In [8]:
for l in p.data:
    pass
#     l.line.color = (l.line.color[0] - 0.2, l.line.color[1]-0.2, l.line.color[2]-2)
#     l['line']['style'] = 'longdash'
#     l['line']['color'] = (0.5,0.5,0.5)
    l['line']['color'] = 'red'
    l['line']['dash'] = 'longdash'
#     print(l['line']['color'])
p.data[0].mode = 'lines+markers'
py.iplot(p)


Out[8]:

In [9]:
p = py.iplot([{
    'x': df.index,
    'y': df[col],
    'name': col
}  for col in df.columns], filename='simple-line')

In [10]:
type(p)


Out[10]:
plotly.tools.PlotlyDisplay

In [11]:
dir(p.data)


Out[11]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getnewargs__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rmod__',
 '__rmul__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'capitalize',
 'casefold',
 'center',
 'count',
 'encode',
 'endswith',
 'expandtabs',
 'find',
 'format',
 'format_map',
 'index',
 'isalnum',
 'isalpha',
 'isdecimal',
 'isdigit',
 'isidentifier',
 'islower',
 'isnumeric',
 'isprintable',
 'isspace',
 'istitle',
 'isupper',
 'join',
 'ljust',
 'lower',
 'lstrip',
 'maketrans',
 'partition',
 'replace',
 'rfind',
 'rindex',
 'rjust',
 'rpartition',
 'rsplit',
 'rstrip',
 'split',
 'splitlines',
 'startswith',
 'strip',
 'swapcase',
 'title',
 'translate',
 'upper',
 'zfill']

In [12]:
p.data


Out[12]:
'<iframe id="igraph" scrolling="no" style="border:none;" seamless="seamless" src="https://plot.ly/~apryor6/1.embed" height="525px" width="100%"></iframe>'

In [13]:
trace = go.Scatter(
    x = df.index,
    y = df.iloc[:,1],
    mode = 'markers'
)

data = [trace]

# Plot and embed in ipython notebook!
py.iplot(data, filename='basic-scatter')


Out[13]:

In [ ]: