Bokeh Tutorial — Styling and Appearance


In [9]:
from bokeh.io import output_notebook, show

from bokeh.plotting import figure

In [10]:
output_notebook()


BokehJS successfully loaded.

Warning: BokehJS previously loaded

Colors

There are many places where you may need to specify colors. Bokeh can accept colors in a variety of different ways:

  • any of the 147 named CSS colors, e.g 'green', 'indigo'
  • an RGB(A) hex value, e.g., '#FF0000', '#44444444'
  • a 3-tuple of integers (r,g,b) between 0 and 255
  • a 4-tuple of (r,g,b,a) where r, g, b are integers between 0 and 255 and a is a floating point value between 0 and 1

Properties

Line Properties

Fill Properties

Text Properties

Plots

Plots


In [ ]:


In [3]:
# EXERCISE Create a plot of your own and customize several plot-level properties

Axes

Axes

To style axes, you first must get ahold of Axis objects. The simplest way is to use some convenience methods on Plot: axis, xaxis, and 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.


In [7]:
# EXERCISE Try out tab completion. Type p.xaxis.<press tab key> to see a list of attributes that can be set.

As a simple first example, let's change the orientation of the major tick labels on both axes of a plot:


In [11]:
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:


In [13]:
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)



In [7]:
# EXERCISE Create a plot of your own and customize several axis properties

There are further customizations possible. See the User Guide for more information on topics such as tick label formatting or limiting axis bounds.


In [16]:
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)



In [5]:
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)



In [10]:
# EXERCISE Create a plot of your own and customize several grid properties

Legends

Legends


In [15]:
import numpy as np

x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)

p = figure()

p.circle(x, y, legend="sin(x)")
p.line(x, y, legend="sin(x)")

p.line(x, 2*y, legend="2*sin(x)", line_dash=[4, 4], line_color="orange", line_width=2)

p.line(x, 3*y, legend="3*sin(x)", line_color="green")
p.square(x, 3*y, legend="3*sin(x)", fill_color="white", line_color="green")

p.legend.orientation = "bottom_left"

show(p)



In [12]:
# EXERCISE Create a plot of your own and add a legend