Skip to content

Instantly share code, notes, and snippets.

@bpostlethwaite
Last active December 21, 2015 00:49
Show Gist options
  • Save bpostlethwaite/6223259 to your computer and use it in GitHub Desktop.
Save bpostlethwaite/6223259 to your computer and use it in GitHub Desktop.
import numpy as np
import plotly
py = plotly.plotly(username='bpostlethwaite', key='xxxxxxxxxx')
numletters = 5
letterblock = 100.
pad = 10.
lp = letterblock + pad
base = 50.
letterheight = 200.
n = 600
def circle(xa, ya, rad, theta = 2*np.pi, density = 1):
theta = np.linspace(0, theta, np.round(n * density))
x = xa + rad*np.cos(theta)
y = ya + rad*np.sin(theta)
return (x, y)
def parabola(x1, width, base, height, density = 1):
x2 = x1 + width
a = (x2 - x1)/2
x = np.linspace(x1, x2, np.round(n * density))
xa = np.linspace(x1, x2, np.round(n * density)) - (a + x1)
y = (xa)**4
y = - y * height / np.max(y) + base + height
return (x, y)
def vertline(x, base, height, density = 1):
return (x * np.ones( np.round(n * density) ),
np.linspace(base, base + height, np.round(n * density)))
def horzline(y, start, width, density = 1):
return (np.linspace(start, start + width, np.round(n * density)), y * np.ones(np.round(n * density)))
def absline(x1, width, base, height, density = 1):
x = np.linspace(x1, x1 + width, np.round(n * density))
y = np.abs(x - (x1 + 0.5*width))
y = y * height / np.max(y) + base
return (x, y)
def scatter((x,y)):
xs = np.zeros(len(x))
ys = np.zeros(len(y))
for i in range(len(x)):
xs[i] = np.random.normal(loc=x[i], scale=8.0, size=1)
ys[i] = np.random.normal(loc=y[i], scale=8.0, size=1)
return (xs, ys)
# Construct Letters
xy = []
xy.append(vertline(pad, base, letterheight)) # h line
xy.append(parabola(pad, letterblock, base, 0.5 * letterheight)) # h parabola
xy.append(vertline(lp + pad, base, 0.5 * letterheight, density = 0.5)) # i line
xy.append(circle(lp + pad, base + 0.6 * letterheight, 0.1 * letterheight, density = 0.5)) # i dot
xy.append(circle(2.5*lp, base + letterblock, 0.8*letterblock, density = 2)) # phi
xy.append(vertline(2.5*lp, base, 2*letterblock)) # phi line
xy.append(horzline(base, 2.5*lp - 0.4*letterblock, 0.8 * letterblock, density = 0.25)) # phi line cap
xy.append(horzline(base + 2*letterblock, 2.5*lp - 0.4*letterblock, 0.8 * letterblock, density = 0.25)) # phi line cap
xy.append(vertline(3.5*lp, base + 0.8 * letterheight, 0.1 * letterheight, density = 0.2)) # comma
xy.append(absline(3.5*lp, letterblock, base, 0.5 * letterheight)) # v
xy.append(circle(5 * lp, base + 0.4*letterblock, 0.4*letterblock, 1.8 * np.pi)) # e circle
xy.append(horzline(base + 0.4*letterblock, 5 * lp - 0.4 * letterblock, 0.8 * letterblock, density = 0.5)) # e line
# Construct Dictionaries
traces = []
xdist = np.array([])
ydist = np.array([])
for t in xy:
# Randomly distribute
ts = scatter(t)
# Create some synthetic distribution
xdist = np.hstack( (xdist, (ts[0])) )
ydist = np.hstack( (ydist, (ts[1])) )
traces.append(
{'x': t[0],
'y': t[1],
'type': 'scatter',
'mode': 'lines',
'line':{
'color': 'lightblue',
'width': 4}
})
# Create Red Line Data
xdistc = np.cumsum( np.diff(np.sort(xdist)) )
xdistc *= (base + letterheight) / np.max(xdistc)
traces.append(
{'x': np.linspace(0, 5.5*lp, len(xdistc)),
'y': xdistc + 0.5 * base,
'type': 'scatter',
'mode': 'lines',
'line':{
'color': 'red',
'width': 4}
})
# Histogram of scattered data
traces.append({
'x': xdist,
'y': ydist,
'type':'histogram2d',
'autobinx': False,
'xbins': {
'start': 0,
'end': 5.5 * lp,
'size': 5},
'autobiny': False,
'ybins': {
'start': 0.5 * base,
'end': 275,
'size': 5},
'histnorm': 'probability'
})
layout = {
"title": "Plotly plot submission: High Five",
"titlefont": {
"color": "rgb(87,54,255)",
"size": 28
},
"xaxis":
{
"title": "time spent with plotly",
"titlefont": {
"color": "rgb(87,54,255)",
"size": 22
},
"autorange": False,
"range": [0, 5.5*lp],
"showticklabels": True,
"ticks": "none",
"showgrid": True,
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white"
},
"yaxis":
{
"title": "enthusiasm for plotly",
"titlefont": {
"color": "rgb(87,54,255)",
"size": 22},
"autorange": False,
"range":[0.5 * base, 275],
"gridcolor": "white",
"linecolor": "white",
"ticks": "none",
"showticklabels": False
},
"showlegend": False
}
r = py.plot(traces)
response = py.layout(layout, filename = r['filename'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment