set title of a python `bokeh` plot figure from outside of the `figure()` function

krishnab picture krishnab · Dec 9, 2017 · Viewed 9.3k times · Source

I ran into a very seemingly simple problem when using the bokeh plotting package in python.

I wanted to set the title of a bokeh figure from outside of the usual figure constructor, but I get a strange error.

Here is the code.

from bokeh.plotting import figure
p = figure()
p.title = 'new title'

But when I tried this code, I get an error message:

ValueError: expected an instance of type Title, got new plot of type str

So it seems like I need to create a Title object or something to pass to the figure. However in the bokeh documentation there is no mention of how to set the title. There is only mention of how to change the title font or the title color, etc.

Does anyone know how to set the title of the plot from outside of the usual figure(title='new title')

Answer

Wesley Hill picture Wesley Hill · Jul 12, 2018

To simply change the title without constructing a new Title object, you can set the figure's title.text attribute:

from bokeh.plotting import figure
p = figure()
p.title.text = 'New title'