Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Jupyter notebook Bokeh Examples.ipynb

636 views
Kernel: Python 2 (SageMath)
# We must use a special json encoder to get Bokeh to work in sagemath # from https://github.com/thomasfricke/sage-json-encoder # Thanks Thomas! import sage_json_encoder
from bokeh.plotting import figure, output_file, show # prepare some data x = [1, 2, 3, 4, 5] y = [6, 7, 2, 4, 5] # output to static HTML file output_file("examples/lines.html") # create a new plot with a title and axis labels p = figure(title="simple line example", x_axis_label='x', y_axis_label='y') # add a line renderer with legend and line thickness p.line(x, y, legend="Temp.", line_width=2) # show the results show(p)
%%HTML <iframe src='examples/lines.html' width='100%' height=600px>
from bokeh.plotting import figure, output_file, show # prepare some data x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0] y0 = [i**2 for i in x] y1 = [10**i for i in x] y2 = [10**(i**2) for i in x] # output to static HTML file output_file("examples/log_lines.html") # create a new plot p = figure( tools="pan,box_zoom,reset,save", y_axis_type="log", y_range=[0.001, 10**11], title="log axis example", x_axis_label='sections', y_axis_label='particles' ) # add some renderers p.line(x, x, legend="y=x") p.circle(x, x, legend="y=x", fill_color="white", size=8) p.line(x, y0, legend="y=x^2", line_width=3) p.line(x, y1, legend="y=10^x", line_color="red") p.circle(x, y1, legend="y=10^x", fill_color="red", line_color="red", size=6) p.line(x, y2, legend="y=10^x^2", line_color="orange", line_dash="4 4") # show the results show(p)
%%HTML <iframe src='examples/log_lines.html' width='100%' height=600px>
import numpy as np from bokeh.plotting import figure, output_file, show # prepare some data N = 4000 x = np.random.random(size=N) * 100 y = np.random.random(size=N) * 100 radii = np.random.random(size=N) * 1.5 colors = [ "#%02x%02x%02x" % (int(r), int(g), 150) for r, g in zip(50+2*x, 30+2*y) ] # output to static HTML file (with CDN resources) output_file("examples/color_scatter.html", title="color_scatter.py example", mode="cdn") TOOLS="resize,crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select" # create a new plot with the tools above, and explicit ranges p = figure(tools=TOOLS, x_range=(0,100), y_range=(0,100)) # add a circle renderer with vectorized colors and sizes p.circle(x,y, radius=radii, fill_color=colors, fill_alpha=0.6, line_color=None) # show the results show(p)
%%HTML <iframe src='examples/color_scatter.html' width='100%' height=600px>
import numpy as np from bokeh.layouts import gridplot from bokeh.plotting import figure, output_file, show # prepare some data N = 100 x = np.linspace(0, 4*np.pi, N) y0 = np.sin(x) y1 = np.cos(x) y2 = np.sin(x) + np.cos(x) # output to static HTML file output_file("examples/linked_panning.html") # create a new plot s1 = figure(width=250, plot_height=250, title=None) s1.circle(x, y0, size=10, color="navy", alpha=0.5) # NEW: create a new plot and share both ranges s2 = figure(width=250, height=250, x_range=s1.x_range, y_range=s1.y_range, title=None) s2.triangle(x, y1, size=10, color="firebrick", alpha=0.5) # NEW: create a new plot and share only one range s3 = figure(width=250, height=250, x_range=s1.x_range, title=None) s3.square(x, y2, size=10, color="olive", alpha=0.5) # NEW: put the subplots in a gridplot p = gridplot([[s1, s2, s3]], toolbar_location=None) # show the results show(p)
%%HTML <iframe src='examples/linked_panning.html' width='100%' height=250px>
import numpy as np from bokeh.plotting import * from bokeh.models import ColumnDataSource # prepare some date N = 300 x = np.linspace(0, 4*np.pi, N) y0 = np.sin(x) y1 = np.cos(x) # output to static HTML file output_file("examples/linked_brushing.html") # NEW: create a column data source for the plots to share source = ColumnDataSource(data=dict(x=x, y0=y0, y1=y1)) TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select,lasso_select" # create a new plot and add a renderer left = figure(tools=TOOLS, width=350, height=350, title=None) left.circle('x', 'y0', source=source) # create another new plot and add a renderer right = figure(tools=TOOLS, width=350, height=350, title=None) right.circle('x', 'y1', source=source) # put the subplots in a gridplot p = gridplot([[left, right]]) # show the results show(p)
%%HTML <iframe src='examples/linked_brushing.html' width='100%' height=400px>
import numpy as np from bokeh.plotting import figure, output_file, show from bokeh.sampledata.stocks import AAPL # prepare some data aapl = np.array(AAPL['adj_close']) aapl_dates = np.array(AAPL['date'], dtype=np.datetime64) window_size = 30 window = np.ones(window_size)/float(window_size) aapl_avg = np.convolve(aapl, window, 'same') # output to static HTML file output_file("examples/stocks.html", title="stocks.py example") # create a new plot with a a datetime axis type p = figure(width=800, height=350, x_axis_type="datetime") # add renderers p.circle(aapl_dates, aapl, size=4, color='darkgrey', alpha=0.2, legend='close') p.line(aapl_dates, aapl_avg, color='navy', legend='avg') # NEW: customize by setting attributes p.title.text = "AAPL One-Month Average" p.legend.location = "top_left" p.grid.grid_line_alpha=0 p.xaxis.axis_label = 'Date' p.yaxis.axis_label = 'Price' p.ygrid.band_fill_color="olive" p.ygrid.band_fill_alpha = 0.1 # show the results show(p)
%%HTML <iframe src='examples/stocks.html' width='100%' height=350px>
# Lorenz example taken from Gallery # http://bokeh.pydata.org/en/latest/docs/gallery/lorenz.html import numpy as np from scipy.integrate import odeint from bokeh.plotting import figure, show, output_file sigma = 10 rho = 28 beta = 8.0/3 theta = 3 * np.pi / 4 def lorenz(xyz, t): x, y, z = xyz x_dot = sigma * (y - x) y_dot = x * rho - x * z - y z_dot = x * y - beta* z return [x_dot, y_dot, z_dot] initial = (-10, -7, 35) t = np.arange(0, 100, 0.006) solution = odeint(lorenz, initial, t) x = solution[:, 0] y = solution[:, 1] z = solution[:, 2] xprime = np.cos(theta) * x - np.sin(theta) * y colors = ["#C6DBEF", "#9ECAE1", "#6BAED6", "#4292C6", "#2171B5", "#08519C", "#08306B",] p = figure(title="lorenz example") p.multi_line(np.array_split(xprime, 7), np.array_split(z, 7), line_color=colors, line_alpha=0.8, line_width=1.5) output_file("examples/lorenz.html", title="lorenz.py example") show(p) # open a browser
%%HTML <iframe src='examples/lorenz.html' width='100%' height=600px>