Line Chart

Interactive line charts with plotly and cufflinks

dataset: cufflinks.datagen.lines()


In [94]:
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 [95]:
df = cf.datagen.lines()
df.head()


Out[95]:
JWL.UX BWK.XN SEB.HG XSI.ZX RDE.CJ
2015-01-01 -0.757131 -0.864369 2.399753 0.921486 0.720824
2015-01-02 1.065374 -1.263472 2.761705 0.550748 -0.369328
2015-01-03 0.490702 0.846537 2.033277 1.914639 0.689105
2015-01-04 0.901229 0.591372 0.419805 2.224403 1.475613
2015-01-05 0.304475 1.072849 1.153209 1.571708 1.429304

Plotting through the plotly interface


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


Out[96]:

Alternatively, use cufflinks to access iplot through pandas


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

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


Figure(
    data=Data([
        Scatter(
            x=['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04', '..'  ],
            y=array([-0.7571311 ,  1.06537441,  0.49070172,  0.9012289 ,  0...,
            line=Line(
                color='rgba(255, 153, 51, 1.0)',
                dash='solid',
                width=1.3
            ),
            mode='lines',
            name='JWL.UX',
            text=''
        ),
        Scatter(
            x=['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04', '..'  ],
            y=array([-0.8643691 , -1.26347193,  0.84653712,  0.59137176,  1...,
            line=Line(
                color='rgba(55, 128, 191, 1.0)',
                dash='solid',
                width=1.3
            ),
            mode='lines',
            name='BWK.XN',
            text=''
        ),
        Scatter(
            x=['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04', '..'  ],
            y=array([  2.39975317,   2.76170455,   2.0332771 ,   0.41980509,..,
            line=Line(
                color='rgba(50, 171, 96, 1.0)',
                dash='solid',
                width=1.3
            ),
            mode='lines',
            name='SEB.HG',
            text=''
        ),
        Scatter(
            x=['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04', '..'  ],
            y=array([ 0.92148579,  0.55074793,  1.91463889,  2.2244029 ,  1...,
            line=Line(
                color='rgba(128, 0, 128, 1.0)',
                dash='solid',
                width=1.3
            ),
            mode='lines',
            name='XSI.ZX',
            text=''
        ),
        Scatter(
            x=['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04', '..'  ],
            y=array([ 0.72082388, -0.36932796,  0.68910463,  1.47561331,  1...,
            line=Line(
                color='rgba(219, 64, 82, 1.0)',
                dash='solid',
                width=1.3
            ),
            mode='lines',
            name='RDE.CJ',
            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 [99]:
dir(p)


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

In [100]:
py.iplot(p)


Out[100]:

In [ ]:


In [101]:
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'
#     print(l['line']['color'])
py.iplot(p)


(0.5, 0.5, 0.5)
(0.5, 0.5, 0.5)
(0.5, 0.5, 0.5)
(0.5, 0.5, 0.5)
(0.5, 0.5, 0.5)
Out[101]:

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

In [103]:
type(p)


Out[103]:
plotly.tools.PlotlyDisplay

In [104]:
dir(p.data)


Out[104]:
['__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 [105]:
p.data


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

In [106]:
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[106]:

In [ ]: