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 make_plot(xname, yname, xax=False, yax=False, text=None): plot = Plot(x_range=xdr, y_range=ydr, background_fill="#efe8e2", border_fill='white', title="", min_border=2, border_symmetry=None, plot_width=250, plot_height=250) circle = Circle(x=xname, y=yname, fill_color="color", fill_alpha=0.2, size=4, line_color="color") plot.add_glyph(source, circle) xticker = BasicTicker() if xax: xaxis = LinearAxis() plot.add_layout(xaxis, 'below') xticker = xaxis.ticker plot.add_layout(Grid(dimension=0, ticker=xticker)) yticker = BasicTicker() if yax: yaxis = LinearAxis() plot.add_layout(yaxis, 'left') yticker = yaxis.ticker plot.add_layout(Grid(dimension=1, ticker=yticker)) plot.add_tools(PanTool(), WheelZoomTool()) 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") plot.add_glyph(text_source, text) 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) 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(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 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
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(): 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", x_range=xdr, y_range=ydr, plot_width=400, plot_height=400) line = Line(x="dates", y="downloads", line_color="blue") plot.add_glyph(source, line) circle = Circle(x="dates", y="downloads", fill_color="red") plot.add_glyph(source, circle) xaxis = DatetimeAxis() 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(HoverTool(tooltips=dict(downloads="@downloads"))) return plot, source
def make_plot(): xdr = DataRange1d(sources=[source.columns("dates")]) ydr = DataRange1d(sources=[source.columns("downloads")]) plot = Plot(title="Product downloads", x_range=xdr, y_range=ydr, plot_width=400, plot_height=400) line = Line(x="dates", y="downloads", line_color="blue") plot.add_glyph(source, line) circle = Circle(x="dates", y="downloads", fill_color="red") plot.add_glyph(source, circle) xaxis = DatetimeAxis() 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(HoverTool(tooltips=dict(downloads="@downloads"))) return plot, source
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
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 large_plot(): source = ColumnDataSource(data=dict(x=[0, 1], y=[0, 1])) xdr = Range1d(start=0, end=1) xdr.tags.append("foo") xdr.tags.append("bar") ydr = Range1d(start=10, end=20) ydr.tags.append("foo") plot = Plot(x_range=xdr, y_range=ydr) ydr2 = Range1d(start=0, end=100) plot.extra_y_ranges = {"liny": ydr2} circle = Circle(x="x", y="y", fill_color="red", size=5, line_color="black") plot.add_glyph(source, circle, name="mycircle") line = Line(x="x", y="y") plot.add_glyph(source, line, name="myline") rect = Rect(x="x", y="y", width=1, height=1, fill_color="green") plot.add_glyph(source, rect, name="myrect") plot.add_layout(DatetimeAxis(), 'below') plot.add_layout(LogAxis(), 'left') plot.add_layout(LinearAxis(y_range_name="liny"), 'left') plot.add_layout(Grid(dimension=0), 'left') plot.add_layout(Grid(dimension=1), 'left') plot.add_tools( BoxZoomTool(), PanTool(), PreviewSaveTool(), ResetTool(), ResizeTool(), WheelZoomTool(), ) 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, x_range=xdr, y_range=ydr, plot_width=600, plot_height=600) xaxis = LinearAxis() plot.add_layout(xaxis, 'below') yaxis = LinearAxis(ticker=SingleIntervalTicker(interval=5)) plot.add_layout(yaxis, 'left') plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker)) 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 = plot.add_glyph(source_pyramid, male_quad) female_quad = Quad(left=0, right="female", bottom="groups", top="shifted", fill_color="#CFF09E") female_quad_glyph = plot.add_glyph(source_pyramid, female_quad) plot.add_layout( Legend( legends=dict(Male=[male_quad_glyph], Female=[female_quad_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, x_range=xdr, y_range=ydr, plot_width=600, plot_height=600) xaxis = LinearAxis() plot.add_layout(xaxis, 'below') yaxis = LinearAxis(ticker=SingleIntervalTicker(interval=5)) plot.add_layout(yaxis, 'left') plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker)) 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 = plot.add_glyph(source_pyramid, male_quad) female_quad = Quad(left=0, right="female", bottom="groups", top="shifted", fill_color="#CFF09E") female_quad_glyph = plot.add_glyph(source_pyramid, female_quad) plot.add_layout(Legend(legends=dict(Male=[male_quad_glyph], Female=[female_quad_glyph]))) return plot
def make_plot(xname, yname, xax=False, yax=False, text=None): plot = Plot( x_range=xdr, y_range=ydr, background_fill="#efe8e2", border_fill='white', title="", min_border=2, border_symmetry=None, plot_width=250, plot_height=250) circle = Circle(x=xname, y=yname, fill_color="color", fill_alpha=0.2, size=4, line_color="color") plot.add_glyph(source, circle) xticker = BasicTicker() if xax: xaxis = LinearAxis() plot.add_layout(xaxis, 'below') xticker = xaxis.ticker plot.add_layout(Grid(dimension=0, ticker=xticker)) yticker = BasicTicker() if yax: yaxis = LinearAxis() plot.add_layout(yaxis, 'left') yticker = yaxis.ticker plot.add_layout(Grid(dimension=1, ticker=yticker)) plot.add_tools(PanTool(), WheelZoomTool()) 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" ) plot.add_glyph(text_source, text) 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 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)
source = ColumnDataSource(dict( names = list(css3_colors.Name), groups = list(css3_colors.Group), colors = list(css3_colors.Color), )) xdr = FactorRange(factors=list(css3_colors.Group.unique())) ydr = FactorRange(factors=list(reversed(css3_colors.Name))) plot = Plot(title="CSS3 Color Names", x_range=xdr, y_range=ydr, plot_width=600, plot_height=2000) rect = Rect(x="groups", y="names", width=1, height=1, fill_color="colors", line_color=None) plot.add_glyph(source, rect) xaxis_above = CategoricalAxis(major_label_orientation=pi/4) plot.add_layout(xaxis_above, 'above') xaxis_below = CategoricalAxis(major_label_orientation=pi/4) plot.add_layout(xaxis_below, 'below') plot.add_layout(CategoricalAxis(), 'left') doc = Document() doc.add(plot) if __name__ == "__main__": filename = "colors.html" with open(filename, "w") as f: f.write(file_html(doc, INLINE, "CSS3 Color Names")) print("Wrote %s" % filename) view(filename)
class Population(object): year = 2010 location = "World" def __init__(self): from bokeh.objects import ColumnDataSource from bokeh.document import Document from bokeh.session import Session from bokeh.sampledata.population import load_population self.document = Document() self.session = Session() self.session.use_doc('population') self.session.load_document(self.document) self.df = load_population() self.source_pyramid = ColumnDataSource(data=dict()) def render(self): self.pyramid_plot() self.create_layout() self.document.add(self.layout) self.update_pyramid() 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 on_year_change(self, obj, attr, old, new): self.year = int(new) self.update_pyramid() def on_location_change(self, obj, attr, old, new): self.location = new self.update_pyramid() def create_layout(self): from bokeh.widgets import Select, HBox, VBox years = list(map(str, sorted(self.df.Year.unique()))) locations = sorted(self.df.Location.unique()) year_select = Select(title="Year:", value="2010", options=years) location_select = Select(title="Location:", value="World", options=locations) year_select.on_change('value', self.on_year_change) location_select.on_change('value', self.on_location_change) controls = HBox(children=[year_select, location_select]) self.layout = VBox(children=[controls, self.plot]) def update_pyramid(self): pyramid = self.df[(self.df.Location == self.location) & (self.df.Year == self.year)] male = pyramid[pyramid.Sex == "Male"] female = pyramid[pyramid.Sex == "Female"] total = male.Value.sum() + female.Value.sum() male_percent = -male.Value / total female_percent = female.Value / total groups = male.AgeGrpStart.tolist() shifted = groups[1:] + [groups[-1] + 5] self.source_pyramid.data = dict( groups=groups, shifted=shifted, male=male_percent, female=female_percent, ) self.session.store_document(self.document)
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') xgrid = Grid(dimension=0, ticker=xaxis.ticker) ygrid = Grid(dimension=1, ticker=yaxis.ticker) legend = Legend(orientation="bottom_left")
x = np.linspace(-2 * pi, 2 * pi, 1000) y = sin(x) z = cos(x) source = ColumnDataSource(data=dict(x=x, y=y)) xdr = DataRange1d(sources=[source.columns("x")]) ydr = DataRange1d(sources=[source.columns("y")]) plot = Plot(x_range=xdr, y_range=ydr, min_border=50) line_glyph = Line(x="x", y="y", line_color="blue") plot.add_glyph(source, line_glyph) plot.add_layout(LinearAxis(), 'below') plot.add_layout(LinearAxis(), 'left') pan = PanTool() wheel_zoom = WheelZoomTool() preview_save = PreviewSaveTool() plot.add_tools(pan, wheel_zoom, preview_save) doc = Document() doc.add(plot) if __name__ == "__main__": filename = "line.html" with open(filename, "w") as f: f.write(file_html(doc, INLINE, "Line Glyph Example"))
# 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: f.write(file_html(doc, INLINE, "Date Axis Example")) print("Wrote %s" % filename) view(filename)
# used. Since no explicit default is provided, this picks up the # default in FillProps, which is "gray". fill_color="color", # An alternative form that explicitly sets a default value: #fill_color={"default": "red", "field": "color"}, # Note that line_color is set to a fixed value. This can be any of # the SVG named 147 colors, or a hex color string starting with "#", # or a string "rgb(r,g,b)" or "rgba(r,g,b,a)". # Any other string will be interpreted as a field name to look up # on the datasource. line_color="black") plot.add_glyph(source, circle) plot.add_layout(LinearAxis(), 'below') plot.add_layout(LinearAxis(), 'left') plot.add_tools(PanTool(), WheelZoomTool()) doc = Document() doc.add(plot) if __name__ == "__main__": filename = "colorspec.html" with open(filename, "w") as f: f.write(file_html(doc, INLINE, "Demonstration of ColorSpec")) print("Wrote %s" % filename) view(filename)
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()) doc = Document() doc.add(plot) if __name__ == "__main__": filename = "iris.html" with open(filename, "w") as f:
] 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') xgrid = Grid(dimension=0, ticker=xaxis.ticker) ygrid = Grid(dimension=1, ticker=yaxis.ticker) legend = Legend(orientation="bottom_left")
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") plot.add_tools(PanTool(), WheelZoomTool()) doc = Document() doc.add(plot) if __name__ == "__main__": filename = "twin_axis.html"
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: f.write(file_html(doc, INLINE, "Date Axis Example")) print("Wrote %s" % filename) view(filename)
from bokeh.objects import Plot, DataRange1d, LinearAxis, ColumnDataSource, PanTool, WheelZoomTool from bokeh.resources import INLINE x = arange(-2 * pi, 2 * pi, 0.1) y = sin(x) source = ColumnDataSource(data=dict(x=x, y=y)) xdr = DataRange1d(sources=[source.columns("x")]) ydr = DataRange1d(sources=[source.columns("y")]) plot = Plot(x_range=xdr, y_range=ydr, min_border=80) 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") plot.add_tools(PanTool(), WheelZoomTool()) doc = Document() doc.add(plot) if __name__ == "__main__": filename = "glyph1.html" with open(filename, "w") as f: f.write(file_html(doc, INLINE, "Glyph Plot")) print("Wrote %s" % filename) view(filename)
source = ColumnDataSource(data=dict(x=x, y=y, r=r)) xdr = DataRange1d(sources=[source.columns("x")]) ydr = DataRange1d(sources=[source.columns("y")]) plot = Plot(x_range=xdr, y_range=ydr, min_border=80) circle = Circle( x="x", y="y", size="r", fill_color="red", line_color="black" ) plot.add_glyph(source, circle) 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) link = session.object_link(document.context) print ("please visit %s to see plots" % link) view(link)
class Population(object): year = 2010 location = "World" def __init__(self): from bokeh.objects import ColumnDataSource from bokeh.document import Document from bokeh.session import Session from bokeh.sampledata.population import load_population self.document = Document() self.session = Session() self.session.use_doc('population_reveal') self.session.load_document(self.document) self.df = load_population() self.source_pyramid = ColumnDataSource(data=dict()) # just render at the initialization self._render() def _render(self): self.pyramid_plot() self.create_layout() self.document.add(self.layout) self.update_pyramid() 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 on_year_change(self, obj, attr, old, new): self.year = int(new) self.update_pyramid() def on_location_change(self, obj, attr, old, new): self.location = new self.update_pyramid() def create_layout(self): from bokeh.widgets import Select, HBox, VBox years = list(map(str, sorted(self.df.Year.unique()))) locations = sorted(self.df.Location.unique()) year_select = Select(title="Year:", value="2010", options=years) location_select = Select(title="Location:", value="World", options=locations) year_select.on_change('value', self.on_year_change) location_select.on_change('value', self.on_location_change) controls = HBox(year_select, location_select) self.layout = VBox(controls, self.plot) def update_pyramid(self): pyramid = self.df[(self.df.Location == self.location) & (self.df.Year == self.year)] male = pyramid[pyramid.Sex == "Male"] female = pyramid[pyramid.Sex == "Female"] total = male.Value.sum() + female.Value.sum() male_percent = -male.Value / total female_percent = female.Value / total groups = male.AgeGrpStart.tolist() shifted = groups[1:] + [groups[-1] + 5] self.source_pyramid.data = dict( groups=groups, shifted=shifted, male=male_percent, female=female_percent, ) self.session.store_document(self.document)
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') plot.add_tools(PanTool(), WheelZoomTool()) doc = Document() doc.add(plot) if __name__ == "__main__": filename = "twin_axis.html"