Skillversitas

Getting Started with Bokeh

This chapter builds your first Bokeh figure, saves it as a standalone HTML file, and labels the moving parts. Everything here uses the high-level bokeh.plotting interface.

Minimum Viable Plot

Three lines produce an interactive plot: create a figure, add a glyph, call show.

from bokeh.plotting import figure, show

p = figure(width=400, height=300)
p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=15)
show(p)

When run from a script, show() writes plot.html beside the script, opens it in the default browser, and the object stored in p is automatically embedded. In a notebook, show() renders inline instead.

Saving Instead of Showing

To produce a file without opening a browser, use save, optionally setting a title that appears in the browser tab:

from bokeh.plotting import figure, save, output_file

output_file("first_plot.html", title="First Bokeh Plot")
p = figure(width=400, height=300, title="My First Plot")
p.line([1, 2, 3, 4], [3, 7, 4, 9], line_width=2, color="navy")
save(p)

output_file() sets the destination that show() or save() will use; it must be called before the figure is rendered.

Adding Multiple Glyphs

Figures are layered in draw order — later calls render on top:

p = figure(title="Layered Glyphs", width=450, height=300)
p.line([1, 2, 3, 4], [4, 6, 5, 8], line_width=2, color="navy")
p.circle([1, 2, 3, 4], [4, 6, 5, 8], size=9, color="orange", alpha=0.8)
p.xaxis.axis_label = "Week"
p.yaxis.axis_label = "Sales"

Tools That Come for Free

Every figure ships with a default tool set: pan, wheel zoom, box zoom, save, reset, and a hover-adjacent box select. Pass an explicit list to customize:

from bokeh.models import HoverTool

p = figure(width=400, height=300,
           tools="pan,wheel_zoom,box_zoom,reset,hover")
hover = p.select_one(HoverTool)
hover.tooltips = [
    ("Week", "@x"),
    ("Sales", "@y"),
]

Styling a Title

Figure titles are text models you can align and style in place:

p.title.text = "Quarterly Results"
p.title.text_color = "gray"
p.title.text_font_size = "16pt"
p.title.align = "center"

Summary

A first Bokeh chart needs only figure, a glyph call, and show or save after output_file; layering more glyph calls, customizing tools, and setting axis or title text properties complete the basic workflow.