Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

A tutorial in basic plotting for a college algebra class.

Views: 1135
License: GPL3
1
from __future__ import print_function
2
3
from bokeh.document import Document
4
from bokeh.embed import file_html
5
from bokeh.util.browser import view
6
from bokeh.resources import INLINE
7
from bokeh.models.glyphs import Circle, Line
8
from bokeh.models import (ColumnDataSource, Range1d, Plot, LinearAxis, Grid,
9
HoverTool, CrosshairTool, TapTool, WheelZoomTool, Legend, LegendItem, CustomJS)
10
11
plot = Plot(x_range=Range1d(-10, 10), y_range=Range1d(-10, 10), plot_width=600, plot_height=600, toolbar_sticky=False)
12
13
xaxis = LinearAxis()
14
yaxis = LinearAxis()
15
xgrid = Grid(dimension=0, ticker=xaxis.ticker)
16
ygrid = Grid(dimension=1, ticker=yaxis.ticker)
17
18
plot.add_layout(xaxis, "below")
19
plot.add_layout(yaxis, "left")
20
plot.add_layout(xgrid)
21
plot.add_layout(ygrid)
22
23
def 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
39
ds1 = ColumnDataSource(data=fds(0, 0, 0.1, "c"))
40
cr1 = plot.add_glyph(ds1, Circle(x="x", y="y", radius="s", fill_color="c", line_color="c"))
41
42
ds2 = ColumnDataSource(data=fds(-5, 5, 0.5, "d"))
43
cr2 = plot.add_glyph(ds2, Circle(x="x", y="y", radius="s", fill_color="c", line_color="c"))
44
ln2 = plot.add_glyph(ds2, Line(x="x", y="y", line_width=3, line_color="red"))
45
46
ds3 = ColumnDataSource(data=fds(5, 5, 0.0, "e"))
47
cr3 = plot.add_glyph(ds3, Circle(x="x", y="y", radius="s", fill_color="c", line_color="c"))
48
49
tooltips = "<b>@name</b> = (@x{0.00}, @y{0.00})"
50
51
hover = HoverTool(tooltips=tooltips, renderers=[cr1, cr2, ln2, cr3], point_policy="follow_mouse")
52
plot.add_tools(hover)
53
54
crosshair = CrosshairTool()
55
plot.add_tools(crosshair)
56
57
tap = TapTool(renderers=[cr1, cr2, cr3], callback=CustomJS(code="console.log('TAP')"))
58
plot.add_tools(tap)
59
60
wheelzoom = WheelZoomTool()
61
plot.add_tools(wheelzoom)
62
63
legends = lambda: [
64
LegendItem(label="CR1", renderers=[cr1]),
65
LegendItem(label="CR2", renderers=[cr2, ln2]),
66
LegendItem(label="CR3", renderers=[cr3]),
67
]
68
legend = lambda **kwargs: Legend(background_fill_alpha=0.7, items=legends(), click_policy="hide", **kwargs)
69
70
plot.add_layout(legend(location="center_left", orientation="vertical"))
71
plot.add_layout(legend(location="center", orientation="vertical"))
72
plot.add_layout(legend(location="top_center", orientation="horizontal"))
73
plot.add_layout(legend(location="top_right", orientation="horizontal"))
74
plot.add_layout(legend(location="bottom_right", orientation="horizontal"))
75
plot.add_layout(legend(location=(0, 0), orientation="vertical", name="(0, 0)"))
76
plot.add_layout(legend(location="center", orientation="horizontal", name="above"), 'above')
77
plot.add_layout(legend(location="center", orientation="horizontal", name="below"), 'below')
78
plot.add_layout(legend(location="center", orientation="vertical", name="left"), 'left')
79
plot.add_layout(legend(location="center", orientation="vertical", name="right"), 'right')
80
81
doc = Document()
82
doc.add_root(plot)
83
84
if __name__ == "__main__":
85
filename = "synthetic.html"
86
with open(filename, "w") as f:
87
f.write(file_html(doc, INLINE, "A synthetic example"))
88
print("Wrote %s" % filename)
89
view(filename)
90
91