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 = Glyph( 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(name, glyph): glyph_renderer = Glyph( data_source = source, xdata_range = xdr, ydata_range = ydr, glyph = glyph, ) pantool = PanTool(dimensions=["width", "height"]) wheelzoomtool = WheelZoomTool(dimensions=["width", "height"]) plot = Plot(x_range=xdr, y_range=ydr, data_sources=[source], min_border=80) xaxis = LinearAxis(plot=plot, dimension=0) yaxis = LinearAxis(plot=plot, dimension=1) xgrid = Grid(plot=plot, dimension=0, axis=xaxis) ygrid = Grid(plot=plot, dimension=1, axis=yaxis) plot.renderers.append(glyph_renderer) plot.tools = [pantool, wheelzoomtool] try: sess = session.PlotServerSession( serverloc="http://localhost:5006", username="******", userapikey="nokey") except requests.exceptions.ConnectionError: print("ERROR: This example requires the plot server. Please make sure plot server is running, by executing 'bokeh-server'") sys.exit(1) sess.use_doc(name) sess.add_plot(plot) sess.store_all()
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=4, y=4, text=text, angle=pi/4, text_font_style="bold", text_baseline="top", text_color="#ffaaaa", text_alpha=0.2, text_align="center", text_font_size="28pt") text_renderer = GlyphRenderer( data_source=source, xdata_range = xdr, ydata_range = ydr, glyph = text, ) plot.renderers.append(text_renderer) objs.append(text_renderer) return plot, objs + [circle_renderer, xgrid, ygrid]
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(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) if xax: xaxis = LinearAxis(plot=plot, dimension=0, location="bottom") if yax: yaxis = LinearAxis(plot=plot, dimension=1, location="left") 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, size=4, line_color="color") circle_renderer = Glyph( 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':'xcenter', 'units':'screen'}, y={'field':'ycenter', '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 = Glyph( data_source=text_source, xdata_range = xdr, ydata_range = ydr, glyph = text, ) plot.data_sources.append(text_source) plot.renderers.append(text_renderer) return plot
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(xname, yname, xax=False, yax=False, text=None): plot = Plot( x_range=xdr, y_range=ydr, data_sources=[source], background_fill="#efe8e2", width=250, height=250, border_fill='white', title="", min_border=2, border_symmetry=None) if xax: xaxis = LinearAxis(plot=plot, dimension=0, location="bottom") xgrid = Grid(plot=plot, dimension=0, axis=xaxis) if yax: yaxis = LinearAxis(plot=plot, dimension=1, location="left") ygrid = Grid(plot=plot, dimension=1, axis=yaxis) circle = Circle(x=xname, y=yname, fill_color="color", fill_alpha=0.2, size=4, line_color="color") circle_renderer = Glyph( 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':'xcenter', 'units':'screen'}, y={'field':'ycenter', 'units':'screen'}, text=[text], angle=pi/4, text_font_style="bold", text_baseline="top", text_color="#ffaaaa", text_alpha=0.7, text_align="center", text_font_size="28pt") text_renderer = Glyph( data_source=text_source, xdata_range = xdr, ydata_range = ydr, glyph = text, ) plot.data_sources.append(text_source) plot.renderers.append(text_renderer) return plot
def make_plot(name, glyph): glyph_renderer = Glyph( data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=glyph, ) pantool = PanTool(dimensions=["width", "height"]) wheelzoomtool = WheelZoomTool(dimensions=["width", "height"]) plot = Plot(x_range=xdr, y_range=ydr, data_sources=[source], min_border=80) xaxis = LinearAxis(plot=plot, dimension=0) yaxis = LinearAxis(plot=plot, dimension=1) xgrid = Grid(plot=plot, dimension=0, axis=xaxis) ygrid = Grid(plot=plot, dimension=1, axis=yaxis) plot.renderers.append(glyph_renderer) plot.tools = [pantool, wheelzoomtool] try: sess = session.PlotServerSession(serverloc="http://localhost:5006", username="******", userapikey="nokey") except requests.exceptions.ConnectionError: print( "ERROR: This example requires the plot server. Please make sure plot server is running, by executing 'bokeh-server'" ) sys.exit(1) sess.use_doc(name) sess.add_plot(plot) sess.store_all()
def job_loc_plot_builder(): xdr = FactorRange(factors=countries) ydr = DataRange1d(sources=[source_country.columns("data_range")]) plot = Plot(title="Postings by Job Location (Country)", data_sources=[source_country], x_range=xdr, y_range=ydr, plot_width=760, plot_height=500) xaxis = CategoricalAxis(plot=plot, dimension=0, major_label_orientation=pi / 4.0) yaxis = LinearAxis(plot=plot, dimension=1) yaxis.major_tick_in = 0 ygrid = Grid(plot=plot, dimension=1, axis=yaxis) quad = Rect(x="country", y="count_half", height="count", width=0.9, fill_color="#483D8B") bars = Glyph(data_source=source_country, xdata_range=xdr, ydata_range=ydr, glyph=quad) plot.renderers.append(bars) plot.background_fill = '#333333' return plot
def weekday_builder(): dow = [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ] xdr = FactorRange(factors=dow) ydr = DataRange1d(sources=[source_dow.columns("data_range")]) plot = Plot(title="Weekday of Job Posting", data_sources=[source_dow], x_range=xdr, y_range=ydr, plot_width=760, plot_height=500) xaxis = CategoricalAxis(plot=plot, dimension=0, major_label_orientation=pi / 4.0) yaxis = LinearAxis(plot=plot, dimension=1) yaxis.major_tick_in = 0 ygrid = Grid(plot=plot, dimension=1, axis=yaxis) quad = Rect(x="weekday", y="weekday_half", height="count", width=0.9, fill_color="#D9301A") bars = Glyph(data_source=source_dow, xdata_range=xdr, ydata_range=ydr, glyph=quad) plot.renderers.append(bars) plot.background_fill = '#686975' return plot
def jobtype_builder(): jtypes = ["Half Time", "Full Time", "Hourly", "Temporary"] xdr = FactorRange(factors=jtypes) ydr = DataRange1d(sources=[source_jobtype.columns("data_range")]) plot = Plot(title="Job Type", data_sources=[source_jobtype], x_range=xdr, y_range=ydr, plot_width=760, plot_height=500) xaxis = CategoricalAxis(plot=plot, dimension=0, major_label_orientation=pi / 4.0) yaxis = LinearAxis(plot=plot, dimension=1) yaxis.major_tick_in = 0 ygrid = Grid(plot=plot, dimension=1, axis=yaxis) quad = Rect(x="jobtypes", y="jobtype_half", height="count", width=0.9, fill_color="#33A6A4") bars = Glyph(data_source=source_jobtype, xdata_range=xdr, ydata_range=ydr, glyph=quad) plot.renderers.append(bars) plot.background_fill = '#686975' return plot
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(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(): 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
def classical_gear(module, large_teeth, small_teeth): xdr = Range1d(start=-300, end=150) ydr = Range1d(start=-100, end=100) source = ColumnDataSource(data=dict(dummy=[0])) plot = Plot( title=None, x_range=xdr, y_range=ydr, plot_width=800, plot_height=800 ) plot.add_tools(PanTool(), WheelZoomTool(), ResetTool()) radius = pitch_radius(module, large_teeth) angle = 0 glyph = Gear( x=-radius, y=0, module=module, teeth=large_teeth, angle=angle, fill_color=fill_color[0], line_color=line_color ) plot.add_glyph(source, glyph) radius = pitch_radius(module, small_teeth) angle = half_tooth(small_teeth) glyph = Gear( x=radius, y=0, module=module, teeth=small_teeth, angle=angle, fill_color=fill_color[1], line_color=line_color ) plot.add_glyph(source, glyph) return plot
def population(): xdr = FactorRange(factors=years) ydr = DataRange1d( sources=[source_known.columns("y"), source_predicted.columns("y")]) plot = Plot(title=None, x_range=xdr, y_range=ydr, plot_width=800, plot_height=200) plot.add_layout(CategoricalAxis(major_label_orientation=pi / 4), 'below') line_known = Line(x="x", y="y", line_color="violet", line_width=2) line_known_glyph = plot.add_glyph(source_known, line_known) line_predicted = Line(x="x", y="y", line_color="violet", line_width=2, line_dash="dashed") line_predicted_glyph = plot.add_glyph(source_predicted, line_predicted) plot.add_layout( Legend(orientation="bottom_right", legends=dict(known=[line_known_glyph], predicted=[line_predicted_glyph]))) return plot
def pyramid_plot(self): from bokeh.objects import (Plot, DataRange1d, LinearAxis, Grid, Legend, SingleIntervalTicker) from bokeh.glyphs import Quad xdr = DataRange1d(sources=[self.source_pyramid.columns("male"), self.source_pyramid.columns("female")]) ydr = DataRange1d(sources=[self.source_pyramid.columns("groups")]) self.plot = Plot(title="Widgets", x_range=xdr, y_range=ydr, plot_width=600, plot_height=600) xaxis = LinearAxis() self.plot.add_layout(xaxis, 'below') yaxis = LinearAxis(ticker=SingleIntervalTicker(interval=5)) self.plot.add_layout(yaxis, 'left') self.plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker)) self.plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker)) male_quad = Quad(left="male", right=0, bottom="groups", top="shifted", fill_color="#3B8686") male_quad_glyph = self.plot.add_glyph(self.source_pyramid, male_quad) female_quad = Quad(left=0, right="female", bottom="groups", top="shifted", fill_color="#CFF09E") female_quad_glyph = self.plot.add_glyph(self.source_pyramid, female_quad) self.plot.add_layout(Legend(legends=dict(Male=[male_quad_glyph], Female=[female_quad_glyph])))
def make_downloads_plot(self, source): xdr = DataRange1d(sources=[source.columns("dates")]) ydr = DataRange1d(sources=[source.columns("downloads")]) title = "%s downloads" % self.modelform.installer plot = Plot(title=title, data_sources=[source], x_range=xdr, y_range=ydr, width=600, height=400) line = Line(x="dates", y="downloads", line_color="blue") line_glyph = Glyph(data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=line) plot.renderers.append(line_glyph) circle = Circle(x="dates", y="downloads", fill_color="red") circle_glyph = Glyph(data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=circle) plot.renderers.append(circle_glyph) hover = HoverTool(plot=plot, tooltips=dict(downloads="@downloads")) plot.tools.append(hover) xformatter = DatetimeTickFormatter(formats=dict(months=["%b %Y"])) yformatter = BasicTickFormatter(precision=None, use_scientific=False) xaxis = DatetimeAxis(plot=plot, dimension=0, formatter=xformatter) yaxis = LinearAxis(plot=plot, dimension=1, formatter=yformatter) xgrid = Grid(plot=plot, dimension=0, axis=xaxis) ygrid = Grid(plot=plot, dimension=1, axis=yaxis) return plot
def make_plot(): source = ColumnDataSource( dict( dates=[date(2014, 3, i) for i in [1, 2, 3, 4, 5]], downloads=[100, 27, 54, 64, 75], )) xdr = DataRange1d(sources=[source.columns("dates")]) ydr = DataRange1d(sources=[source.columns("downloads")]) plot = Plot(title="Product downloads", data_sources=[source], x_range=xdr, y_range=ydr, width=400, height=400) line = Line(x="dates", y="downloads", line_color="blue") line_glyph = Glyph(data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=line) plot.renderers.append(line_glyph) circle = Circle(x="dates", y="downloads", fill_color="red") circle_glyph = Glyph(data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=circle) plot.renderers.append(circle_glyph) hover = HoverTool(plot=plot, tooltips=dict(downloads="@downloads")) plot.tools.append(hover) xaxis = DatetimeAxis(plot=plot, dimension=0) yaxis = LinearAxis(plot=plot, dimension=1) xgrid = Grid(plot=plot, dimension=0, axis=xaxis) ygrid = Grid(plot=plot, dimension=1, axis=yaxis) return plot, source
def sample_gear(): xdr = Range1d(start=-30, end=30) ydr = Range1d(start=-30, end=30) source = ColumnDataSource(data=dict(dummy=[0])) plot = Plot(title=None, data_sources=[source], x_range=xdr, y_range=ydr, width=800, height=800) plot.tools.extend( [PanTool(plot=plot), WheelZoomTool(plot=plot), ResetTool(plot=plot)]) glyph = Gear(x=0, y=0, module=5, teeth=8, angle=0, shaft_size=0.2, fill_color=fill_color[2], line_color=line_color) renderer = Glyph(data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=glyph) plot.renderers.append(renderer) return plot
def make_punchcard_plot(self, source): xdr = FactorRange(factors=source.data["hours"][::7]) ydr = FactorRange(factors=source.data["days"][:7]) title = "%s punchcard" % self.modelform.installer plot = Plot(title=title, data_sources=[source], x_range=xdr, y_range=ydr, width=600, height=400) rect = Rect(x="hours", y="days", width=1, height=1, fill_color="red", fill_alpha="percentages") rect_glyph = Glyph(data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=rect) plot.renderers.append(rect_glyph) hover = HoverTool(plot=plot, tooltips=dict(downloads="@counts")) plot.tools.append(hover) xaxis = CategoricalAxis(plot=plot, dimension=0) yaxis = CategoricalAxis(plot=plot, dimension=1) return plot
def make_tab(title, glyph): plot = Plot(title=title, x_range=xdr, y_range=ydr) plot.add_glyph(source, glyph) xaxis = LinearAxis() plot.add_layout(xaxis, 'below') yaxis = LinearAxis() plot.add_layout(yaxis, 'left') plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker)) plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker)) tab = Panel(child=plot, title=title) return tab
def jobtype_builder(): jtypes = ["Half Time", "Full Time", "Hourly", "Temporary"] xdr = FactorRange(factors=jtypes) ydr = DataRange1d(sources=[source_jobtype.columns("data_range")]) plot = Plot(title="Job Type", data_sources=[source_jobtype], x_range=xdr, y_range=ydr, plot_width=760, plot_height=500) xaxis = CategoricalAxis(plot=plot, dimension=0, major_label_orientation=pi/4.0) yaxis = LinearAxis(plot=plot, dimension=1) yaxis.major_tick_in = 0 ygrid = Grid(plot=plot, dimension=1, axis=yaxis) quad = Rect(x="jobtypes", y="jobtype_half", height="count", width=0.9, fill_color="#33A6A4") bars = Glyph(data_source=source_jobtype, xdata_range=xdr, ydata_range=ydr, glyph=quad) plot.renderers.append(bars) plot.background_fill = '#686975' return plot
def epicyclic_gear(module, sun_teeth, planet_teeth): xdr = Range1d(start=-150, end=150) ydr = Range1d(start=-150, end=150) source = ColumnDataSource(data=dict(dummy=[0])) plot = Plot( title=None, x_range=xdr, y_range=ydr, plot_width=800, plot_height=800 ) plot.add_tools(PanTool(), WheelZoomTool(), ResetTool()) annulus_teeth = sun_teeth + 2*planet_teeth glyph = Gear( x=0, y=0, module=module, teeth=annulus_teeth, angle=0, fill_color=fill_color[0], line_color=line_color, internal=True ) plot.add_glyph(source, glyph) glyph = Gear( x=0, y=0, module=module, teeth=sun_teeth, angle=0, fill_color=fill_color[2], line_color=line_color ) plot.add_glyph(source, glyph) sun_radius = pitch_radius(module, sun_teeth) planet_radius = pitch_radius(module, planet_teeth) radius = sun_radius + planet_radius angle = half_tooth(planet_teeth) for i, j in [(+1, 0), (0, +1), (-1, 0), (0, -1)]: glyph = Gear( x=radius*i, y=radius*j, module=module, teeth=planet_teeth, angle=angle, fill_color=fill_color[1], line_color=line_color ) plot.add_glyph(source, glyph) return plot
def weekday_builder(): dow = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] xdr = FactorRange(factors=dow) ydr = DataRange1d(sources=[source_dow.columns("data_range")]) plot = Plot(title="Weekday of Job Posting", data_sources=[source_dow], x_range=xdr, y_range=ydr, plot_width=760, plot_height=500) xaxis = CategoricalAxis(plot=plot, dimension=0, major_label_orientation=pi/4.0) yaxis = LinearAxis(plot=plot, dimension=1) yaxis.major_tick_in = 0 ygrid = Grid(plot=plot, dimension=1, axis=yaxis) quad = Rect(x="weekday", y="weekday_half", height="count", width=0.9, fill_color="#D9301A") bars = Glyph(data_source=source_dow, xdata_range=xdr, ydata_range=ydr, glyph=quad) plot.renderers.append(bars) plot.background_fill = '#686975' return plot
def pyramid_plot(self): from bokeh.objects import (Plot, DataRange1d, LinearAxis, Grid, Legend, SingleIntervalTicker) from bokeh.glyphs import Quad xdr = DataRange1d(sources=[ self.source_pyramid.columns("male"), self.source_pyramid.columns("female") ]) ydr = DataRange1d(sources=[self.source_pyramid.columns("groups")]) self.plot = Plot(title=None, x_range=xdr, y_range=ydr, plot_width=600, plot_height=600) xaxis = LinearAxis() self.plot.add_layout(xaxis, 'below') yaxis = LinearAxis(ticker=SingleIntervalTicker(interval=5)) self.plot.add_layout(yaxis, 'left') self.plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker)) self.plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker)) male_quad = Quad(left="male", right=0, bottom="groups", top="shifted", fill_color="#3B8686") male_quad_glyph = self.plot.add_glyph(self.source_pyramid, male_quad) female_quad = Quad(left=0, right="female", bottom="groups", top="shifted", fill_color="#CFF09E") female_quad_glyph = self.plot.add_glyph(self.source_pyramid, female_quad) self.plot.add_layout( Legend(legends=dict(Male=[male_quad_glyph], Female=[female_quad_glyph])))
def make_plot(source, xname, yname, line_color, 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, min_border=50) plot.add_layout(LinearAxis(), 'below') plot.add_layout(LinearAxis(), 'left') plot.add_glyph(source, Line(x=xname, y=yname, line_color=line_color)) plot.add_tools(PanTool(), WheelZoomTool()) return plot
def make_plot(name, glyph): glyph_renderer = Glyph( data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=glyph, ) pantool = PanTool(dimensions=["width", "height"]) wheelzoomtool = WheelZoomTool(dimensions=["width", "height"]) plot = Plot(x_range=xdr, y_range=ydr, data_sources=[source], min_border=80) xaxis = LinearAxis(plot=plot, dimension=0) yaxis = LinearAxis(plot=plot, dimension=1) xgrid = Grid(plot=plot, dimension=0, axis=xaxis) ygrid = Grid(plot=plot, dimension=1, axis=yaxis) plot.renderers.append(glyph_renderer) plot.tools = [pantool, wheelzoomtool] document.add(plot) session.store_document(document)
def make_plot(name, glyph): glyph_renderer = Glyph( data_source = source, xdata_range = xdr, ydata_range = ydr, glyph = glyph, ) pantool = PanTool(dimensions=["width", "height"]) wheelzoomtool = WheelZoomTool(dimensions=["width", "height"]) plot = Plot(x_range=xdr, y_range=ydr, data_sources=[source], min_border=80) xaxis = LinearAxis(plot=plot, dimension=0) yaxis = LinearAxis(plot=plot, dimension=1) xgrid = Grid(plot=plot, dimension=0, axis=xaxis) ygrid = Grid(plot=plot, dimension=1, axis=yaxis) plot.renderers.append(glyph_renderer) plot.tools = [pantool, wheelzoomtool] document.add(plot) session.store_document(document)
def make_tab(title, glyph): plot = Plot(title=title, data_sources=[source], x_range=xdr, y_range=ydr) renderer = Glyph(data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=glyph) plot.renderers.append(renderer) xaxis = LinearAxis(plot=plot, dimension=0) yaxis = LinearAxis(plot=plot, dimension=1) xgrid = Grid(plot=plot, dimension=0, axis=xaxis) ygrid = Grid(plot=plot, dimension=1, axis=yaxis) tab = Panel(child=plot, title=title) return tab
def make_plot(source, xname, yname, line_color, 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], min_border=50) xaxis = LinearAxis(plot=plot, dimension=0, location="bottom") yaxis = LinearAxis(plot=plot, dimension=1, location="left") pantool = PanTool(dimensions=["width", "height"]) wheelzoomtool = WheelZoomTool(dimensions=["width", "height"]) renderer = Glyph( data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=Line(x=xname, y=yname, line_color=line_color), ) plot.renderers.append(renderer) plot.tools = [pantool, wheelzoomtool] return plot
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_box_violin_plot(data, maxwidth=0.9): """ data: dict[Str -> List[Number]] maxwidth: float Maximum width of tornado plot within each factor/facet Returns the plot object """ print("Plotting box violin graph") plot_width = 500 plot_height = 350 df = pd.DataFrame(columns=["group", "width", "height", "texts", "cats"]) bar_height = 50 bins = [0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e10] # Compute histograms, while keeping track of max Y values and max counts for i, (group, vals) in enumerate(data.iteritems()): hist, edges = np.histogram(vals, bins) df = df.append(pd.DataFrame(dict( group = group, width = np.log2(hist[1:]), height = np.ones(len(hist) - 1), texts = ["%d Nodes" % i for i in hist[1:-1]] + ["%d" % hist[-1]], cats = [">10^%d" % np.log10(bin) for bin in bins[1:-1]], ))) df.replace(-np.inf, 0) # Normalize the widths df["width"] *= (maxwidth / df["width"].max()) ds = ColumnDataSource(df) xdr = FactorRange(factors=sorted(df["group"].unique())) ydr = FactorRange(factors=list(df["cats"])) plot = Plot(data_sources=[ds], x_range=xdr, y_range=ydr, title="Degree Distribution (log scale)", plot_width=plot_width, plot_height=plot_height, tools=[]) yaxis = CategoricalAxis(plot=plot, location="left", axis_label="degree") plot.left.append(yaxis) glyph = Rect(x="group", y="cats", width="width", height="height", fill_color="#3366ff") text_glyph = Text(x="group", y="cats", text="texts", text_baseline="middle", text_align="center", angle=0) plot.renderers.append(Glyph(data_source=ds, xdata_range=xdr, ydata_range=ydr, glyph=glyph)) plot.renderers.append(Glyph(data_source=ds, xdata_range=xdr, ydata_range=ydr, glyph=text_glyph)) return plot
def job_loc_plot_builder(): xdr = FactorRange(factors=countries) ydr = DataRange1d(sources=[source_country.columns("data_range")]) plot = Plot(title="Postings by Job Location (Country)", data_sources=[source_country], x_range=xdr, y_range=ydr, plot_width=760, plot_height=500) xaxis = CategoricalAxis(plot=plot, dimension=0, major_label_orientation=pi/4.0) yaxis = LinearAxis(plot=plot, dimension=1) yaxis.major_tick_in = 0 ygrid = Grid(plot=plot, dimension=1, axis=yaxis) quad = Rect(x="country", y="count_half", height="count", width=0.9, fill_color="#483D8B") bars = Glyph(data_source=source_country, xdata_range=xdr, ydata_range=ydr, glyph=quad) plot.renderers.append(bars) plot.background_fill = '#333333' return plot
def make_plot(source, xname, yname, line_color, 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, min_border=50) xaxis = LinearAxis(plot=plot) plot.below.append(xaxis) yaxis = LinearAxis(plot=plot) plot.left.append(yaxis) pantool = PanTool(dimensions=["width", "height"]) wheelzoomtool = WheelZoomTool(dimensions=["width", "height"]) renderer = Glyph( data_source = source, xdata_range = xdr, ydata_range = ydr, glyph = Line(x=xname, y=yname, line_color=line_color), ) plot.renderers.append(renderer) plot.tools = [pantool, wheelzoomtool] return plot
def classical_gear(module, large_teeth, small_teeth): xdr = Range1d(start=-300, end=150) ydr = Range1d(start=-100, end=100) source = ColumnDataSource(data=dict(dummy=[0])) plot = Plot(title=None, data_sources=[source], x_range=xdr, y_range=ydr, width=800, height=800) plot.tools.extend( [PanTool(plot=plot), WheelZoomTool(plot=plot), ResetTool(plot=plot)]) radius = pitch_radius(module, large_teeth) angle = 0 glyph = Gear(x=-radius, y=0, module=module, teeth=large_teeth, angle=angle, fill_color=fill_color[0], line_color=line_color) renderer = Glyph(data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=glyph) plot.renderers.append(renderer) radius = pitch_radius(module, small_teeth) angle = half_tooth(small_teeth) glyph = Gear(x=radius, y=0, module=module, teeth=small_teeth, angle=angle, fill_color=fill_color[1], line_color=line_color) renderer = Glyph(data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=glyph) plot.renderers.append(renderer) return plot
def large_plot(n): from bokeh.objects import (Plot, PlotContext, LinearAxis, Grid, Glyph, 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, data_sources=[source]) xaxis = LinearAxis(plot=plot, dimension=0) yaxis = LinearAxis(plot=plot, dimension=1) xgrid = Grid(plot=plot, dimension=0) ygrid = Grid(plot=plot, dimension=1) tickers = [ xaxis.ticker, xaxis.formatter, yaxis.ticker, yaxis.formatter ] renderer = Glyph(data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=Line(x='x', y='y')) 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] + tickers + tools) return context, objects
def sample_gear(): xdr = Range1d(start=-30, end=30) ydr = Range1d(start=-30, end=30) source = ColumnDataSource(data=dict(dummy=[0])) plot = Plot(title=None, x_range=xdr, y_range=ydr, plot_width=800, plot_height=800) plot.add_tools(PanTool(), WheelZoomTool(), ResetTool()) glyph = Gear(x=0, y=0, module=5, teeth=8, angle=0, shaft_size=0.2, fill_color=fill_color[2], line_color=line_color) plot.add_glyph(source, glyph) return plot
def population(): xdr = FactorRange(factors=years) ydr = DataRange1d(sources=[source_known.columns("y"), source_predicted.columns("y")]) plot = Plot(title=None, data_sources=[source_known, source_predicted], x_range=xdr, y_range=ydr, plot_width=800, plot_height=200) xaxis = CategoricalAxis(plot=plot, dimension=0, major_label_orientation=pi/4) # yaxis = LinearAxis(plot=plot, dimension=1, ...) line_known = Line(x="x", y="y", line_color="violet", line_width=2) line_known_glyph = Glyph(data_source=source_known, xdata_range=xdr, ydata_range=ydr, glyph=line_known) plot.renderers.append(line_known_glyph) line_predicted = Line(x="x", y="y", line_color="violet", line_width=2, line_dash="dashed") line_predicted_glyph = Glyph(data_source=source_predicted, xdata_range=xdr, ydata_range=ydr, glyph=line_predicted) plot.renderers.append(line_predicted_glyph) legend = Legend(plot=plot, orientation="bottom_right", legends=dict(known=[line_known_glyph], predicted=[line_predicted_glyph])) plot.renderers.append(legend) return plot
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 population(): xdr = FactorRange(factors=years) ydr = DataRange1d(sources=[source_known.columns("y"), source_predicted.columns("y")]) plot = Plot(title=None, x_range=xdr, y_range=ydr, plot_width=800, plot_height=200) plot.add_layout(CategoricalAxis(major_label_orientation=pi/4), 'below') line_known = Line(x="x", y="y", line_color="violet", line_width=2) line_known_glyph = plot.add_glyph(source_known, line_known) line_predicted = Line(x="x", y="y", line_color="violet", line_width=2, line_dash="dashed") line_predicted_glyph = plot.add_glyph(source_predicted, line_predicted) plot.add_layout( Legend( orientation="bottom_right", legends=dict(known=[line_known_glyph], predicted=[line_predicted_glyph]) ) ) return plot
def pyramid(): xdr = DataRange1d(sources=[source_pyramid.columns("male"), source_pyramid.columns("female")]) ydr = DataRange1d(sources=[source_pyramid.columns("groups")]) plot = Plot(title=None, data_sources=[source_pyramid], x_range=xdr, y_range=ydr, plot_width=600, plot_height=600) xaxis = LinearAxis(plot=plot, dimension=0) yaxis = LinearAxis(plot=plot, dimension=1, ticker=SingleIntervalTicker(interval=5)) xgrid = Grid(plot=plot, dimension=0, axis=xaxis) ygrid = Grid(plot=plot, dimension=1, axis=yaxis) male_quad = Quad(left="male", right=0, bottom="groups", top="shifted", fill_color="blue") male_quad_glyph = Glyph(data_source=source_pyramid, xdata_range=xdr, ydata_range=ydr, glyph=male_quad) plot.renderers.append(male_quad_glyph) female_quad = Quad(left=0, right="female", bottom="groups", top="shifted", fill_color="violet") female_quad_glyph = Glyph(data_source=source_pyramid, xdata_range=xdr, ydata_range=ydr, glyph=female_quad) plot.renderers.append(female_quad_glyph) legend = Legend(plot=plot, legends=dict(Male=[male_quad_glyph], Female=[female_quad_glyph])) plot.renderers.append(legend) return plot
) ) xdr = DataRange1d(sources=[source.columns("x")]) ydr = DataRange1d(sources=[source.columns("y")]) line_glyph = Line(x="x", y="y", line_color="blue") renderer = Glyph( data_source = source, xdata_range = xdr, ydata_range = ydr, glyph = line_glyph ) plot = Plot(x_range=xdr, y_range=ydr, min_border=50) xaxis = LinearAxis(plot=plot) plot.below.append(xaxis) yaxis = LinearAxis(plot=plot) plot.left.append(yaxis) pantool = PanTool(dimensions=["width", "height"]) wheelzoomtool = WheelZoomTool(dimensions=["width", "height"]) previewsave = PreviewSaveTool(plot=plot) objectexplorer = ObjectExplorerTool() plot.renderers.append(renderer) plot.tools = [pantool, wheelzoomtool, previewsave, objectexplorer] doc = Document() doc.add(plot)
from numpy import pi, arange, sin, linspace from bokeh.browserlib import view from bokeh.document import Document from bokeh.embed import file_html from bokeh.glyphs import Circle from bokeh.objects import Plot, DataRange1d, LinearAxis, ColumnDataSource, Range1d, PanTool, WheelZoomTool from bokeh.resources import INLINE x = arange(-2 * pi, 2 * pi, 0.1) y = sin(x) y2 = linspace(0, 100, len(y)) source = ColumnDataSource(data=dict(x=x, y=y, y2=y2)) plot = Plot(x_range=Range1d(start=-6.5, end=6.5), y_range=Range1d(start=-1.1, end=1.1), min_border=80) plot.extra_y_ranges = {"foo": Range1d(start=0, end=100)} circle = Circle(x="x", y="y", fill_color="red", size=5, line_color="black") plot.add_glyph(source, circle) plot.add_layout(LinearAxis(), "below") plot.add_layout(LinearAxis(), "left") circle2 = Circle(x="x", y="y2", fill_color="blue", size=5, line_color="black") plot.add_glyph(source, circle2, y_range_name="foo") plot.add_layout(LinearAxis(y_range_name="foo"), "left")
def altitude_profile(data): plot = Plot(title="%s - Altitude Profile" % title, plot_width=800, plot_height=400) xaxis = LinearAxis(axis_label="Distance (km)") plot.add_layout(xaxis, 'below') yaxis = LinearAxis(axis_label="Altitude (m)") plot.add_layout(yaxis, 'left') xgrid = Grid(plot=plot, dimension=0, ticker=xaxis.ticker) ygrid = Grid(plot=plot, dimension=1, ticker=yaxis.ticker) plot.renderers.extend([xgrid, ygrid]) plot.add_tools(PanTool(), WheelZoomTool(), ResetTool(), BoxSelectTool()) X, Y = data.dist, data.alt y0 = min(Y) patches_source = ColumnDataSource( dict(xs=[[X[i], X[i + 1], X[i + 1], X[i]] for i in range(len(X[:-1]))], ys=[[y0, y0, Y[i + 1], Y[i]] for i in range(len(Y[:-1]))], color=data.colors[:-1])) patches = Patches(xs="xs", ys="ys", fill_color="color", line_color="color") plot.add_glyph(patches_source, patches) line_source = ColumnDataSource(dict( x=data.dist, y=data.alt, )) line = Line(x='x', y='y', line_color="black", line_width=1) plot.add_glyph(line_source, line) plot.x_range = DataRange1d(sources=[line_source.columns("x")]) plot.y_range = DataRange1d(sources=[line_source.columns("y")]) return plot
) ) xdr = DataRange1d(sources=[source.columns("petal_length")]) ydr = DataRange1d(sources=[source.columns("petal_width")]) circle = Circle(x="petal_length", y="petal_width", fill_color="color", fill_alpha=0.2, size=10, line_color="color") glyph_renderer = Glyph( data_source = source, xdata_range = xdr, ydata_range = ydr, glyph = circle, ) plot = Plot(x_range=xdr, y_range=ydr, data_sources=[source], min_border=80, title="Iris Data") xaxis = LinearAxis(plot=plot, dimension=0, location="min", axis_label="petal length", bounds=(1,7), major_tick_in=0) yaxis = LinearAxis(plot=plot, dimension=1, location="min", axis_label="petal width", bounds=(0,2.5), major_tick_in=0) xgrid = Grid(plot=plot, dimension=0, axis=xaxis) ygrid = Grid(plot=plot, dimension=1, axis=yaxis) pantool = PanTool(dimensions=["width", "height"]) wheelzoomtool = WheelZoomTool(dimensions=["width", "height"]) plot.renderers.append(glyph_renderer) plot.tools = [pantool, wheelzoomtool] sess = session.HTMLFileSession("iris.html") sess.add_plot(plot)
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())
county_renderer = Glyph( data_source = county_source, xdata_range = xdr, ydata_range = ydr, glyph = county_patches, ) state_renderer = Glyph( data_source = state_source, xdata_range = xdr, ydata_range = ydr, glyph = state_patches, ) plot = Plot(x_range=xdr, y_range=ydr, data_sources=[state_source, county_source], min_border=0, border_fill="white", title="2009 Unemployment Data", width=1300, height=800) resizetool = ResizeTool(plot=plot) plot.renderers.append(county_renderer) plot.renderers.append(state_renderer) plot.tools = [resizetool] sess = session.HTMLFileSession("choropleth.html") sess.add_plot(plot) if __name__ == "__main__": sess.save() print("Wrote %s" % sess.filename) sess.view()
plot.title = "%s vs. taylor(%s, n=%d)" % (expr, expr, order) legend.legends = [ ("%s" % expr, [line_f_glyph]), ("taylor(%s)" % expr, [line_t_glyph]), ] source.data = dict(x=x, fy=fy, ty=ty) slider.value = order session.store_document(document) source = ColumnDataSource(data=dict(x=[], fy=[], ty=[])) xdr = DataRange1d(sources=[source.columns("x")]) ydr = DataRange1d(sources=[source.columns("fy")]) plot = Plot(x_range=xdr, y_range=ydr, plot_width=800, plot_height=400) line_f = Line(x="x", y="fy", line_color="blue", line_width=2) line_f_glyph = plot.add_glyph(source, line_f) plot.add_layout(line_f_glyph) line_t = Line(x="x", y="ty", line_color="red", line_width=2) line_t_glyph = plot.add_glyph(source, line_t) plot.add_layout(line_t_glyph) xaxis = LinearAxis() plot.add_layout(xaxis, 'below') yaxis = LinearAxis() plot.add_layout(yaxis, 'left')
flowers["color"] = flowers["species"].map(lambda x: colormap[x]) source = ColumnDataSource( data=dict( petal_length=flowers["petal_length"], petal_width=flowers["petal_width"], sepal_length=flowers["sepal_length"], sepal_width=flowers["sepal_width"], color=flowers["color"], ) ) xdr = DataRange1d(sources=[source.columns("petal_length")]) ydr = DataRange1d(sources=[source.columns("petal_width")]) plot = Plot(x_range=xdr, y_range=ydr, min_border=80, title="Iris Data") circle = Circle(x="petal_length", y="petal_width", size=10, fill_color="color", fill_alpha=0.2, line_color="color") plot.add_glyph(source, circle) xaxis = LinearAxis(axis_label="petal length", bounds=(1, 7), major_tick_in=0) plot.add_layout(xaxis, "below") yaxis = LinearAxis(axis_label="petal width", bounds=(0, 2.5), major_tick_in=0) plot.add_layout(yaxis, "left") plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker)) plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker)) plot.add_tools(PanTool(), WheelZoomTool())
def make_plot(title, xname, yname): plot = Plot( x_range=xdr, y_range=ydr, title=title, plot_width=400, plot_height=400, border_fill='white', background_fill='#e9e0db' ) xaxis = LinearAxis(axis_line_color=None) plot.add_layout(xaxis, 'below') yaxis = LinearAxis(axis_line_color=None) plot.add_layout(yaxis, 'left') plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker)) plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker)) line = Line(x='x', y='y', line_color="#666699", line_width=2) plot.add_glyph(lines_source, line) circle = Circle( x=xname, y=yname, size=12, fill_color="#cc6633", line_color="#cc6633", fill_alpha=0.5 ) plot.add_glyph(circles_source, circle) return plot
def make_plot(name, glyph): plot = Plot(x_range=xdr, y_range=ydr, min_border=80) plot.add_glyph(source, glyph) xaxis = LinearAxis() plot.add_layout(xaxis, 'below') yaxis = LinearAxis() plot.add_layout(yaxis, 'left') plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker)) plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker)) plot.add_tools(PanTool(), WheelZoomTool()) document.add(plot) session.store_document(document)
x = arange(-2 * pi, 2 * pi, 0.1) y = sin(x) # Create an array of times, starting at the current time, and extending # for len(x) number of hours. times = np.arange(len(x)) * 3600000 + time.time() source = ColumnDataSource( data=dict(x=x, y=y, times=times) ) xdr = DataRange1d(sources=[source.columns("times")]) ydr = DataRange1d(sources=[source.columns("y")]) plot = Plot(x_range=xdr, y_range=ydr, min_border=80) circle = Circle(x="times", y="y", fill_color="red", size=5, line_color="black") plot.add_glyph(source, circle) plot.add_layout(DatetimeAxis(), 'below') plot.add_layout(DatetimeAxis(), 'left') plot.add_tools(PanTool(), WheelZoomTool()) doc = Document() doc.add(plot) if __name__ == "__main__": filename = "dateaxis.html" with open(filename, "w") as f:
line_glyph = Line(x="x", y="y", line_color="blue") line_glyph2 = Line(x="x", y="z", line_color="red") renderer = Glyph( data_source = source, xdata_range = xdr, ydata_range = ydr, glyph = line_glyph ) renderer2 = Glyph( data_source = source, xdata_range = xdr_static, ydata_range = ydr, glyph = line_glyph2 ) plot = Plot(x_range=xdr_static, 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")) plot.renderers.append(renderer) plot.renderers.append(renderer2) plot.tools = [pantool, zoomtool] sess = session.PlotServerSession( username="******", serverloc="http://localhost:5006", userapikey="nokey") sess.use_doc("line_animate") sess.add(plot, renderer, renderer2, xaxis, yaxis,
session = Session() session.use_doc('line_animate') session.load_document(document) x = linspace(-2*pi, 2*pi, 1000) x_static = linspace(-2*pi, 2*pi, 1000) y = sin(x) z = cos(x) source = ColumnDataSource(data=dict(x=x, y=y, z=z, x_static=x_static)) xdr = DataRange1d(sources=[source.columns("x")]) xdr_static = DataRange1d(sources=[source.columns("x_static")]) ydr = DataRange1d(sources=[source.columns("y")]) plot = Plot(x_range=xdr_static, y_range=ydr, min_border=50) line_glyph = Line(x="x", y="y", line_color="blue") plot.add_glyph(source, line_glyph) line_glyph2 = Line(x="x", y="z", line_color="red") plot.add_glyph(source, line_glyph2) plot.add_layout(LinearAxis(), 'below') plot.add_layout(LinearAxis(), 'left') plot.add_tools(PanTool(), WheelZoomTool()) document.add(plot) session.store_document(document)