In [4]:
import plotly
plotly.offline.init_notebook_mode();
plotly.__version__
Out[4]:
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
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)
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)
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')
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')
In [ ]: