CoCalc Shared Files105_talk / bok.py
Author: Hunter Johnson
Description: A tutorial in basic plotting for a college algebra class.
1from __future__ import print_function
2
3from bokeh.document import Document
4from bokeh.embed import file_html
5from bokeh.util.browser import view
6from bokeh.resources import INLINE
7from bokeh.models.glyphs import Circle, Line
8from bokeh.models import (ColumnDataSource, Range1d, Plot, LinearAxis, Grid,
9    HoverTool, CrosshairTool, TapTool, WheelZoomTool, Legend, LegendItem, CustomJS)
10
11plot = Plot(x_range=Range1d(-10, 10), y_range=Range1d(-10, 10), plot_width=600, plot_height=600, toolbar_sticky=False)
12
13xaxis = LinearAxis()
14yaxis = LinearAxis()
15xgrid = Grid(dimension=0, ticker=xaxis.ticker)
16ygrid = Grid(dimension=1, ticker=yaxis.ticker)
17
22
23def fds(x, y, e, n):
24    d = [
25        (x+0*e, y+0*e, 1, "red",    "%s00" % n),
26        (x+1*e, y+1*e, 1, "blue",   "%s01" % n),
27        (x+2*e, y+2*e, 1, "green",  "%s02" % n),
28        (x+3*e, y+3*e, 1, "violet", "%s03" % n),
29        (x+4*e, y+4*e, 1, "pink",   "%s04" % n),
30        (x+5*e, y+5*e, 1, "black",  "%s05" % n),
31        (x+6*e, y+6*e, 1, "gray",   "%s06" % n),
32        (x+7*e, y+7*e, 1, "olive",  "%s07" % n),
33        (x+8*e, y+8*e, 1, "yellow", "%s08" % n),
34        (x+9*e, y+9*e, 1, "orange", "%s09" % n),
35    ]
36    f = lambda i: [ t[i] for t in d ]
37    return dict(x=f(0), y=f(1), s=f(2), c=f(3), name=f(4))
38
39ds1 = ColumnDataSource(data=fds(0, 0, 0.1, "c"))
40cr1 = plot.add_glyph(ds1, Circle(x="x", y="y", radius="s", fill_color="c", line_color="c"))
41
42ds2 = ColumnDataSource(data=fds(-5, 5, 0.5, "d"))
43cr2 = plot.add_glyph(ds2, Circle(x="x", y="y", radius="s", fill_color="c", line_color="c"))
44ln2 = plot.add_glyph(ds2, Line(x="x", y="y", line_width=3, line_color="red"))
45
46ds3 = ColumnDataSource(data=fds(5, 5, 0.0, "e"))
47cr3 = plot.add_glyph(ds3, Circle(x="x", y="y", radius="s", fill_color="c", line_color="c"))
48
49tooltips = "<b>@name</b> = (@x{0.00}, @y{0.00})"
50
51hover = HoverTool(tooltips=tooltips, renderers=[cr1, cr2, ln2, cr3], point_policy="follow_mouse")
53
54crosshair = CrosshairTool()
56
57tap = TapTool(renderers=[cr1, cr2, cr3], callback=CustomJS(code="console.log('TAP')"))
59
60wheelzoom = WheelZoomTool()
62
63legends = lambda: [
64    LegendItem(label="CR1", renderers=[cr1]),
65    LegendItem(label="CR2", renderers=[cr2, ln2]),
66    LegendItem(label="CR3", renderers=[cr3]),
67]
68legend = lambda **kwargs: Legend(background_fill_alpha=0.7, items=legends(), click_policy="hide", **kwargs)
69
75plot.add_layout(legend(location=(0, 0), orientation="vertical", name="(0, 0)"))
76plot.add_layout(legend(location="center", orientation="horizontal", name="above"), 'above')
77plot.add_layout(legend(location="center", orientation="horizontal", name="below"), 'below')
78plot.add_layout(legend(location="center", orientation="vertical", name="left"), 'left')
79plot.add_layout(legend(location="center", orientation="vertical", name="right"), 'right')
80
81doc = Document()