In this chapter we will learn how to configure various visual aspects of our plots, and how to find out more about what an be configured.
from bokeh.io import output_notebook, show from bokeh.plotting import figure
This notebook uses Bokeh sample data. If you haven't downloaded it already, this can be downloaded by running the following:
import bokeh.sampledata bokeh.sampledata.download()
Using data directory: /Users/bryan/.bokeh/data Skipping 'CGM.csv' (checksum match) Skipping 'US_Counties.zip' (checksum match) Skipping 'us_cities.json' (checksum match) Skipping 'unemployment09.csv' (checksum match) Skipping 'AAPL.csv' (checksum match) Skipping 'FB.csv' (checksum match) Skipping 'GOOG.csv' (checksum match) Skipping 'IBM.csv' (checksum match) Skipping 'MSFT.csv' (checksum match) Skipping 'WPP2012_SA_DB03_POPULATION_QUINQUENNIAL.zip' (checksum match) Skipping 'gapminder_fertility.csv' (checksum match) Skipping 'gapminder_population.csv' (checksum match) Skipping 'gapminder_life_expectancy.csv' (checksum match) Skipping 'gapminder_regions.csv' (checksum match) Skipping 'world_cities.zip' (checksum match) Skipping 'airports.json' (checksum match) Skipping 'movies.db.zip' (checksum match) Skipping 'airports.csv' (checksum match) Skipping 'routes.csv' (checksum match) Skipping 'haarcascade_frontalface_default.xml' (checksum match)
Before we get started, it's useful to describe how colors and properties are specified in Bokeh.
There are many places where you may need to specify colors. Bokeh can accept colors in a variety of different ways:
Regardless of how a Bokeh plot is created, styling the visual aspects of the plot can always be accomplished by setting attributes on the Bokeh objects that comprise the resulting plot. Visual properties come in three kinds: line, fill, and text properties. For full information with code and examples see the Styling Visual Properties section of the user guide.
Set the visual appearance of lines. The most common are
Set the visual appearance of filled areas:
Set the visual appearance of lines of text. The most common are
Sometimes a prefix is used with property names, e.g. to distinguish between different line properties on the same object, or to give a more meaningful name. For example, to set the line width of the plot outline, you would say
myplot.outline_line_width = 2.
# create a new plot with a title p = figure(plot_width=400, plot_height=400) p.outline_line_width = 7 p.outline_line_alpha = 0.3 p.outline_line_color = "navy" p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) show(p)
# EXERCISE Create a plot of your own and customize several plot-level properties
It's also possible to style the visual properties of glyphs (see the Glyphs section of the styling guide for more information). When using
bokeh.plotting this is often done when calling the glyph methods:
p.circle(line_color="red", fill_alpha=0.2, ...)
But it is also possible to set these properties directly on glyph objects. Glyph objects are found on
GlyphRenderer objects, which are returned by the
bokeh.plotting glyph methods like
rect, etc. Let's look at an example:
p = figure(plot_width=400, plot_height=400) # keep a reference to the returned GlyphRenderer r = p.circle([1,2,3,4,5], [2,5,8,2,7]) r.glyph.size = 50 r.glyph.fill_alpha = 0.2 r.glyph.line_color = "firebrick" r.glyph.line_dash = [5, 1] r.glyph.line_width = 2 show(p)
You can also control how glyphs look when there are selections involved. The set of "selected" points is displayed according to the optional
.selection_glyph property of a
r.selection_glyph = Circle(fill_alpha=1, fill_color="firebrick", line_color=None)
When there is a non-empty selection, the set of "unselected" points is displayed according to the optional
.nonselection_glyph property of a
r.nonselection_glyph = Circle(fill_alpha=0.2, fill_color="grey", line_color=None)
When using the
bokeh.plotting interface, it is easier to pass these visual properties to the glyph methods as shown below. The glyph method will create the selection or nonselection glyphs and attach them to the renderer for you.
p = figure(plot_width=400, plot_height=400, tools="tap", title="Select a circle") renderer = p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=50, # set visual properties for selected glyphs selection_color="firebrick", # set visual properties for non-selected glyphs nonselection_fill_alpha=0.2, nonselection_fill_color="grey", nonselection_line_color="firebrick", nonselection_line_alpha=1.0) show(p)
It is also possible to specify the visual appearance of glyphs when they are "inspected", e.g. by a hover tool. This is accomplished by setting an optional
hover_glyph on the glyph renderer:
r.hover_glyph = Circle(fill_alpha=1, fill_color="firebrick", line_color=None)
Or if using
bokeh.plotting glyph methods, by passing
hover_fill_alpha, etc. to the glyph method. Lets look at an example that works together with a
HoverTool configured for "hline" hit-testing.
from bokeh.models.tools import HoverTool from bokeh.sampledata.glucose import data subset = data.loc['2010-10-06'] x, y = subset.index.to_series(), subset['glucose'] # Basic plot setup p = figure(width=600, height=300, x_axis_type="datetime", title='Hover over points') p.line(x, y, line_dash="4 4", line_width=1, color='gray') cr = p.circle(x, y, size=20, fill_color="grey", hover_fill_color="firebrick", fill_alpha=0.05, hover_alpha=0.3, line_color=None, hover_line_color="white") p.add_tools(HoverTool(tooltips=None, renderers=[cr], mode='hline')) show(p)
# EXERCISE: experiment with standard, selected, hover glyph visual properties
Next we will take a look at stlying of Axes.
To style axes, you first must get ahold of
Axis objects. The simplest way is to use some convenience methods on
yaxis. These methods return lists of axis objects:
>>> p.xaxis [<bokeh.models.axes.LinearAxis at 0x106fa2390>]
However, you can set properties on all the elements of the list as if it was a single object:
p.xaxis.axis_label = "Temperature" p.axis.major_label_text_color = "orange"
These are referred to as "splattable" lists, and tab completion works on them as well.
# EXERCISE Try out tab completion. Type p.xaxis.<press tab key> to see a list of attributes that can be set.
Axes objects have many configurable properties that afford control over most visual aspects of an axis. These can be grouped by function according to prefix:
axis line properties e.g
axis_label text properties e.g.
axis_label_text_color, as well as
major_label text properties e.g.
major_label_text_font_size, as well as
major_tick line_properties e.g.
major_tick_line_dash, as well as
minor_tick line properties e.g.
minor_tick_line_width, as well as
As a simple first case, let's change the orientation of the major tick labels on both axes of a plot:
from math import pi p = figure(plot_width=400, plot_height=400) p.x([1,2,3,4,5], [2,5,8,2,7], size=10, line_width=2) p.xaxis.major_label_orientation = pi/4 p.yaxis.major_label_orientation = "vertical" show(p)
The next example shows customizations on several of the different Axis properties at once:
p = figure(plot_width=400, plot_height=400) p.asterisk([1,2,3,4,5], [2,5,8,2,7], size=12, color="olive") # change just some things about the x-axes p.xaxis.axis_label = "Temp" p.xaxis.axis_line_width = 3 p.xaxis.axis_line_color = "red" # change just some things about the y-axes p.yaxis.axis_label = "Pressure" p.yaxis.major_label_text_color = "orange" p.yaxis.major_label_orientation = "vertical" # change things on all axes p.axis.minor_tick_in = -3 p.axis.minor_tick_out = 6 show(p)
# EXERCISE Create a plot of your own and customize several axis properties
All Bokeh axes have a
formatter property, whose value is a
TickFormatter object that Bokeh uses to format the ticks displayed by that axis. Bokeh will configure default tick formatters for numeric, datetime, or categotical axes. But often we would like to customize the appearance of tick labels. This can be accomplished by changing properties on the default formatter that Bokeh chooses, or by replacing the formatter with a new type entirely.
Let's first look at changing the properties of a default formatter. The default datetime formatter is configured to show month/day when the axis is on the scale of days. If would like the also always show the year, we can change the
days property to a format that includes the year, as done below.
from math import pi from bokeh.sampledata.glucose import data week = data.loc['2010-10-01':'2010-10-08'] p = figure(x_axis_type="datetime", title="Glocose Range", plot_height=350, plot_width=800) p.xaxis.formatter.days = '%m/%d/%Y' p.xaxis.major_label_orientation = pi/3 p.line(week.index, week.glucose) show(p)
See the reference guide entry for DatetimeTickFormatter to see other properties that can be updated.
In addition to the tick formatters that Bokeh will use by default, there are others such as the
NumeralTickFormatter that we can configure explicitly. The example below shows how to set a formatter on each axis.
from bokeh.models import NumeralTickFormatter p = figure(plot_height=300, plot_width=800) p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) p.xaxis.formatter = NumeralTickFormatter(format="0.0%") p.yaxis.formatter = NumeralTickFormatter(format="$0.00") show(p)
Try experimenting with the format argument and re-execute the cell above.
It is also possible to customize where ticks will be drawn. See the Tick Locations section of the User's Guide for more information.
It is also possible to control the styling of Grids
Grids properties in Bokeh have two possible prefixes:
In this first example we turn off the vertical grid lines (by setting the line color to None) and set the horizontal grid to be light and dashed.
p = figure(plot_width=400, plot_height=400) p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) # change just some things about the x-grid p.xgrid.grid_line_color = None # change just some things about the y-grid p.ygrid.grid_line_alpha = 0.5 p.ygrid.grid_line_dash = [6, 4] show(p)
The next example shows how the "band" properties of a plot can be specified
p = figure(plot_width=400, plot_height=400) p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) # change just some things about the x-grid p.xgrid.grid_line_color = None # change just some things about the y-grid p.ygrid.band_fill_alpha = 0.1 p.ygrid.band_fill_color = "navy" show(p)
# EXERCISE Create a plot of your own and customize several grid properties