# plotly stuff
import plotly
import plotly.plotly as py
from plotly.graph_objs import *
# numerical stuff
import numpy as np
# matplotlib stuff
%matplotlib inline
import matplotlib.pyplot as plt
# streaming stuff
import twitter
import time
import json
import requests
# organize data
x = np.linspace(0,2*np.pi,100)
y = np.sin(np.linspace(0,2*np.pi,100))
s = np.random.rand(100)*50
# design a trace
trace = Scatter(x=x, y=y, mode='markers', marker=Marker(size=s))
# open figure in browser
url = py.plot([trace], filename="quick-plot", auto_open=False)
print url
# embed your url inside IPython (works for all public figures on Plotly)
plotly.tools.embed(url)
https://plot.ly/~theengineear/2192
Now, you can view this plot and all future edits at that same location!
In addition, if I make you a collaborator, you can edit this at the same location.
Let's looks at how we might share this:
The original plot:
Executable code in Plotly's supported APIs:
As a static image:
When you make a graph all of these resources are automatically available to you.
One of the intended outcomes from the above is reproducibility!
Perhaps you upload a plot from Python. By simply sharing the root unique url, you give collaborators access to both your data and your visualization. Pulling a quick example from the public feed...
plotly.tools.embed('https://plot.ly/~lucagiuliano/3/life-span-and-gestation-time-in-mammals/')
It now doesn't really matter what language produced this plot in the first place. All we care about is that we're currently using Python. So we use a native Python call to retrieve the figure...
pfig = py.get_figure('https://plot.ly/~lucagiuliano/3/life-span-and-gestation-time-in-mammals/')
print pfig.to_string(pretty=True)
Figure( data=Data([ Scatter( x=[42, 60, 180, 63, 230, 281, 365, 400, 148, 252, 63, 28, 68, 3...], y=[4.5, 14, 27, 28, 50, 30, 40, 28, 20, 39.3, 16.2, 9, 7.6, 46,...], mode='markers', name='maximum life span (years)' ), Scatter( x=[19, 26.775510204081634, 34.55102040816327, 42.32653061224489...], y=[7.5017678398522065, 8.91740585145299, 10.280204708119975, 11...], name='maximum life span (years) - fit', line=Line( color='rgb(55, 126, 184)', width=4 ), opacity=0.5, xaxis='x', yaxis='y' ) ]), layout=Layout( title='Life span and gestation time in Mammals', titlefont={'color': '', 'family': '', 'size': 0}, font=Font( family="'Open sans', verdana, arial, sans-serif", size=12, color='#444' ), showlegend=True, autosize=True, width=964, height=338, xaxis=XAxis( title='gestation time (days)', titlefont={'color': '', 'family': '', 'size': 0}, range=[-10.459920824325465, 429.4599208243255], domain=[0, 1], type='linear', rangemode='normal', showgrid=True, zeroline=True, showline=False, autotick=True, nticks=0, ticks='', showticklabels=True, tick0=0, dtick=50, ticklen=5, tickwidth=1, tickcolor='#444', tickangle='auto', tickfont=Font( family='', size=0, color='' ), exponentformat='B', showexponent='all', gridcolor='#eee', gridwidth=1, zerolinecolor='#444', zerolinewidth=1, linecolor='#444', linewidth=1, anchor='y', position=0, mirror=False, overlaying=False, autorange=True ), yaxis=YAxis( title='maximum life span (years)', titlefont={'color': '', 'family': '', 'size': 0}, range=[-0.545374449339207, 53.74537444933921], domain=[0, 1], type='linear', rangemode='normal', showgrid=True, zeroline=True, showline=False, autotick=True, nticks=0, ticks='', showticklabels=True, tick0=0, dtick=20, ticklen=5, tickwidth=1, tickcolor='#444', tickangle='auto', tickfont=Font( family='', size=0, color='' ), exponentformat='B', showexponent='all', gridcolor='#eee', gridwidth=1, zerolinecolor='#444', zerolinewidth=1, linecolor='#444', linewidth=1, anchor='x', position=0, mirror=False, overlaying=False, autorange=True ), legend=Legend( x=1.02, y=1, traceorder='normal', font=Font( family='', size=0, color='' ), bgcolor='#fff', bordercolor='#444', borderwidth=0, xanchor='left', yanchor='top' ), margin=Margin( l=80, r=80, b=80, t=100, pad=0, autoexpand=True ), paper_bgcolor='#fff', plot_bgcolor='#fff', hovermode='x', dragmode='zoom', barmode='group', bargap=0.2, bargroupgap=0, boxmode='overlay', separators='.,', hidesources=False ) )
pfig.get_data()
[{'name': 'maximum life span (years)', 'x': [42, 60, 180, 63, 230, 281, 365, 400, 148, 252, 63, 28, 68, 336, 100, 33, 19, 140, 170, 115, 31, 63, 21, 52, 164, 151, 38], 'y': [4.5, 14, 27, 28, 50, 30, 40, 28, 20, 39.3, 16.2, 9, 7.6, 46, 22.4, 16.3, 3.2, 12, 20.2, 27, 18, 13.7, 4.7, 9.8, 29, 20, 13]}, {'name': 'maximum life span (years) - fit', 'x': [19, 26.775510204081634, 34.55102040816327, 42.326530612244895, 50.10204081632653, 57.87755102040816, 65.65306122448979, 73.42857142857143, 81.20408163265306, 88.9795918367347, 96.75510204081633, 104.53061224489795, 112.3061224489796, 120.08163265306122, 127.85714285714286, 135.6326530612245, 143.40816326530611, 151.18367346938774, 158.9591836734694, 166.73469387755102, 174.51020408163265, 182.28571428571428, 190.0612244897959, 197.83673469387756, 205.6122448979592, 213.3877551020408, 221.16326530612244, 228.9387755102041, 236.71428571428572, 244.48979591836735, 252.26530612244898, 260.0408163265306, 267.81632653061223, 275.59183673469386, 283.3673469387755, 291.14285714285717, 298.9183673469388, 306.6938775510204, 314.46938775510205, 322.2448979591837, 330.0204081632653, 337.7959183673469, 345.57142857142856, 353.3469387755102, 361.1224489795918, 368.8979591836735, 376.6734693877551, 384.44897959183675, 392.2244897959184, 400], 'y': [7.5017678398522065, 8.91740585145299, 10.280204708119975, 11.592136648710301, 12.855100297616325, 14.070923412449769, 15.241365529167648, 16.36812050846799, 17.452818987140493, 18.497030737919733, 19.50226694125608, 20.469982372291966, 21.401577506208596, 22.29840054498987, 23.16174936853673, 23.992873412955387, 24.792975478738008, 25.56321347145231, 26.304702077459407, 27.018514377084937, 27.70568339757803, 28.36720360810559, 29.00403235894538, 29.617091266960834, 30.207267549362445, 30.775415307686202, 31.32235676384702, 31.848883450056075, 32.355757354324126, 32.843712023208525, 33.31345362339982, 33.76566196368431, 34.20099147876147, 34.62007217634016, 35.023510548884005, 35.41189045132576, 35.78577394602053, 36.14570211616095, 36.4921958488313, 36.825756588833954, 37.14686706437888, 37.45599198568668, 37.753578717515936, 38.040057926588304, 38.31584420484825, 38.58133666945938, 38.83691954040576, 39.08296269653401, 39.31982221084104, 39.547840865781865]}]
We're also working to allow matplotlib users to port visualizations to the web where both the data and the visualization are sourcable in the same location.
# decay and frequency for position and signal
w1 = 4.
w2 = 3/w1
a1 = 2
a2 = .5
# time, position, signal
t = np.linspace(0, 1, 400)
yt = np.e**(-a1*t)*np.cos(2*np.pi*w1*t)
sig = np.e**(1j*2*np.pi*w2*yt)
# open up a figure
fig2, ax = plt.subplots()
# define the color map we'll use for the imaginary axis
cm = plt.cm.get_cmap('rainbow')
# make those bubbles!
ax.scatter(t, yt, s=np.real(sig)**2*2000, c=np.imag(sig), cmap=cm)
# annotate in axes coordinates
ax.text(.97, .97, "Color = imag(sig)<br>Size = real(sig)",
transform=ax.transAxes,
horizontalalignment='right',
verticalalignment='top',
size=11)
# label axes
ax.set_ylabel('position')
ax.set_xlabel('time')
<matplotlib.text.Text at 0x10b5d7710>
py.iplot_mpl(fig2, filename='complex-exponential')
durl = 'http://datasets.flowingdata.com/crimeRatesByState2005.csv'
rdata = np.genfromtxt(durl,dtype='S8,f,f,f,f,f,f,f,i',delimiter=',')
rdata=rdata[2:] # cut titles/statistics
# pull out data
x = [data[1] for data in rdata] # murders per 100,000 people
y = [data[5] for data in rdata] # buglaries per 100,000 people
c = [data[6] for data in rdata] # larceny_theft per 100,000 people
s = [data[8] for data in rdata] # population
name = [data[0] for data in rdata] # state name
annotations = [[x[i], y[i], name[i]] for i in range(len(rdata))]
# making the scatter plot
fig3, ax = plt.subplots()
cm = plt.cm.get_cmap('RdBu_r') # define color map ('_r' means 'reverse')
al = 0.9
ax.scatter(x, y, c=c, cmap=cm, s=np.sqrt(s), linewidths=2, edgecolor='w', alpha=al)
# put in annotations
text_style = {"size": 11, "horizontalalignment": "center"}
for text_data in annotations:
ax.text(*text_data, **text_style) # *unpack_x_y_name, **unpack_size_alignment
ax.text(.97, .97, "Color = larceny-theft levels",
transform=ax.transAxes,
horizontalalignment='right',
verticalalignment='top',
size=11)
# label axes
ax.set_xlabel('Murders per 100,000 population')
ax.set_ylabel('Burglaries per 100,000 population')
<matplotlib.text.Text at 0x10be761d0>
py.iplot_mpl(fig3, filename='crime-time')
plotly.tools.embed('https://plot.ly/~etpinard/265')
# grab my ids so no one sees them in the NB
stream_ids = plotly.tools.get_credentials_file()['stream_ids']
# assemble my blank figure I'll be streaming to
fig = Figure()
fig['data'] += [Scatter(x=[], y=[], text=[], marker=Marker(size=[], color=[]), mode='markers',
stream=Stream(token=stream_ids[0], maxpoints=200))]
fig['layout'].update(#title="most recent tweets",
showlegend=False,
hovermode='closest',
xaxis=XAxis(title="number of statuses posted"),
yaxis=YAxis(title="number of followers"),
annotations=[Annotation(text='size = number of friends<br>color = clean/obscene',
x=.97,
y=.97,
xref='paper',
yref='paper',
showarrow=False)])
# show the url of this figure for reference
print py.plot(fig, filename='twitter', auto_open=False)
# assemble a list of naughty words used in english
res = requests.get('https://raw.githubusercontent.com/shutterstock/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words/master/en')
bad_words = res.content.splitlines()
# open up a Plotly stream
my_stream = py.Stream(stream_ids[0])
my_stream.open()
# open up a Twitter stream
twitter_auth = json.loads(open('../.twitter_auth').read())
t = twitter.TwitterStream(auth=twitter.OAuth(*twitter_auth))
iterator = t.statuses.sample()
tweet_keys = ['text', 'user', 'lang']
user_keys = ['followers_count', 'friends_count', 'statuses_count']
for tweet in iterator:
if all(k in tweet for k in tweet_keys) and all(k in tweet['user'] for k in user_keys):
if tweet['lang'] == 'en':
try:
text = str(tweet['text'])
except UnicodeEncodeError:
pass
else:
x = tweet['user']['statuses_count']
y = tweet['user']['followers_count']
s = np.log(1 + tweet['user']['friends_count'])*3
if any(word in text.lower() for word in bad_words):
c = '#FF4136'
title="--naughty--"
else:
c = '#2ECC40'
title=text[:30]
s_text = "friends: {}<br>{}".format(tweet['user']['friends_count'], text)
my_stream.write(Scatter(x=x, y=y, text=s_text, marker=Marker(size=s, color=c)),
layout=Layout(title=title))
time.sleep(.5)
# CSS styling within IPython notebook
from IPython.display import display, HTML
display(HTML(open('custom.css').read()))