| Download
A tutorial in basic plotting for a college algebra class.
Project: CharmedProject
Views: 1135License: GPL3
from __future__ import print_function12from bokeh.document import Document3from bokeh.embed import file_html4from bokeh.util.browser import view5from bokeh.resources import INLINE6from bokeh.models.glyphs import Circle, Line7from bokeh.models import (ColumnDataSource, Range1d, Plot, LinearAxis, Grid,8HoverTool, CrosshairTool, TapTool, WheelZoomTool, Legend, LegendItem, CustomJS)910plot = Plot(x_range=Range1d(-10, 10), y_range=Range1d(-10, 10), plot_width=600, plot_height=600, toolbar_sticky=False)1112xaxis = LinearAxis()13yaxis = LinearAxis()14xgrid = Grid(dimension=0, ticker=xaxis.ticker)15ygrid = Grid(dimension=1, ticker=yaxis.ticker)1617plot.add_layout(xaxis, "below")18plot.add_layout(yaxis, "left")19plot.add_layout(xgrid)20plot.add_layout(ygrid)2122def fds(x, y, e, n):23d = [24(x+0*e, y+0*e, 1, "red", "%s00" % n),25(x+1*e, y+1*e, 1, "blue", "%s01" % n),26(x+2*e, y+2*e, 1, "green", "%s02" % n),27(x+3*e, y+3*e, 1, "violet", "%s03" % n),28(x+4*e, y+4*e, 1, "pink", "%s04" % n),29(x+5*e, y+5*e, 1, "black", "%s05" % n),30(x+6*e, y+6*e, 1, "gray", "%s06" % n),31(x+7*e, y+7*e, 1, "olive", "%s07" % n),32(x+8*e, y+8*e, 1, "yellow", "%s08" % n),33(x+9*e, y+9*e, 1, "orange", "%s09" % n),34]35f = lambda i: [ t[i] for t in d ]36return dict(x=f(0), y=f(1), s=f(2), c=f(3), name=f(4))3738ds1 = ColumnDataSource(data=fds(0, 0, 0.1, "c"))39cr1 = plot.add_glyph(ds1, Circle(x="x", y="y", radius="s", fill_color="c", line_color="c"))4041ds2 = ColumnDataSource(data=fds(-5, 5, 0.5, "d"))42cr2 = plot.add_glyph(ds2, Circle(x="x", y="y", radius="s", fill_color="c", line_color="c"))43ln2 = plot.add_glyph(ds2, Line(x="x", y="y", line_width=3, line_color="red"))4445ds3 = ColumnDataSource(data=fds(5, 5, 0.0, "e"))46cr3 = plot.add_glyph(ds3, Circle(x="x", y="y", radius="s", fill_color="c", line_color="c"))4748tooltips = "<b>@name</b> = (@x{0.00}, @y{0.00})"4950hover = HoverTool(tooltips=tooltips, renderers=[cr1, cr2, ln2, cr3], point_policy="follow_mouse")51plot.add_tools(hover)5253crosshair = CrosshairTool()54plot.add_tools(crosshair)5556tap = TapTool(renderers=[cr1, cr2, cr3], callback=CustomJS(code="console.log('TAP')"))57plot.add_tools(tap)5859wheelzoom = WheelZoomTool()60plot.add_tools(wheelzoom)6162legends = lambda: [63LegendItem(label="CR1", renderers=[cr1]),64LegendItem(label="CR2", renderers=[cr2, ln2]),65LegendItem(label="CR3", renderers=[cr3]),66]67legend = lambda **kwargs: Legend(background_fill_alpha=0.7, items=legends(), click_policy="hide", **kwargs)6869plot.add_layout(legend(location="center_left", orientation="vertical"))70plot.add_layout(legend(location="center", orientation="vertical"))71plot.add_layout(legend(location="top_center", orientation="horizontal"))72plot.add_layout(legend(location="top_right", orientation="horizontal"))73plot.add_layout(legend(location="bottom_right", orientation="horizontal"))74plot.add_layout(legend(location=(0, 0), orientation="vertical", name="(0, 0)"))75plot.add_layout(legend(location="center", orientation="horizontal", name="above"), 'above')76plot.add_layout(legend(location="center", orientation="horizontal", name="below"), 'below')77plot.add_layout(legend(location="center", orientation="vertical", name="left"), 'left')78plot.add_layout(legend(location="center", orientation="vertical", name="right"), 'right')7980doc = Document()81doc.add_root(plot)8283if __name__ == "__main__":84filename = "synthetic.html"85with open(filename, "w") as f:86f.write(file_html(doc, INLINE, "A synthetic example"))87print("Wrote %s" % filename)88view(filename)899091