def line_advanced(): source = ColumnDataSource(data=dict(x=x,y=y,z=z,widths=widths, heights=heights)) xdr = DataRange1d(sources=[source.columns("x")]) xdr2 = DataRange1d(sources=[source.columns("x")]) ydr = DataRange1d(sources=[source.columns("y")]) ydr2 = DataRange1d(sources=[source.columns("y")]) line_glyph = Line(x="x", y="y", line_color="blue") renderer = GlyphRenderer(data_source = source, xdata_range = xdr, ydata_range = ydr, glyph = line_glyph) pantool = PanTool(dataranges = [xdr, ydr], dimensions=["width","height"]) zoomtool = ZoomTool(dataranges=[xdr,ydr], dimensions=("width","height")) plot = Plot(x_range=xdr, y_range=ydr, data_sources=[source], border=50) plot.tools = [pantool, zoomtool] plot.renderers.append(renderer) #notice that these two have a differen y data range renderer2 = GlyphRenderer(data_source = source, xdata_range = xdr, ydata_range = ydr2, glyph = line_glyph) plot2 = Plot(x_range=xdr, y_range=ydr2, data_sources=[source], border=50) plot2.renderers.append(renderer2) #notice that these two have a differen y data range renderer3 = GlyphRenderer(data_source = source, xdata_range = xdr2, ydata_range = ydr, glyph = line_glyph) plot3 = Plot(x_range=xdr2, y_range=ydr, data_sources=[source], border=50) plot3.renderers.append(renderer3) #this is a dummy plot with no renderers plot4 = Plot(x_range=xdr2, y_range=ydr, data_sources=[source], border=50) sess = session.HTMLFileSession("line_linked_advanced.html") sess.add(plot, renderer, source, xdr, ydr, pantool, zoomtool) sess.add(plot2, renderer2, ydr2, xdr2, renderer3, plot3, plot4) grid = GridPlot(children=[[plot, plot2], [plot3, plot4 ]], name="linked_advanced") sess.add(grid) sess.plotcontext.children.append(grid) sess.save(js="relative", css="relative", rootdir=os.path.abspath(".")) print "Wrote line_linked_advanced.html" webbrowser.open("file://" + os.path.abspath("line_linked_advanced.html"))
def make_plot(name, glyph): glyph_renderer = GlyphRenderer( data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=glyph, ) pantool = PanTool(dataranges=[xdr, ydr], dimensions=["width", "height"]) zoomtool = ZoomTool(dataranges=[xdr, ydr], dimensions=("width", "height")) plot = Plot(x_range=xdr, y_range=ydr, data_sources=[source], border=80) xaxis = LinearAxis(plot=plot, dimension=0) yaxis = LinearAxis(plot=plot, dimension=1) xgrid = Rule(plot=plot, dimension=0) ygrid = Rule(plot=plot, dimension=1) plot.renderers.append(glyph_renderer) plot.tools = [pantool, zoomtool] sess = session.PlotServerSession(username="******", serverloc="http://localhost:5006", userapikey="nokey") sess.add(plot, glyph_renderer, xaxis, yaxis, xgrid, ygrid, source, xdr, ydr, pantool, zoomtool) sess.use_doc(name) sess.store_all()
def make_plot(name, glyph): glyph_renderer = GlyphRenderer( data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=glyph, ) pantool = PanTool(dataranges=[xdr, ydr], dimensions=["width", "height"]) zoomtool = ZoomTool(dataranges=[xdr, ydr], dimensions=("width", "height")) plot = Plot(x_range=xdr, y_range=ydr, data_sources=[source], border=80) xaxis = LinearAxis(plot=plot, dimension=0) yaxis = LinearAxis(plot=plot, dimension=1) xgrid = Grid(plot=plot, dimension=0) ygrid = Grid(plot=plot, dimension=1) plot.renderers.append(glyph_renderer) plot.tools = [pantool, zoomtool] try: sess = session.PlotServerSession(username="******", serverloc="http://localhost:5006", userapikey="nokey") except requests.exceptions.ConnectionError as e: print e print "\nThis example requires the plot server. Please make sure plot server is running, via 'python runserver.py' in the bokeh root directory.\n" sys.exit() sess.add(plot, glyph_renderer, xaxis, yaxis, xgrid, ygrid, source, xdr, ydr, pantool, zoomtool) sess.use_doc(name) sess.plotcontext.children.append(plot) sess.plotcontext._dirty = True sess.store_all()
def make_plot(title, xname, yname): plot = Plot(x_range=xdr, y_range=ydr, data_sources=[lines_source, circles_source], title=title, width=400, height=400, border_fill='white', background_fill='#e9e0db') xaxis = LinearAxis(plot=plot, dimension=0, location="bottom", axis_line_alpha=0) yaxis = LinearAxis(plot=plot, dimension=1, location="left", axis_line_alpha=0) xgrid = Grid(plot=plot, dimension=0) ygrid = Grid(plot=plot, dimension=1) line_renderer = GlyphRenderer( data_source=lines_source, xdata_range=xdr, ydata_range=ydr, glyph=Line(x='x', y='y', line_color="#666699", line_width=2), ) plot.renderers.append(line_renderer) circle_renderer = GlyphRenderer( data_source=circles_source, xdata_range=xdr, ydata_range=ydr, glyph=Circle(x=xname, y=yname, radius=6, fill_color="#cc6633", line_color="#cc6633", fill_alpha=0.5), ) plot.renderers.append(circle_renderer) return plot, (line_renderer, circle_renderer, xaxis, yaxis, xgrid, ygrid)
def large_plot(n): from bokeh.objects import (Plot, PlotContext, LinearAxis, Grid, GlyphRenderer, ColumnDataSource, DataRange1d, PanTool, WheelZoomTool, BoxZoomTool, BoxSelectTool, BoxSelectionOverlay, ResizeTool, PreviewSaveTool, ResetTool) from bokeh.glyphs import Line context = PlotContext() objects = set([context]) for i in xrange(n): source = ColumnDataSource(data=dict(x=[0, i + 1], y=[0, i + 1])) xdr = DataRange1d(sources=[source.columns("x")]) ydr = DataRange1d(sources=[source.columns("y")]) plot = Plot(x_range=xdr, y_range=ydr) xaxis = LinearAxis(plot=plot) yaxis = LinearAxis(plot=plot) xgrid = Grid(plot=plot, dimension=0) ygrid = Grid(plot=plot, dimension=1) tickers = [ xaxis.ticker, xaxis.formatter, yaxis.ticker, yaxis.formatter ] glyph = Line(x='x', y='y') renderer = GlyphRenderer(data_source=source, glyph=glyph) plot.renderers.append(renderer) pan = PanTool(plot=plot) wheel_zoom = WheelZoomTool(plot=plot) box_zoom = BoxZoomTool(plot=plot) box_select = BoxSelectTool(plot=plot) box_selection = BoxSelectionOverlay(tool=box_select) resize = ResizeTool(plot=plot) previewsave = PreviewSaveTool(plot=plot) reset = ResetTool(plot=plot) tools = [ pan, wheel_zoom, box_zoom, box_select, box_selection, resize, previewsave, reset ] plot.tools.append(tools) context.children.append(plot) objects |= set([ source, xdr, ydr, plot, xaxis, yaxis, xgrid, ygrid, renderer, glyph ] + tickers + tools) return context, objects
def make_plot(source, xname, yname, linecolor, xdr=None, ydr=None): """ Returns a tuple (plot, [obj1...objN]); the former can be added to a GridPlot, and the latter is added to the plotcontext. """ if xdr is None: xdr = DataRange1d(sources=[source.columns(xname)]) if ydr is None: ydr = DataRange1d(sources=[source.columns(yname)]) plot = Plot(x_range=xdr, y_range=ydr, data_sources=[source], border=50) xaxis = LinearAxis(plot=plot, dimension=0, location="bottom") yaxis = LinearAxis(plot=plot, dimension=1, location="left") pantool = PanTool(dataranges=[xdr, ydr], dimensions=["width", "height"]) zoomtool = ZoomTool(dataranges=[xdr, ydr], dimensions=("width", "height")) renderer = GlyphRenderer( data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=Line(x=xname, y=yname, linecolor=linecolor), ) plot.renderers.append(renderer) plot.tools = [pantool, zoomtool] return plot, (renderer, xaxis, yaxis, source, xdr, ydr, pantool, zoomtool)
def make_plot(xname, yname, xax=False, yax=False, text=None): plot = Plot(x_range=xdr, y_range=ydr, data_sources=[source], background_fill="#ffeedd", width=250, height=250, border_fill='white', title="", border_symmetry="", min_border=2) objs = [] if xax: xaxis = LinearAxis(plot=plot, dimension=0, location="bottom") objs.append(xaxis) if yax: yaxis = LinearAxis(plot=plot, dimension=1, location="left") objs.append(yaxis) xgrid = Grid(plot=plot, dimension=0) ygrid = Grid(plot=plot, dimension=1) circle = Circle(x=xname, y=yname, fill_color="color", fill_alpha=0.2, radius=2, line_color="color") circle_renderer = GlyphRenderer( data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=circle, ) plot.renderers.append(circle_renderer) plot.tools = [pan, zoom] if text: text = " ".join(text.split('_')) text = Text(x={ 'field': 'center', 'units': 'screen' }, y={ 'field': 'center', 'units': 'screen' }, text=text, angle=pi / 4, text_font_style="bold", text_baseline="top", text_color="#ffaaaa", text_alpha=0.5, text_align="center", text_font_size="28pt") text_renderer = GlyphRenderer( data_source=text_source, xdata_range=xdr, ydata_range=ydr, glyph=text, ) plot.data_sources.append(text_source) plot.renderers.append(text_renderer) objs.append(text_renderer) objs.append(text_source) return plot, objs + [circle_renderer, xgrid, ygrid]
def generate_embed_test(): """this generates a new plot and uses the script inject to put it into a page running this repeatedly will fill up your redis DB quickly, but it allows quick iteration """ from numpy import pi, arange, sin, cos import numpy as np from bokeh.objects import ( Plot, DataRange1d, LinearAxis, Rule, ColumnDataSource, GlyphRenderer, PanTool, ZoomTool, PreviewSaveTool) from bokeh.glyphs import Circle from bokeh import session x = arange(-2*pi, 2*pi, 0.1) y = sin(x) z = cos(x) widths = np.ones_like(x) * 0.02 heights = np.ones_like(x) * 0.2 source = ColumnDataSource(data=dict(x=x,y=y,z=z,widths=widths, heights=heights)) xdr = DataRange1d(sources=[source.columns("x")]) ydr = DataRange1d(sources=[source.columns("y")]) circle = Circle(x="x", y="y", fill="red", radius=5, line_color="black") glyph_renderer = GlyphRenderer( data_source = source, xdata_range = xdr, ydata_range = ydr, glyph = circle) pantool = PanTool(dataranges = [xdr, ydr], dimensions=["width","height"]) #zoomtool = ZoomTool(dataranges=[xdr,ydr], dimensions=("width","height")) previewtool = PreviewSaveTool(dataranges=[xdr,ydr], dimensions=("width","height")) plot = Plot(x_range=xdr, y_range=ydr, data_sources=[source], border= 80) xaxis = LinearAxis(plot=plot, dimension=0) yaxis = LinearAxis(plot=plot, dimension=1) xgrid = Rule(plot=plot, dimension=0) ygrid = Rule(plot=plot, dimension=1) plot.renderers.append(glyph_renderer) plot.tools = [pantool, previewtool] sess = session.PlotServerSession( username="******", serverloc="http://localhost:5006", userapikey="nokey") sess.use_doc("glyph2") sess.add(plot, glyph_renderer, xaxis, yaxis, xgrid, ygrid, source, xdr, ydr, pantool, previewtool) sess.plotcontext.children.append(plot) sess.plotcontext._dirty = True # not so nice.. but set the model doens't know # that we appended to children sess.store_all() if app.debug: slug = hemlib.slug_json() static_js = hemlib.slug_libs(app, slug['libs']) hemsource = os.path.join(app.static_folder, "coffee") hem_js = hemlib.coffee_assets(hemsource, "localhost", 9294) hemsource = os.path.join(app.static_folder, "vendor", "bokehjs", "coffee") hem_js += hemlib.coffee_assets(hemsource, "localhost", 9294) else: static_js = ['/bokeh/static/js/application.js'] hem_js = [] return render_template("generate_embed_test.html", jsfiles=static_js, hemfiles=hem_js, plot_scr=plot.script_inject())
# data = [ # {'x' : 1, 'y' : 5, 'z':3}, # {'x' : 2, 'y' : 4, 'z':3, 'radius':10}, # {'x' : 3, 'y' : 3, 'z':3, 'fill':"blue"}, # {'x' : 4, 'y' : 2, 'z':3}, # {'x' : 5, 'y' : 1, 'z':3}, # ]) xdr = DataRange1d(sources=[source.columns("x")]) ydr = DataRange1d(sources=[source.columns("y")]) circle = Circle(x="x", y="y", fill_color="red", radius="z", line_color="black") glyph_renderer = GlyphRenderer( data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=circle, ) pantool = PanTool(dataranges=[xdr, ydr], dimensions=["width", "height"]) zoomtool = ZoomTool(dataranges=[xdr, ydr], dimensions=("width", "height")) plot = Plot(x_range=xdr, y_range=ydr, data_sources=[source], border=80) xaxis = LinearAxis(plot=plot, dimension=0, location="min") yaxis = LinearAxis(plot=plot, dimension=1, location="min") xgrid = Grid(plot=plot, dimension=0) ygrid = Grid(plot=plot, dimension=1) plot.renderers.append(glyph_renderer) plot.tools = [pantool, zoomtool]
x = np.linspace(-2 * pi, 2 * pi, 1000) y = sin(x) z = cos(x) widths = np.ones_like(x) * 0.02 heights = np.ones_like(x) * 0.2 source = ColumnDataSource( data=dict(x=x, y=y, z=z, widths=widths, heights=heights)) xdr = DataRange1d(sources=[source.columns("x")]) ydr = DataRange1d(sources=[source.columns("y")]) line_glyph = Line(x="x", y="y", line_color="blue") renderer = GlyphRenderer(data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=line_glyph) plot = Plot(x_range=xdr, y_range=ydr, data_sources=[source], border=50) plot.renderers.append(renderer) renderer2 = GlyphRenderer(data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=line_glyph) plot2 = Plot(x_range=xdr, y_range=ydr, data_sources=[source], border=50) pantool2 = PanTool(dataranges=[xdr, ydr], dimensions=["width", "height"]) zoomtool2 = ZoomTool(dataranges=[xdr, ydr], dimensions=("width", "height"))
def make_plot(): from numpy import pi, arange, sin, cos import numpy as np from bokeh.objects import (Plot, DataRange1d, LinearAxis, ColumnDataSource, GlyphRenderer, PanTool, PreviewSaveTool) from bokeh.glyphs import Circle from bokeh import session x = arange(-2 * pi, 2 * pi, 0.1) y = sin(x) z = cos(x) widths = np.ones_like(x) * 0.02 heights = np.ones_like(x) * 0.2 source = ColumnDataSource( data=dict(x=x, y=y, z=z, widths=widths, heights=heights)) xdr = DataRange1d(sources=[source.columns("x")]) ydr = DataRange1d(sources=[source.columns("y")]) circle = Circle(x="x", y="y", fill="red", radius=5, line_color="black") glyph_renderer = GlyphRenderer(data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=circle) pantool = PanTool(dataranges=[xdr, ydr], dimensions=["width", "height"]) previewtool = PreviewSaveTool(dataranges=[xdr, ydr], dimensions=("width", "height")) plot = Plot(x_range=xdr, y_range=ydr, data_sources=[source], border=80) xaxis = LinearAxis(plot=plot, dimension=0) yaxis = LinearAxis(plot=plot, dimension=1) plot.renderers.append(glyph_renderer) plot.tools = [pantool, previewtool] sess = session.PlotServerSession(username="******", serverloc="http://localhost:5006", userapikey="nokey") sess.use_doc("glyph2") sess.add( plot, glyph_renderer, xaxis, yaxis, # xgrid, ygrid, source, xdr, ydr, pantool, previewtool) sess.plotcontext.children.append(plot) sess.plotcontext._dirty = True # not so nice.. but set the model doens't know # that we appended to children sess.store_all() return plot
'long': -97.7390, 'z': 15, 'fill': 'blue' }, { 'lat': 30.2869, 'long': -97.7405, 'z': 15, 'fill': 'green' }, ]) circle_renderer = GlyphRenderer(data_source=source, xdata_range=x_range, ydata_range=y_range, glyph=Circle(x="long", y="lat", fill_color="fill", radius=6, radius_units="screen", line_color="black")) plot.data_sources.append(source) plot.renderers.append(circle_renderer) import requests try: sess = session.PlotServerSession(username="******", serverloc="http://localhost:5006", userapikey="nokey") except requests.exceptions.ConnectionError as e: print e print "\nThis example requires the plot server. Please make sure plot server is running, via 'bokeh-server' in the bokeh root directory.\n"
def create(self): manufacturers = sorted(mpg["manufacturer"].unique()) models = sorted(mpg["model"].unique()) transmissions = sorted(mpg["trans"].unique()) drives = sorted(mpg["drv"].unique()) classes = sorted(mpg["class"].unique()) manufacturer_select = Select(title="Manufacturer:", value="All", options=["All"] + manufacturers) manufacturer_select.on_change('value', self.on_manufacturer_change) model_select = Select(title="Model:", value="All", options=["All"] + models) model_select.on_change('value', self.on_model_change) transmission_select = Select(title="Transmission:", value="All", options=["All"] + transmissions) transmission_select.on_change('value', self.on_transmission_change) drive_select = Select(title="Drive:", value="All", options=["All"] + drives) drive_select.on_change('value', self.on_drive_change) class_select = Select(title="Class:", value="All", options=["All"] + classes) class_select.on_change('value', self.on_class_change) columns = [ TableColumn(field="manufacturer", header="Manufacturer", type="autocomplete", source=manufacturers), TableColumn(field="model", header="Model", type="autocomplete", source=models), TableColumn(field="displ", header="Displacement", type="numeric", format="0.00"), TableColumn(field="year", header="Year", type="numeric"), TableColumn(field="cyl", header="Cylinders", type="numeric"), TableColumn(field="trans", header="Transmission", type="dropdown", strict=True, source=transmissions), TableColumn(field="drv", header="Drive", type="autocomplete", strict=True, source=drives), TableColumn(field="class", header="Class", type="autocomplete", strict=True, source=classes), TableColumn(field="cty", header="City MPG", type="numeric"), TableColumn(field="hwy", header="Highway MPG", type="numeric"), ] handson_table = HandsonTable(source=self.source, columns=columns, sorting=True) xdr = DataRange1d(sources=[self.source.columns("index")]) #xdr = FactorRange(factors=manufacturers) ydr = DataRange1d( sources=[self.source.columns("cty"), self.source.columns("hwy")]) plot = Plot(title=None, data_sources=[self.source], x_range=xdr, y_range=ydr, plot_width=800, plot_height=300) xaxis = LinearAxis(plot=plot) plot.below.append(xaxis) yaxis = LinearAxis(plot=plot) ygrid = Grid(plot=plot, dimension=1, ticker=yaxis.ticker) plot.left.append(yaxis) cty = GlyphRenderer(data_source=self.source, glyph=Circle(x="index", y="cty", fill_color="#396285", size=8, fill_alpha=0.5, line_alpha=0.5)) hwy = GlyphRenderer(data_source=self.source, glyph=Circle(x="index", y="hwy", fill_color="#CE603D", size=8, fill_alpha=0.5, line_alpha=0.5)) select_tool = BoxSelectTool(plot=plot, renderers=[cty, hwy], dimensions=['width']) plot.tools.append(select_tool) plot.renderers.extend([cty, hwy, ygrid]) controls = VBox(children=[ manufacturer_select, model_select, transmission_select, drive_select, class_select ], width=200) top_panel = HBox(children=[controls, plot]) layout = VBox(children=[top_panel, handson_table]) return layout