from IPython.display import IFrame, HTML
from IPython.core.display import display
display(IFrame("http://demographics.coopercenter.org/DotMap/index.html", '800px', '600px'))
display(IFrame("http://www.nytimes.com/interactive/2014/07/31/world/africa/ebola-virus-outbreak-qa.html", '800px', '600px'))
import bokeh.plotting as bk
bk.output_notebook()
import numpy as np
x = np.linspace(-6, 6, 100)
y = np.random.normal(0.3*x, 1)
bk.circle(x, y, color="red", plot_width=500, plot_height=500)
bk.show()
from scipy.stats import expon
theta = 1
measured = np.random.exponential(theta, 1000)
hist, edges = np.histogram(measured, density=True, bins=50)
bk.hold(True)
bk.figure(title="Exponential Distribution (θ=1)",tools="previewsave",
background_fill="#E8DDCB")
bk.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], fill_color="#036564", line_color="#033649")
x = np.linspace(0, 10, 1000)
bk.line(x, expon.pdf(x, scale=1), line_color="#D95B43", line_width=8, alpha=0.7, legend="PDF")
bk.line(x, expon.cdf(x, scale=1), line_color="white", line_width=2, alpha=0.7, legend="CDF")
bk.legend().orientation = "top_right"
bk.hold(False)
bk.show()
from bokeh.sampledata.autompg import autompg
grouped = autompg.groupby("yr")
mpg = grouped["mpg"]
mpg_avg = mpg.mean()
mpg_std = mpg.std()
years = np.asarray(grouped.groups.keys())
american = autompg[autompg["origin"]==1]
japanese = autompg[autompg["origin"]==3]
american.head(10)
bk.hold(True)
bk.figure(title='Automobile mileage by year and country')
bk.quad(left=years-0.4, right=years+0.4, bottom=mpg_avg-mpg_std, top=mpg_avg+mpg_std, fill_alpha=0.4)
# Add Japanese cars as circles
bk.circle(x=np.asarray(japanese["yr"]),
y=np.asarray(japanese["mpg"]),
size=8, alpha=0.4, line_color="red", fill_color=None, line_width=2)
# Add American cars as triangles
bk.triangle(x=np.asarray(american["yr"]),
y=np.asarray(american["mpg"]),
size=8, alpha=0.4, line_color="blue", fill_color=None, line_width=2)
xax, yax = bk.axis()
xax.axis_label = 'Year'
yax.axis_label = 'MPG'
bk.hold(False)
bk.show()
source = bk.ColumnDataSource(autompg.to_dict("list"))
source.add(autompg["yr"], name="yr")
plot_config = dict(plot_width=300, plot_height=300, tools="pan, wheel_zoom, box_zoom, select")
bk.gridplot([[
bk.circle("yr", "mpg", color="blue", title="MPG by Year",
source=source, **plot_config),
bk.circle("hp", "displ", color="green", title="HP vs. Displacement",
source=source, **plot_config),
bk.circle("mpg", "displ", size="cyl", line_color="red", title="MPG vs. Displacement",
fill_color=None, source=source, **plot_config)
]])
bk.show()
import pandas as pd
from bokeh.objects import HoverTool
from bokeh.sampledata.unemployment1948 import data
from collections import OrderedDict
data['Year'] = [str(x) for x in data['Year']]
years = list(data['Year'])
months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
data = data.set_index('Year')
colors = [
"#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce",
"#ddb7b1", "#cc7878", "#933b41", "#550b1d"
]
month = []
year = []
color = []
rate = []
for y in years:
for m in months:
month.append(m)
year.append(y)
monthly_rate = data[m][y]
rate.append(monthly_rate)
color.append(colors[min(int(monthly_rate)-2, 8)])
source = bk.ColumnDataSource(
data=dict(
month=month,
year=year,
color=color,
rate=rate,
)
)
bk.figure()
bk.rect('year', 'month', 0.95, 0.95, source=source,
x_axis_location="above",
x_range=years, y_range=list(reversed(months)),
color='color', line_color=None,
tools="resize,hover", title="US Unemployment (1948 - 2013)",
plot_width=900, plot_height=400)
bk.grid().grid_line_color = None
bk.axis().axis_line_color = None
bk.axis().major_tick_line_color = None
bk.axis().major_label_text_font_size = "5pt"
bk.axis().major_label_standoff = 0
bk.xaxis().major_label_orientation = np.pi/3
hover = bk.curplot().tools[1]
hover.tooltips = OrderedDict([
('date', '@month @year'),
('rate', '@rate'),
])
bk.show()
from bokeh.sampledata import us_counties, unemployment
from collections import OrderedDict
county_xs=[
us_counties.data[code]['lons'] for code in us_counties.data
if us_counties.data[code]['state'] == 'tx'
]
county_ys=[
us_counties.data[code]['lats'] for code in us_counties.data
if us_counties.data[code]['state'] == 'tx'
]
colors = ["#F1EEF6", "#D4B9DA", "#C994C7", "#DF65B0", "#DD1C77", "#980043"]
county_colors = []
for county_id in us_counties.data:
if us_counties.data[county_id]['state'] != 'tx':
continue
try:
rate = unemployment.data[county_id]
idx = min(int(rate/2), 5)
county_colors.append(colors[idx])
except KeyError:
county_colors.append("black")
bk.patches(county_xs, county_ys, fill_color=county_colors, fill_alpha=0.7,
tools="pan,wheel_zoom,box_zoom,reset,hover,previewsave",
line_color="white", line_width=0.5,
title="Texas Unemployment 2009")
hover = bk.curplot().select(dict(type=HoverTool))
hover.tooltips = OrderedDict([
("index", "$index"),
("(x,y)", "($x, $y)"),
("fill color", "$color[hex, swatch]:fill_color"),
])
bk.show()
normal = np.random.standard_normal(1000)
student_t = np.random.standard_t(6, 1000)
distributions = pd.DataFrame({'Normal': normal, 'Student-T': student_t})
from bokeh.charts import Histogram
hist = Histogram(distributions, bins=np.sqrt(len(normal)), notebook=True)
hist.title("Histograms").ylabel("frequency").legend(True).width(600).height(300)
hist.show()
from collections import OrderedDict
from bokeh.charts import Scatter
from bokeh.sampledata.iris import flowers
setosa = flowers[(flowers.species == "setosa")][["petal_length", "petal_width"]]
versicolor = flowers[(flowers.species == "versicolor")][["petal_length", "petal_width"]]
virginica = flowers[(flowers.species == "virginica")][["petal_length", "petal_width"]]
xyvalues = OrderedDict([("setosa", setosa.values),
("versicolor", versicolor.values),
("virginica", virginica.values)])
scatter = Scatter(xyvalues)
scatter.title("iris dataset, dict_input").xlabel("petal_length").ylabel("petal_width").legend("top_left")
scatter.width(600).height(400).notebook().show()
titanic = pd.read_csv('http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic3.csv')
%matplotlib inline
from ggplot import ggplot, aes, geom_density
from bokeh import mpl
import matplotlib.pyplot as plt
g = ggplot(titanic, aes(x='age', color='pclass')) + geom_density()
g.draw()
plt.title("Plot of titanic passenger age distribution by class")
mpl.to_bokeh(name="density")
import seaborn as sns
# We generated random data
titanic = titanic.dropna(subset=['age'])
# And then just call the violinplot from Seaborn
sns.violinplot(titanic.age, groupby=titanic.pclass, color="Set3")
plt.title("Distribution of age by passenger class")
mpl.to_bokeh(name="violin")
from __future__ import division
def mandel(x, y, max_iters):
"""
Given the real and imaginary parts of a complex number,
determine if it is a candidate for membership in the Mandelbrot
set given a fixed number of iterations.
"""
c = complex(x, y)
z = 0.0j
for i in range(max_iters):
z = z*z + c
if (z.real*z.real + z.imag*z.imag) >= 4:
return i
return max_iters
def create_fractal(min_x, max_x, min_y, max_y, image, iters):
height = image.shape[0]
width = image.shape[1]
pixel_size_x = (max_x - min_x) / width
pixel_size_y = (max_y - min_y) / height
for x in range(width):
real = min_x + x * pixel_size_x
for y in range(height):
imag = min_y + y * pixel_size_y
color = mandel(real, imag, iters)
image[y, x] = color
min_x = -2.0
max_x = 1.0
min_y = -1.0
max_y = 1.0
img = np.zeros((1024, 1536), dtype = np.uint8)
create_fractal(min_x, max_x, min_y, max_y, img, 20)
bk.image(image=[img],
x=[min_x],
y=[min_y],
dw=[max_x-min_x],
dh=[max_y-min_y],
palette=["Spectral-11"],
x_range = [min_x, max_x],
y_range = [min_y, max_y],
title="Mandelbrot",
tools="pan,wheel_zoom,box_zoom,reset,previewsave",
plot_width=900,
plot_height=600
)
bk.show()
from bokeh.objects import Range1d
from StringIO import StringIO
from math import log, sqrt
from collections import OrderedDict
antibiotics = """
bacteria, penicillin, streptomycin, neomycin, gram
Mycobacterium tuberculosis, 800, 5, 2, negative
Salmonella schottmuelleri, 10, 0.8, 0.09, negative
Proteus vulgaris, 3, 0.1, 0.1, negative
Klebsiella pneumoniae, 850, 1.2, 1, negative
Brucella abortus, 1, 2, 0.02, negative
Pseudomonas aeruginosa, 850, 2, 0.4, negative
Escherichia coli, 100, 0.4, 0.1, negative
Salmonella (Eberthella) typhosa, 1, 0.4, 0.008, negative
Aerobacter aerogenes, 870, 1, 1.6, negative
Brucella antracis, 0.001, 0.01, 0.007, positive
Streptococcus fecalis, 1, 1, 0.1, positive
Staphylococcus aureus, 0.03, 0.03, 0.001, positive
Staphylococcus albus, 0.007, 0.1, 0.001, positive
Streptococcus hemolyticus, 0.001, 14, 10, positive
Streptococcus viridans, 0.005, 10, 40, positive
Diplococcus pneumoniae, 0.005, 11, 10, positive
"""
drug_color = OrderedDict([
("Penicillin", "#0d3362"),
("Streptomycin", "#c64737"),
("Neomycin", "black" ),
])
gram_color = {
"positive" : "#aeaeb8",
"negative" : "#e69584",
}
df = pd.read_csv(StringIO(antibiotics), skiprows=1, skipinitialspace=True)
width = 800
height = 800
inner_radius = 90
outer_radius = 300 - 10
minr = np.sqrt(log(.001 * 1E4))
maxr = np.sqrt(log(1000 * 1E4))
a = (outer_radius - inner_radius) / (minr - maxr)
b = inner_radius - a * maxr
def rad(mic):
return a * np.sqrt(np.log(mic * 1E4)) + b
big_angle = 2.0 * np.pi / (len(df) + 1)
small_angle = big_angle / 7
bk.figure()
bk.hold(True)
x = np.zeros(len(df))
y = np.zeros(len(df))
angles = np.pi/2 - big_angle/2 - df.index.values*big_angle
colors = [gram_color[gram] for gram in df.gram]
bk.annular_wedge(
x, y, inner_radius, outer_radius, -big_angle+angles, angles, color=colors,
plot_width=width, plot_height=height, title="", tools="", x_axis_type=None, y_axis_type=None
)
plot = bk.curplot()
plot.x_range = Range1d(start=-420, end=420)
plot.y_range = Range1d(start=-420, end=420)
plot.min_border = 0
plot.background_fill = "#f0e1d2"
plot.border_fill = "#f0e1d2"
plot.outline_line_color = None
bk.xgrid().grid_line_color = None
bk.ygrid().grid_line_color = None
bk.annular_wedge(
x, y, inner_radius, rad(df.penicillin), -big_angle+angles + 5*small_angle, -big_angle+angles+6*small_angle, color=drug_color['Penicillin'],
)
bk.annular_wedge(
x, y, inner_radius, rad(df.streptomycin), -big_angle+angles + 3*small_angle, -big_angle+angles+4*small_angle, color=drug_color['Streptomycin'],
)
bk.annular_wedge(
x, y, inner_radius, rad(df.neomycin), -big_angle+angles + 1*small_angle, -big_angle+angles+2*small_angle, color=drug_color['Neomycin'],
)
labels = np.power(10.0, np.arange(-3, 4))
radii = a * np.sqrt(np.log(labels * 1E4)) + b
bk.circle(x, y, radius=radii, fill_color=None, line_color="white")
bk.text(x[:-1], radii[:-1], [str(r) for r in labels[:-1]], angle=0, text_font_size="8pt", text_align="center", text_baseline="middle")
bk.annular_wedge(
x, y, inner_radius-10, outer_radius+10, -big_angle+angles, -big_angle+angles, color="black",
)
xr = radii[0]*np.cos(np.array(-big_angle/2 + angles))
yr = radii[0]*np.sin(np.array(-big_angle/2 + angles))
label_angle = np.array(-big_angle/2+angles)
label_angle[label_angle < -np.pi/2] += np.pi # easier to read labels on the left side
bk.text(xr, yr, df.bacteria, angle=label_angle, text_font_size="9pt", text_align="center", text_baseline="middle")
bk.circle([-40, -40], [-370, -390], color=gram_color.values(), radius=5)
bk.text([-30, -30], [-370, -390], text=["Gram-" + x for x in gram_color.keys()],
angle=0, text_font_size="7pt", text_align="left", text_baseline="middle")
bk.rect([-40, -40, -40], [18, 0, -18], width=30, height=13, color=drug_color.values())
bk.text([-15, -15, -15], [18, 0, -18], text=drug_color.keys(), angle=0,
text_font_size="9pt", text_align="left", text_baseline="middle")
bk.hold(False)
bk.show()
from bokeh.plotting import square, output_server, show
from bokeh.objects import ServerDataSource
import bokeh.transforms.ar_downsample as ar
output_server("Census")
# 2010 US Census tracts
source = ServerDataSource(data_url="/defaultuser/CensusTracts.hdf5", owner_username="defaultuser")
plot = square(
'LON', 'LAT',
source=source,
plot_width=600,
plot_height=400,
title="Census Tracts")
ar.heatmap(plot, low=(255, 200, 200), points=True, title="Census Tracts (Server Colors)")
show()
ar.contours(plot, title="ISO Contours")
show()
from IPython.core.display import HTML
import urllib
def css_styling():
styles = urllib.urlopen('https://raw.githubusercontent.com/fonnesbeck/Bios366/master/notebooks/styles/custom.css').read()
return HTML(styles)
css_styling()