从`figure()`函数的外部设置python`flow`情节图的标题

kri*_*nab 7 python bokeh

bokeh在python中使用绘图包时遇到了一个非常简单的问题.

我想从通常的图形构造函数外部设置散景图的标题,但是我得到一个奇怪的错误.

这是代码.

from bokeh.plotting import figure
p = figure()
p.title = 'new title'
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试此代码时,我收到一条错误消息:

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

因此,我似乎需要创建一个Title对象或其他东西传递给图.但是在散景文档中没有提到如何设置标题.仅提及如何更改标题字体或标题颜色等.

有没有人知道如何从通常的外部设置情节的标题 figure(title='new title')

Wes*_*ill 14

要在不构建新Title对象的情况下更改标题,可以设置图形的title.text属性:

from bokeh.plotting import figure
p = figure()
p.title.text = 'New title'
Run Code Online (Sandbox Code Playgroud)

  • 根据一位散景开发人员的说法,这是有效的并且是正确的方法。 (2认同)

Hal*_*Ali 7

你必须指定的实例Titlep.title.因为,我们可以使用函数调查python中的事物类型,type弄清楚这些事情是相当简单的.

> type(p.title) 
bokeh.models.annotations.Title
Run Code Online (Sandbox Code Playgroud)

这是一个jupyter笔记本中的完整示例:

from bokeh.models.annotations import Title
from bokeh.plotting import figure, show
import numpy as np
from bokeh.io import output_notebook
output_notebook()
x = np.arange(0, 2*np.pi, np.pi/100)
y = np.sin(x)
p = figure()
p.circle(x, y)
t = Title()
t.text = 'new title'
p.title = t
show(p)
Run Code Online (Sandbox Code Playgroud)

输出以下图表,标题设置为new title:

示例输出