In [4]:
import plotly
plotly.offline.init_notebook_mode();
plotly.__version__


Out[4]:
'3.5.0'

In [7]:
import plotly
import plotly.graph_objs as go
import MySQLdb as mdb
import pandas as pd
import plotly
con = mdb.connect('127.0.0.1', 'root', 'paytm@197', 'stocks');
with con:
    cur = con.cursor()
    cur.execute("select * from stock_data where symbol = 'pnb'  and series = 'eq'")
    rows = cur.fetchall()
    df = pd.DataFrame( [[ij for ij in i] for i in rows] )
    print(df.head(20))
    plotly.offline.plot({
    "data": [go.Scatter(x=df[3], y=df[4], mode="markers")]
    }, auto_open=True)
    print


         0    1   2           3      4      5      6      7      8      9   \
0   4018075  PNB  EQ  2002-04-25  31.00  40.00  40.00  35.00  37.30  37.40   
1   4018073  PNB  EQ  2002-04-28  37.40  38.50  39.10  37.65  39.10  38.60   
2   4018076  PNB  EQ  2002-04-29  38.60  39.00  40.35  38.50  40.15  40.10   
3   4018078  PNB  EQ  2002-05-01  40.10  40.25  46.10  40.25  45.10  44.80   
4   4018079  PNB  EQ  2002-05-02  44.80  45.80  47.45  44.55  46.95  46.45   
5   4018077  PNB  EQ  2002-05-05  46.45  47.00  48.25  45.95  46.80  46.70   
6   4018081  PNB  EQ  2002-05-06  46.70  46.85  46.85  44.65  45.00  45.30   
7   4018085  PNB  EQ  2002-05-07  45.30  45.50  45.70  42.60  44.25  43.40   
8   4018080  PNB  EQ  2002-05-08  43.40  44.70  44.70  42.85  43.80  43.70   
9   4018083  PNB  EQ  2002-05-09  43.70  48.00  48.00  42.90  43.60  43.35   
10  4028190  PNB  EQ  2002-05-12  43.35  44.00  45.00  43.90  44.70  44.65   
11  4028191  PNB  EQ  2002-05-13  44.65  45.00  45.00  43.25  43.35  43.50   
12  4028182  PNB  EQ  2002-05-14  43.50  43.80  44.25  42.40  42.50  42.60   
13  4028199  PNB  EQ  2002-05-15  42.60  42.80  42.80  41.10  41.10  41.25   
14  4028195  PNB  EQ  2002-05-16  41.25  41.00  41.65  39.00  40.25  40.15   
15  4028184  PNB  EQ  2002-05-19  40.15  39.95  40.00  38.30  38.60  38.70   
16  4028243  PNB  EQ  2002-05-20  38.70  38.50  38.50  36.05  37.50  37.25   
17  4028203  PNB  EQ  2002-05-21  37.25  36.55  38.20  36.40  37.25  37.15   
18  4028207  PNB  EQ  2002-05-22  37.15  37.75  38.10  37.00  37.70  37.60   
19  4028220  PNB  EQ  2002-05-23  37.60  38.50  39.65  38.40  39.60  38.85   

       10       11      12  13       14  15                  16  \
0   37.03  4000760  1482.0   0  2048637  51 2016-07-25 18:56:13   
1   38.26  2602886   996.0   0  1632801  63 2016-07-25 18:56:13   
2   39.79  1377334   548.0   0        0   0 2016-07-25 18:56:13   
3   44.15  3348126  1478.0   0  1307465  39 2016-07-25 18:56:13   
4   46.10  2689672  1240.0   0  1032559  38 2016-07-25 18:56:13   
5   46.96  1609113   756.0   0  1041920  65 2016-07-25 18:56:13   
6   45.69   964485   441.0   0   649068  67 2016-07-25 18:56:13   
7   43.90   642380   282.0   0   399537  62 2016-07-25 18:56:13   
8   43.62   507464   221.0   0   317226  63 2016-07-25 18:56:13   
9   43.32   315013   136.0   0   214039  68 2016-07-25 18:56:13   
10  44.65   525301   235.0   0   397041  76 2016-07-25 18:57:05   
11  44.16   226877   100.0   0   154916  68 2016-07-25 18:57:05   
12  43.10   280129   121.0   0   196848  70 2016-07-25 18:57:05   
13  41.61   164177    68.0   0   113349  69 2016-07-25 18:57:05   
14  40.22   361286   145.0   0   225755  62 2016-07-25 18:57:05   
15  39.13   237898    93.0   0   148760  63 2016-07-25 18:57:05   
16  37.32   271686   101.0   0   180770  67 2016-07-25 18:57:05   
17  37.23   173312    65.0   0   129204  75 2016-07-25 18:57:05   
18  37.68   218721    82.0   0   151730  69 2016-07-25 18:57:05   
19  39.09   278098   109.0   0   173187  62 2016-07-25 18:57:05   

                    17  
0  2016-07-25 18:56:13  
1  2016-07-25 18:56:13  
2  2016-07-25 18:56:13  
3  2016-07-25 18:56:13  
4  2016-07-25 18:56:13  
5  2016-07-25 18:56:13  
6  2016-07-25 18:56:13  
7  2016-07-25 18:56:13  
8  2016-07-25 18:56:13  
9  2016-07-25 18:56:13  
10 2016-07-25 18:57:05  
11 2016-07-25 18:57:05  
12 2016-07-25 18:57:05  
13 2016-07-25 18:57:05  
14 2016-07-25 18:57:05  
15 2016-07-25 18:57:05  
16 2016-07-25 18:57:05  
17 2016-07-25 18:57:05  
18 2016-07-25 18:57:05  
19 2016-07-25 18:57:05  

In [14]:
import plotly
import plotly.graph_objs as go
import MySQLdb as mdb
import pandas as pd
import plotly
con = mdb.connect('127.0.0.1', 'root', 'paytm@197', 'stocks');
with con:
    cur = con.cursor()
    cur.execute("select * from stock_data where symbol = 'pnb'  and series = 'eq'")
    rows = cur.fetchall()
    df = pd.DataFrame( [[ij for ij in i] for i in rows] )
    print(df.head(20))
    plotly.offline.plot({
    "data": [go.Scatter(x=df[3], y=df[4])]
    }, auto_open=True)


         0    1   2           3      4      5      6      7      8      9   \
0   4018075  PNB  EQ  2002-04-25  31.00  40.00  40.00  35.00  37.30  37.40   
1   4018073  PNB  EQ  2002-04-28  37.40  38.50  39.10  37.65  39.10  38.60   
2   4018076  PNB  EQ  2002-04-29  38.60  39.00  40.35  38.50  40.15  40.10   
3   4018078  PNB  EQ  2002-05-01  40.10  40.25  46.10  40.25  45.10  44.80   
4   4018079  PNB  EQ  2002-05-02  44.80  45.80  47.45  44.55  46.95  46.45   
5   4018077  PNB  EQ  2002-05-05  46.45  47.00  48.25  45.95  46.80  46.70   
6   4018081  PNB  EQ  2002-05-06  46.70  46.85  46.85  44.65  45.00  45.30   
7   4018085  PNB  EQ  2002-05-07  45.30  45.50  45.70  42.60  44.25  43.40   
8   4018080  PNB  EQ  2002-05-08  43.40  44.70  44.70  42.85  43.80  43.70   
9   4018083  PNB  EQ  2002-05-09  43.70  48.00  48.00  42.90  43.60  43.35   
10  4028190  PNB  EQ  2002-05-12  43.35  44.00  45.00  43.90  44.70  44.65   
11  4028191  PNB  EQ  2002-05-13  44.65  45.00  45.00  43.25  43.35  43.50   
12  4028182  PNB  EQ  2002-05-14  43.50  43.80  44.25  42.40  42.50  42.60   
13  4028199  PNB  EQ  2002-05-15  42.60  42.80  42.80  41.10  41.10  41.25   
14  4028195  PNB  EQ  2002-05-16  41.25  41.00  41.65  39.00  40.25  40.15   
15  4028184  PNB  EQ  2002-05-19  40.15  39.95  40.00  38.30  38.60  38.70   
16  4028243  PNB  EQ  2002-05-20  38.70  38.50  38.50  36.05  37.50  37.25   
17  4028203  PNB  EQ  2002-05-21  37.25  36.55  38.20  36.40  37.25  37.15   
18  4028207  PNB  EQ  2002-05-22  37.15  37.75  38.10  37.00  37.70  37.60   
19  4028220  PNB  EQ  2002-05-23  37.60  38.50  39.65  38.40  39.60  38.85   

       10       11      12  13       14  15                  16  \
0   37.03  4000760  1482.0   0  2048637  51 2016-07-25 18:56:13   
1   38.26  2602886   996.0   0  1632801  63 2016-07-25 18:56:13   
2   39.79  1377334   548.0   0        0   0 2016-07-25 18:56:13   
3   44.15  3348126  1478.0   0  1307465  39 2016-07-25 18:56:13   
4   46.10  2689672  1240.0   0  1032559  38 2016-07-25 18:56:13   
5   46.96  1609113   756.0   0  1041920  65 2016-07-25 18:56:13   
6   45.69   964485   441.0   0   649068  67 2016-07-25 18:56:13   
7   43.90   642380   282.0   0   399537  62 2016-07-25 18:56:13   
8   43.62   507464   221.0   0   317226  63 2016-07-25 18:56:13   
9   43.32   315013   136.0   0   214039  68 2016-07-25 18:56:13   
10  44.65   525301   235.0   0   397041  76 2016-07-25 18:57:05   
11  44.16   226877   100.0   0   154916  68 2016-07-25 18:57:05   
12  43.10   280129   121.0   0   196848  70 2016-07-25 18:57:05   
13  41.61   164177    68.0   0   113349  69 2016-07-25 18:57:05   
14  40.22   361286   145.0   0   225755  62 2016-07-25 18:57:05   
15  39.13   237898    93.0   0   148760  63 2016-07-25 18:57:05   
16  37.32   271686   101.0   0   180770  67 2016-07-25 18:57:05   
17  37.23   173312    65.0   0   129204  75 2016-07-25 18:57:05   
18  37.68   218721    82.0   0   151730  69 2016-07-25 18:57:05   
19  39.09   278098   109.0   0   173187  62 2016-07-25 18:57:05   

                    17  
0  2016-07-25 18:56:13  
1  2016-07-25 18:56:13  
2  2016-07-25 18:56:13  
3  2016-07-25 18:56:13  
4  2016-07-25 18:56:13  
5  2016-07-25 18:56:13  
6  2016-07-25 18:56:13  
7  2016-07-25 18:56:13  
8  2016-07-25 18:56:13  
9  2016-07-25 18:56:13  
10 2016-07-25 18:57:05  
11 2016-07-25 18:57:05  
12 2016-07-25 18:57:05  
13 2016-07-25 18:57:05  
14 2016-07-25 18:57:05  
15 2016-07-25 18:57:05  
16 2016-07-25 18:57:05  
17 2016-07-25 18:57:05  
18 2016-07-25 18:57:05  
19 2016-07-25 18:57:05  

In [12]:
import plotly
import plotly.graph_objs as go
import MySQLdb as mdb
import pandas as pd
import plotly
con = mdb.connect('127.0.0.1', 'root', 'paytm@197', 'stocks');
with con:
    cur = con.cursor()
    cur.execute("select * from stock_data where symbol = 'pnb'  and series = 'eq'")
    rows = cur.fetchall()
    df = pd.DataFrame( [[ij for ij in i] for i in rows] )
    print(df.head(20))
    plotly.offline.plot({
    "data": [go.Ohlc(x=df[3], open=df[5], high=df[6], low=df[7], close=df[9])]
    }, auto_open=True)


         0    1   2           3      4      5      6      7      8      9   \
0   4018075  PNB  EQ  2002-04-25  31.00  40.00  40.00  35.00  37.30  37.40   
1   4018073  PNB  EQ  2002-04-28  37.40  38.50  39.10  37.65  39.10  38.60   
2   4018076  PNB  EQ  2002-04-29  38.60  39.00  40.35  38.50  40.15  40.10   
3   4018078  PNB  EQ  2002-05-01  40.10  40.25  46.10  40.25  45.10  44.80   
4   4018079  PNB  EQ  2002-05-02  44.80  45.80  47.45  44.55  46.95  46.45   
5   4018077  PNB  EQ  2002-05-05  46.45  47.00  48.25  45.95  46.80  46.70   
6   4018081  PNB  EQ  2002-05-06  46.70  46.85  46.85  44.65  45.00  45.30   
7   4018085  PNB  EQ  2002-05-07  45.30  45.50  45.70  42.60  44.25  43.40   
8   4018080  PNB  EQ  2002-05-08  43.40  44.70  44.70  42.85  43.80  43.70   
9   4018083  PNB  EQ  2002-05-09  43.70  48.00  48.00  42.90  43.60  43.35   
10  4028190  PNB  EQ  2002-05-12  43.35  44.00  45.00  43.90  44.70  44.65   
11  4028191  PNB  EQ  2002-05-13  44.65  45.00  45.00  43.25  43.35  43.50   
12  4028182  PNB  EQ  2002-05-14  43.50  43.80  44.25  42.40  42.50  42.60   
13  4028199  PNB  EQ  2002-05-15  42.60  42.80  42.80  41.10  41.10  41.25   
14  4028195  PNB  EQ  2002-05-16  41.25  41.00  41.65  39.00  40.25  40.15   
15  4028184  PNB  EQ  2002-05-19  40.15  39.95  40.00  38.30  38.60  38.70   
16  4028243  PNB  EQ  2002-05-20  38.70  38.50  38.50  36.05  37.50  37.25   
17  4028203  PNB  EQ  2002-05-21  37.25  36.55  38.20  36.40  37.25  37.15   
18  4028207  PNB  EQ  2002-05-22  37.15  37.75  38.10  37.00  37.70  37.60   
19  4028220  PNB  EQ  2002-05-23  37.60  38.50  39.65  38.40  39.60  38.85   

       10       11      12  13       14  15                  16  \
0   37.03  4000760  1482.0   0  2048637  51 2016-07-25 18:56:13   
1   38.26  2602886   996.0   0  1632801  63 2016-07-25 18:56:13   
2   39.79  1377334   548.0   0        0   0 2016-07-25 18:56:13   
3   44.15  3348126  1478.0   0  1307465  39 2016-07-25 18:56:13   
4   46.10  2689672  1240.0   0  1032559  38 2016-07-25 18:56:13   
5   46.96  1609113   756.0   0  1041920  65 2016-07-25 18:56:13   
6   45.69   964485   441.0   0   649068  67 2016-07-25 18:56:13   
7   43.90   642380   282.0   0   399537  62 2016-07-25 18:56:13   
8   43.62   507464   221.0   0   317226  63 2016-07-25 18:56:13   
9   43.32   315013   136.0   0   214039  68 2016-07-25 18:56:13   
10  44.65   525301   235.0   0   397041  76 2016-07-25 18:57:05   
11  44.16   226877   100.0   0   154916  68 2016-07-25 18:57:05   
12  43.10   280129   121.0   0   196848  70 2016-07-25 18:57:05   
13  41.61   164177    68.0   0   113349  69 2016-07-25 18:57:05   
14  40.22   361286   145.0   0   225755  62 2016-07-25 18:57:05   
15  39.13   237898    93.0   0   148760  63 2016-07-25 18:57:05   
16  37.32   271686   101.0   0   180770  67 2016-07-25 18:57:05   
17  37.23   173312    65.0   0   129204  75 2016-07-25 18:57:05   
18  37.68   218721    82.0   0   151730  69 2016-07-25 18:57:05   
19  39.09   278098   109.0   0   173187  62 2016-07-25 18:57:05   

                    17  
0  2016-07-25 18:56:13  
1  2016-07-25 18:56:13  
2  2016-07-25 18:56:13  
3  2016-07-25 18:56:13  
4  2016-07-25 18:56:13  
5  2016-07-25 18:56:13  
6  2016-07-25 18:56:13  
7  2016-07-25 18:56:13  
8  2016-07-25 18:56:13  
9  2016-07-25 18:56:13  
10 2016-07-25 18:57:05  
11 2016-07-25 18:57:05  
12 2016-07-25 18:57:05  
13 2016-07-25 18:57:05  
14 2016-07-25 18:57:05  
15 2016-07-25 18:57:05  
16 2016-07-25 18:57:05  
17 2016-07-25 18:57:05  
18 2016-07-25 18:57:05  
19 2016-07-25 18:57:05  

In [8]:
import plotly
import plotly.graph_objs as go
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_us_cities.csv')
df.head()

df['text'] = df['name'] + '<br>Population ' + (df['pop']/1e6).astype(str)+' million'
limits = [(0,2),(3,10),(11,20),(21,50),(50,3000)]
colors = ["rgb(0,116,217)","rgb(255,65,54)","rgb(133,20,75)","rgb(255,133,27)","lightgrey"]
cities = []
scale = 5000

for i in range(len(limits)):
    lim = limits[i]
    df_sub = df[lim[0]:lim[1]]
    city = dict(
        type = 'scattergeo',
        locationmode = 'USA-states',
        lon = df_sub['lon'],
        lat = df_sub['lat'],
        text = df_sub['text'],
        marker = dict(
            size = df_sub['pop']/scale,
            # sizeref = 2. * max(df_sub['pop']/scale) / (25 ** 2),
            color = colors[i],
            line = dict(width=0.5, color='rgb(40,40,40)'),
            sizemode = 'area'
        ),
        name = '{0} - {1}'.format(lim[0],lim[1]) )
    cities.append(city)

layout = dict(
        title = '2014 US city populations<br>(Click legend to toggle traces)',
        showlegend = True,
        geo = dict(
            scope='usa',
            projection=dict( type='albers usa' ),
            showland = True,
            landcolor = 'rgb(217, 217, 217)',
            subunitwidth=1,
            countrywidth=1,
            subunitcolor="rgb(255, 255, 255)",
            countrycolor="rgb(255, 255, 255)"
        ),
    )

fig = dict(data=cities, layout=layout)
plotly.offline.iplot(fig, validate=False, filename='d3-bubble-map-populations')



In [9]:
import plotly
import plotly.graph_objs as go

import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_ebola.csv')
df.head()

cases = []
colors = ['rgb(239,243,255)','rgb(189,215,231)','rgb(107,174,214)','rgb(33,113,181)']
months = {6:'June',7:'July',8:'Aug',9:'Sept'}

for i in range(6,10)[::-1]:
    cases.append(go.Scattergeo(
            lon = df[ df['Month'] == i ]['Lon'], #-(max(range(6,10))-i),
            lat = df[ df['Month'] == i ]['Lat'],
            text = df[ df['Month'] == i ]['Value'],
            name = months[i],
            marker = dict(
                size = df[ df['Month'] == i ]['Value']/50,
                color = colors[i-6],
                line = dict(width = 0)
            )
        )
    )

cases[0]['text'] = df[ df['Month'] == 9 ]['Value'].map('{:.0f}'.format).astype(str)+' '+\
    df[ df['Month'] == 9 ]['Country']
cases[0]['mode'] = 'markers+text'
cases[0]['textposition'] = 'bottom center'

inset = [
    go.Choropleth(
        locationmode = 'country names',
        locations = df[ df['Month'] == 9 ]['Country'],
        z = df[ df['Month'] == 9 ]['Value'],
        text = df[ df['Month'] == 9 ]['Country'],
        colorscale = [[0,'rgb(0, 0, 0)'],[1,'rgb(0, 0, 0)']],
        autocolorscale = False,
        showscale = False,
        geo = 'geo2'
    ),
    go.Scattergeo(
        lon = [21.0936],
        lat = [7.1881],
        text = ['Africa'],
        mode = 'text',
        showlegend = False,
        geo = 'geo2'
    )
]

layout = go.Layout(
    title = 'Ebola cases reported by month in West Africa 2014<br> \
Source: <a href="https://data.hdx.rwlabs.org/dataset/rowca-ebola-cases">\
HDX</a>',
    geo = dict(
        resolution = 50,
        scope = 'africa',
        showframe = False,
        showcoastlines = True,
        showland = True,
        landcolor = "rgb(229, 229, 229)",
        countrycolor = "rgb(255, 255, 255)" ,
        coastlinecolor = "rgb(255, 255, 255)",
        projection = dict(
            type = 'mercator'
        ),
        lonaxis = dict( range= [ -15.0, -5.0 ] ),
        lataxis = dict( range= [ 0.0, 12.0 ] ),
        domain = dict(
            x = [ 0, 1 ],
            y = [ 0, 1 ]
        )
    ),
    geo2 = dict(
        scope = 'africa',
        showframe = False,
        showland = True,
        landcolor = "rgb(229, 229, 229)",
        showcountries = False,
        domain = dict(
            x = [ 0, 0.6 ],
            y = [ 0, 0.6 ]
        ),
        bgcolor = 'rgba(255, 255, 255, 0.0)',
    ),
    legend = dict(
           traceorder = 'reversed'
    )
)

fig = go.Figure(layout=layout, data=cases+inset)
plotly.offline.iplot(fig, validate=False, filename='West Africa Ebola cases 2014')


/home/palashkulshreshtha/anaconda2/lib/python2.7/site-packages/numpy/core/numeric.py:2397: FutureWarning:

elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison


In [ ]:
import plotly.plotly as py
figure = py.get_figure('https://plot.ly/~prashasti/7')
print figure

In [6]:
import plotly
import plotly.graph_objs as go


import igraph as ig


import json
import urllib2

data = []
req = urllib2.Request("https://raw.githubusercontent.com/plotly/datasets/master/miserables.json")
opener = urllib2.build_opener()
f = opener.open(req)
data = json.loads(f.read())

print data.keys()

N=len(data['nodes'])
L=len(data['links'])
Edges=[(data['links'][k]['source'], data['links'][k]['target']) for k in range(L)]

G=ig.Graph(Edges, directed=False)
labels=[]
group=[]
for node in data['nodes']:
    labels.append(node['name'])
    group.append(node['group'])
    
layt=G.layout('kk', dim=3) 

Xn=[layt[k][0] for k in range(N)]# x-coordinates of nodes
Yn=[layt[k][1] for k in range(N)]# y-coordinates
Zn=[layt[k][2] for k in range(N)]# z-coordinates
Xe=[]
Ye=[]
Ze=[]
for e in Edges:
    Xe+=[layt[e[0]][0],layt[e[1]][0], None]# x-coordinates of edge ends
    Ye+=[layt[e[0]][1],layt[e[1]][1], None]  
    Ze+=[layt[e[0]][2],layt[e[1]][2], None]  
    

trace1=go.Scatter3d(x=Xe,
               y=Ye,
               z=Ze,
               mode='lines',
               line=dict(color='rgb(125,125,125)', width=1),
               hoverinfo='none'
               )

trace2=go.Scatter3d(x=Xn,
               y=Yn,
               z=Zn,
               mode='markers',
               name='actors',
               marker=dict(symbol='circle',
                             size=6,
                             color=group,
                             colorscale='Viridis',
                             line=dict(color='rgb(50,50,50)', width=0.5)
                             ),
               text=labels,
               hoverinfo='text'
               )

axis=dict(showbackground=False,
          showline=False,
          zeroline=False,
          showgrid=False,
          showticklabels=False,
          title=''
          )

layout = go.Layout(
         title="Network of coappearances of characters in Victor Hugo's novel<br> Les Miserables (3D visualization)",
         width=1000,
         height=1000,
         showlegend=False,
         scene=dict(
             xaxis=dict(axis),
             yaxis=dict(axis),
             zaxis=dict(axis),
        ),
     margin=dict(
        t=100
    ),
    hovermode='closest',
    annotations=[
           dict(
           showarrow=False,
            text="Data source: <a href='http://bost.ocks.org/mike/miserables/miserables.json'>[1] miserables.json</a>",
            xref='paper',
            yref='paper',
            x=0,
            y=0.1,
            xanchor='left',
            yanchor='bottom',
            font=dict(
            size=14
            )
            )
        ],    )


data=[trace1, trace2]
fig=go.Figure(data=data, layout=layout)

plotly.offline.iplot(fig, filename='Les-Miserables')


[u'nodes', u'links']

In [ ]: