import plotly
import plotly.plotly as py
import plotly.tools as tls
from plotly.graph_objs import * # note, __all__ is defined to make the `import *` safe for use.
print plotly.check_version()
py.sign_in('PythonAPI', 'ubpiol2cve')
import numpy as np
import math
import datetime
import random
import urllib2, StringIO, csv
url = 'https://gist.github.com/chriddyp/8818473/raw/d8c73ff66a190a84eb8c6c19df4d8865673234ca/2007gapminder.csv'
response = urllib2.urlopen(url).read()
output = StringIO.StringIO(response)
cr = csv.reader(output)
def tryFloat(d):
try:
return float(d)
except ValueError:
return d
data = [[tryFloat(dij) for dij in di] for di in cr]
for row in data[0:10]:
print row
fig = Figure()
for continent in ['Asia', 'Europe', 'Africa', 'Americas', 'Oceania']:
fig['data'] += [Scatter(name=continent, # the "name" of this series is the Continent
x=[row[4] for row in data if row[2] == continent], # "x" data is GDP Per Capit
y=[row[3] for row in data if row[2] == continent], # "y" data is Life Expectancy
text=[row[0] for row in data if row[2] == continent], # "text" data is the Country
mode='markers',
# specify the style of the individual scatter points
marker=Marker(# scale the "marker" size to each Country's population
size=[np.sqrt(row[1])/1.e3 for row in data if row[2] == continent],
sizemode='area',
sizeref=0.05,
opacity=0.55
)
)]
fig['layout'] = Layout(xaxis=XAxis(title='GDP Per Capita'),
yaxis=YAxis(title='Life Expectancy'),
title='Hans Rosling Bubble Chart
2007'
)
py.iplot(fig, filename='My first plotly graph')
tls.embed('ChrisP', '21')
t = [i*2*np.pi/100 for i in range(100)]
trace = Scatter(x=[16.*np.sin(ti)**3 for ti in t],
y=[13.*np.cos(ti)-5*np.cos(2*ti)-2*np.cos(3*ti)-np.cos(4*ti) for ti in t],
line=Line(color='red', width=6))
xaxis = XAxis(ticks='', showline=False, zeroline=False)
yaxis = YAxis(ticks='', showline=False, zeroline=False)
fig = Figure(data=[trace], layout=Layout(xaxis=xaxis, yaxis=yaxis))
py.iplot(fig, filename="public hearts", world_readable=True)
py.iplot(fig, filename="private hearts", world_readable=False)
py.plot(fig, filename="unopened hearts", auto_open=False)
fig = Figure() # this creates `data=Data()` and `layout=Layout()` objects for you if you don't specify them
fig['data'] += [Box(y=np.random.randn(50), boxpoints='all', jitter=0.5, pointpos=-1.8) for i in range(10)]
fig['layout'].update(title="NumPy Boxes", showlegend=False)
fig['layout']['yaxis'] = XAxis(zeroline=False, ticks='', showline=False)
fig['layout']['xaxis'] = YAxis(ticks='', showgrid=False, showline=False)
py.iplot(fig, filename='numpy boxes')
N = 500
now = datetime.datetime.now()
fig = Figure()
trace = Scatter(mode='markers')
trace['x'] = [datetime.timedelta(hours=i) + now for i in range(N)]
trace['y'] = [np.sin(i*5*np.pi/N)*np.exp(-2.*i/N)+np.random.random()/3. for i in range(N)]
fig['data'] += [trace]
fig['layout']['title'] = 'Datetime Decay'
# let's add a note about this plot!
text = "The date-formatted x-axis will increase it\'s time-resolution when you zoom." \
"
Click-and-drag your mouse on the plot to see how it responds!"
my_note = Annotation(text=text, xref='paper', yref='paper', showarrow=False, x=.9, y=.9)
fig['layout']['annotations']=Annotations([my_note])
py.iplot(fig, filename='Datetime Decay')
x = np.linspace(-7, 7, 100)
y = [x]
y += [y[0] - x**3/np.math.factorial(3)]
y += [y[1] + x**5/np.math.factorial(5)]
y += [y[2] - x**7/np.math.factorial(7)]
y += [y[3] + x**9/np.math.factorial(9)]
linestyle1 = Line(color='#7FDBFF', dash='dot')
linestyle2 = Line(color='#0074D9')
fig = Figure()
fig['data'] += [Scatter(x=x, y=approx, line=linestyle1) for approx in y]
fig['data'] += [Scatter(x=x, y=np.sin(x), line=linestyle2)]
fig['data'].update([{'name':'trace{}'.format(i)} for i in range(len(fig['data']))]) # add some labels using Data's update method
fig['layout']['title'] = '$\\sin(x)=\sum\limits_{k=0}^{\infty}\dfrac{(-1)^k x^{1+2k}}{(1+2k)!}$'
fig['layout']['xaxis'] = XAxis(showgrid=False, showline=False, zeroline=False, ticks='')
fig['layout']['yaxis'] = YAxis(range=[-4.5, 4.5], zeroline=False, ticks='', showline=False)
fig['layout']['showlegend'] = False
py.iplot(fig, filename='LaTeX')
fig = Figure()
fig['layout'].update(xaxis1=XAxis(domain=[0,0.8]),
xaxis2=XAxis(domain=[0.85,1.0]),
yaxis1=YAxis(domain=[0,0.8]),
yaxis2=YAxis(domain=[0.85,1.0]))
fig['layout'].update(title="i <3 subplots", showlegend=False)
for key in ['xaxis1', 'xaxis2', 'yaxis1', 'yaxis2']:
fig['layout'][key].update(showgrid=False, showline=False, zeroline=False)
x0 = np.concatenate([np.random.randn(400), np.random.randn(400)+6])
y0 = np.random.rayleigh(size=800)
ybins = YBins(start=0, end=5, size=.2)
xbins = XBins(start=-5, end=10, size=.6)
hist2d = Histogram2d(x=x0, y=y0, ybins=ybins, xbins=xbins)
marker = Marker(color="rgb(31, 119, 180)")
# histy = Histogramy(y=hist2d['y'], bardir='h', xaxis='x2', yaxis='y1', autobiny=False, ybins=ybins, marker=marker)
histx1 = Histogramx(x=hist2d['y'], bardir='h', xaxis='x2', yaxis='y1', autobinx=False, xbins=ybins, marker=marker)
histx = Histogramx(x=hist2d['x'], xaxis='x1', yaxis='y2', autobinx=False, xbins=xbins, marker=marker)
fig['data'] += [hist2d, histx1, histx]
py.iplot(fig, filename='histogram subplots')
fig = tls.get_subplots(rows=4, columns=4, print_grid=True)
fig['data'] += [Scatter(x=np.random.rand(20),
y=np.random.rand(20),
name='plot{}'.format(iii),
mode='markers',
xaxis='x{}'.format(iii),
yaxis='y{}'.format(iii))
for iii in range(1, 17)]
fig['layout'].update(title='i really <3 subplots')
py.iplot(fig, filename='a bunch of subplots')
help(Scatter)