# 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)
plotly.tools.embed('https://plot.ly/~lucagiuliano/3/life-span-and-gestation-time-in-mammals/')
pfig = py.get_figure('https://plot.ly/~lucagiuliano/3/life-span-and-gestation-time-in-mammals/')
print pfig.to_string(pretty=True)
pfig.get_data()
# 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)
Size = real(sig)",
transform=ax.transAxes,
horizontalalignment='right',
verticalalignment='top',
size=11)
# label axes
ax.set_ylabel('position')
ax.set_xlabel('time')
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')
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
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: {}
{}".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()))