import plotly.plotly as py #
import plotly.tools as tls #
stream_ids = tls.get_credentials_file()['stream_ids']
stream_ids
from plotly.graph_objs import Data, Layout, Figure, Stream
from plotly.graph_objs import Heatmap, Contour
from plotly.graph_objs import XAxis, YAxis, ColorBar, Margin
from plotly.graph_objs import Font, Contours
import numpy as np
import pandas as pd
import urllib2 #
import time
import datetime
#
url_csv = 'http://donnees.ville.montreal.qc.ca/storage/f/\
2014-01-20T20%3A48%3A50.296Z/2013.csv'
fichier_csv = urllib2.urlopen(url_csv) #
df = pd.read_csv(fichier_csv) #
df.head()
# Sites' full names
sites = ['Berri/
de Maisonneuve',
'Côte-Ste-Catherine
(parc Beaubien)',
'de Maisonneuve/
Berri',
'de Maisonneuve/
Peel',
'du Parc/
Duluth',
'Pierre-Dupuy
(Habitat 67)',
'Rachel/
Marquette',
'Laurier
(métro)']
dates = df['Date'].tolist()
dates_in_words = [datetime.datetime.strptime(i,'%d/%m/%Y').strftime('%A, %B %d %Y')
for i in dates]
dates_in_words
# Get values of every row of columns 2 and up
Z = df.ix[:,2:].values
# 153 sample days (rows) at 8 sites (columns)
Z.shape
Z.max()
# Make a first stream id object
stream_id = stream_ids[0] # get a stream token from list
stream = Stream(token=stream_id, # N.B. link stream id to 'token' key
maxpoints=30) # N.B. keep maximum of 30 pts on plot
z_init = np.zeros((1,len(sites)))
# Scatter object for sin(x) line
data = Data([
Heatmap(
z=z_init,
x=sites,
y=[0],
zauto=False,
zmin=0,
zmax=9000,
stream=stream
)
])
layout = Layout(
title='Heatmap Stream test',
yaxis=YAxis(
ticks='',
showticklabels=False
),
margin= Margin(
l=10,
b=100,
t=50
),
autosize=False,
width=600,
height=280)
figure = Figure(data=data, layout=layout)
py.iplot(figure, filename='stream-heatmap-test')
s = py.Stream(stream_id)
s.open()
time.sleep(2)
for i,z in enumerate(Z):
s_data = dict(z=[z], type='heatmap') # must include type='heatmap'
s_layout = dict(title=dates_in_words[i])
#s.write(s_data, layout=s_layout, validate=False)
s.write(s_data, layout=s_layout)
time.sleep(0.5)
s.close()
from IPython.display import display, HTML
import urllib2
url = 'https://raw.githubusercontent.com/plotly/python-user-guide/master/custom.css'
display(HTML(urllib2.urlopen(url).read()))