Bokeh 简明教程

Bokeh - Annotations and Legends

注释是对图表添加的解释性文本片段。可以通过指定绘图标题、x 和 y 轴标签以及在绘图区域的任何位置插入文本标签来注释 Bokeh 绘图。

Annotations are pieces of explanatory text added to the diagram. Bokeh plot can be annotated by way of specifying plot title, labels for x and y axes as well as inserting text labels anywhere in the plot area.

可以在 Figure 构造函数中提供绘图标题以及 x 和 y 轴标签。

Plot title as well as x and y axis labels can be provided in the Figure constructor itself.

fig = figure(title, x_axis_label, y_axis_label)

在以下绘图中,这些属性如下所示 -

In the following plot, these properties are set as shown below −

from bokeh.plotting import figure, output_file, show
import numpy as np
import math
x = np.arange(0, math.pi*2, 0.05)
y = np.sin(x)
fig = figure(title = "sine wave example", x_axis_label = 'angle', y_axis_label = 'sin')
fig.line(x, y,line_width = 2)
show(p)

Output

annotations

标题的文本和轴标签也可以通过将适当的字符串值分配给 figure 对象的对应属性来指定。

The title’s text and axis labels can also be specified by assigning appropriate string values to corresponding properties of figure object.

fig.title.text = "sine wave example"
fig.xaxis.axis_label = 'angle'
fig.yaxis.axis_label = 'sin'

还可以指定标题的位置、对齐方式、字体和颜色。

It is also possible to specify location, alignment, font and color of title.

fig.title.align = "right"
fig.title.text_color = "orange"
fig.title.text_font_size = "25px"
fig.title.background_fill_color = "blue"

向绘图图形中添加图例非常容易。我们必须使用任何字形方法的图例属性。

Adding legends to the plot figure is very easy. We have to use legend property of any glyph method.

下面我们有三个带有三个不同图例的绘图曲线 -

Below we have three glyph curves in the plot with three different legends −

from bokeh.plotting import figure, output_file, show
import numpy as np
import math
x = np.arange(0, math.pi*2, 0.05)
fig = figure()
fig.line(x, np.sin(x),line_width = 2, line_color = 'navy', legend = 'sine')
fig.circle(x,np.cos(x), line_width = 2, line_color = 'orange', legend = 'cosine')
fig.square(x,-np.sin(x),line_width = 2, line_color = 'grey', legend = '-sine')
show(fig)

Output

legends