from bokeh.plotting import * output_notebook() from bokeh.sampledata.autompg import autompg autompg.head(10) source = ColumnDataSource(autompg.to_dict("list")) #help(source) #help(source.data) plot = figure(plot_width=400, plot_height=400, title="MPG by Year", tools="pan,wheel_zoom,box_zoom,box_select,reset") plot.circle("yr", "mpg", size="cyl",color="blue", source=source), show(plot) fig1 = figure(plot_width=300, plot_height=300, title="MPG by Year", tools="pan,wheel_zoom,box_zoom,box_select,reset") fig1.circle("yr", "mpg", color="blue", source=source) fig2 = figure(plot_width=300, plot_height=300, title="HP vs. Displacement", tools="pan,wheel_zoom,box_zoom,box_select,reset") fig2.circle("hp", "displ", color="green", source=source) fig3 = figure(plot_width=300, plot_height=300, title="MPG vs. Displacement", tools="pan,wheel_zoom,box_zoom,box_select,reset") fig3.circle("mpg", "displ", size="cyl", line_color="red", fill_color=None, source=source) p = gridplot([[fig1, fig2], [None, fig3]]) show(p) fig4 = figure(plot_width=300, plot_height=300, tools="pan,wheel_zoom,box_zoom,box_select,reset") fig5 = figure(plot_width=300, plot_height=300, tools="pan,wheel_zoom,box_zoom,box_select,reset") fig6 = figure(plot_width=300, plot_height=300, tools="pan,wheel_zoom,box_zoom,box_select,reset") fig7 = figure(plot_width=300, plot_height=300, tools="pan,wheel_zoom,box_zoom,box_select,reset") # include some other plots here from bokeh.sampledata import download download() from bokeh.sampledata import us_counties, unemployment print(us_counties.data.items()[0]) print(unemployment.data.items()[0]) from collections import OrderedDict county_xs=[ us_counties.data[county_id]['lons'] for county_id in us_counties.data if us_counties.data[county_id]['state'] == 'ca' ] county_ys=[ us_counties.data[county_id]['lats'] for county_id in us_counties.data if us_counties.data[county_id]['state'] == 'ca' ] # D3 category10 colors colors =["#1f77b4","#ff7f0e","#2ca02c","#d62728","#9467bd","#8c564b","#e377c2","#7f7f7f","#bcbd22"] # light-to-dark encoding (try it) # colors = ["#f7fcfd","#e5f5f9","#ccece6","#99d8c9","#66c2a4","#41ae76","#238b45","#006d2c","#00441b"] rates=[] names = [] for county_id in us_counties.data: if us_counties.data[county_id]['state'] != 'ca': continue try: rate = unemployment.data[county_id] rates.append(rate) names.append(us_counties.data[county_id]['name']) except KeyError: rates.append(0) min_rate=min(rates) max_rate=max(rates) idx = map(lambda rate:min(int(rate-min_rate), 8),rates) county_colors = map(lambda id: colors[id],idx) alldata=ColumnDataSource({"name":names,"rate":rates}) fig8 = figure(plot_width=500, plot_height=500, title="California Unemployment 2009") fig8.patches(county_xs, county_ys, fill_color=county_colors, fill_alpha=0.7, line_color="white", line_width=0.5, source=alldata) #output_file("california.html") #save() show(fig8) %matplotlib inline import numpy as np import matplotlib import matplotlib.pyplot as plt import mpld3 from mpld3 import plugins, utils mpld3.enable_notebook() fig, ax = plt.subplots(3, 3, sharex="col", sharey="row", figsize=(8, 8)) fig.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95, hspace=0.1, wspace=0.1) name=["mpg","hp","displ"] data=autompg #data=autompg.head(100) for i in range(3): for j in range(3): points = ax[2 - i, j].scatter(data[name[j]], data[name[i]], c=data["yr"],s=40, alpha=0.6) # Here we connect the linked brush plugin plugins.connect(fig, plugins.LinkedBrush(points)) mpld3.display(fig) fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE')) N=len(data) scatter = ax.scatter(data["hp"], data["mpg"], c=data["yr"], s=data["cyl"]*10, alpha=0.3, cmap=plt.cm.jet) ax.grid(color='white', linestyle='solid') ax.set_title("Scatter Plot (with tooltips!)", size=20) labels = ['point {0}'.format(i + 1) for i in range(N)] #labels = [data["name"][i] for i in range(N)] tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels) mpld3.plugins.connect(fig, tooltip) mpld3.display(fig) mpld3.save_json(fig,"viz.json") mpld3.save_html(fig,"viz.html") %%html