This section of the tutorial covers the
interface. This interface is a "mid-level" interface, and the main idea can be described by the statement:
Starting from simple default figures (with sensible default tools, grids and axes), add markers and other shapes whose visual attributes are tied to directly data.
We will see that it is possible to customize and change all of the defaults, but having them means that it is possible to get up and running very quickly.
When using the
bokeh.plotting interface, there are a few common imports:
from bokeh.io import output_notebook, show from bokeh.plotting import figure
In this case, we are in the Jupyter notebook, so call
output_notebook(). We only need to call this once, and all subsequent calls to
show() will display inline in the notebook.
In this section you will see how to use Bokeh's various marker types to create simple scatter plots.
# create a new plot with default tools, using figure p = figure(plot_width=400, plot_height=400) # add a circle renderer with a size, color, and alpha p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=15, line_color="navy", fill_color="orange", fill_alpha=0.5) show(p) # show the results
All Bokeh markers accept
size (measured in screen space units) as a property. Circles also have
radius (measured in "data" space units).
# EXERCISE: Try changing the example above to set a `radius` value instead of `size`
To scatter square markers instead of circles, you can use the
square method on figures.
# create a new plot using figure p = figure(plot_width=400, plot_height=400) # add a square renderer with a size, color, alpha, and sizes p.square([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=[10, 15, 20, 25, 30], color="firebrick", alpha=0.6) show(p) # show the results
Note that in the example above, we are also specifying different sizes for each individual marker. In general, all of a glyph's properties can be "vectorized" in this fashion. Also note that we have passed
color as a shorthand to set both the line and fill colors easily at the same time. This is a convenience specific to
There are many marker types available in Bokeh, you can see details and example plots for all of them in the reference guide by clicking on entries in the list below:
# EXERCISE: Plot some different markers in this cell
# create a new plot (with a title) using figure p = figure(plot_width=400, plot_height=400, title="My Line Plot") # add a line renderer p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2) show(p) # show the results
The example below shows how to use the
image_rgba method to display raw RGBA data.
Note: This example makes use of the NumPy library
from __future__ import division import numpy as np # set up some data N = 20 img = np.empty((N,N), dtype=np.uint32) view = img.view(dtype=np.uint8).reshape((N, N, 4)) for i in range(N): for j in range(N): view[i, j, 0] = int(i/N*255) # red view[i, j, 1] = 158 # green view[i, j, 2] = int(j/N*255) # blue view[i, j, 3] = 255 # alpha # create a new plot (with a fixed range) using figure p = figure(x_range=[0,10], y_range=[0,10]) # add an RGBA image renderer p.image_rgba(image=[img], x=, y=, dw=, dh=) show(p) # show the results
# EXERCISE: Plot some of the other glyph types, following the examples in the User Guide.
It is possible to combine more than one glyph on a single figure. You just need to call multiple glyph methods on one figure object:
# set up some data x = [1, 2, 3, 4, 5] y = [6, 7, 8, 7, 3] # create a new plot with figure p = figure(plot_width=400, plot_height=400) # add both a line and circles on the same plot p.line(x, y, line_width=2) p.circle(x, y, fill_color="white", size=8) show(p) # show the results
# EXERCISE: create your own plot combining multiple glyphs together