第一步 2:添加和自定义渲染器#

之前的入门指南中,你使用了 Bokeh 的 figure() 函数来渲染折线图。

在本节中,你将使用不同的渲染器函数来创建各种其他类型的图形。你还将自定义字形的外观。

渲染不同的字形#

Bokeh 的 plotting 接口支持许多不同的字形,例如线条、条形、六边形瓦片或其他多边形。

另请参阅

所有支持的字形方法的完整列表可在 Bokeh 的 figure() 函数的参考指南中找到。有关 Bokeh 字形的详细信息,请参阅 Bokeh 用户指南中的基本绘图

渲染圆形#

使用 scatter() 函数而不是 line() 函数来渲染圆形。圆形是 scatter 函数的默认标记类型。

p.scatter(x, y3, legend_label="Objects", color="yellow", size=12)

将你之前可视化中的一个 line() 函数替换为 scatter() 函数来创建圆形。

from bokeh.plotting import figure, show

# prepare some data
x = [1, 2, 3, 4, 5]
y1 = [6, 7, 2, 4, 5]
y2 = [2, 3, 4, 5, 6]
y3 = [4, 5, 5, 7, 2]

# create a new plot with a title and axis labels
p = figure(title="Multiple glyphs example", x_axis_label="x", y_axis_label="y")

# add multiple renderers
p.line(x, y1, legend_label="Temp.", color="#004488", line_width=3)
p.line(x, y2, legend_label="Rate", color="#906c18", line_width=3)
p.scatter(x, y3, legend_label="Objects", color="#bb5566", size=16)

# show the results
show(p)

渲染条形图#

类似地,使用 vbar() 函数来渲染垂直条形图。

p.vbar(x=x, top=y2, legend_label="Rate", width=0.5, bottom=0, color="red")

vbar() 函数添加到你之前的可视化中。

from bokeh.plotting import figure, show

# prepare some data
x = [1, 2, 3, 4, 5]
y1 = [6, 7, 2, 4, 5]
y2 = [2, 3, 4, 5, 6]
y3 = [4, 5, 5, 7, 2]

# create a new plot with a title and axis labels
p = figure(title="Multiple glyphs example", x_axis_label="x", y_axis_label="y")

# add multiple renderers
p.line(x, y1, legend_label="Temp.", color="blue", line_width=2)
p.vbar(x=x, top=y2, legend_label="Rate", width=0.5, bottom=0, color="red")
p.scatter(x, y3, legend_label="Objects", color="yellow", size=12)

# show the results
show(p)

另请参阅

要了解有关条形图以及 Bokeh 处理分类数据的其他方式的更多信息,请参阅用户指南中的条形图

自定义字形#

不同的渲染器函数接受各种参数来控制字形的外观。

定义新字形的属性#

例如,scatter() 函数允许你定义圆形的颜色或直径等属性

  • fill_color:圆形的填充颜色

  • fill_alpha:填充颜色的透明度(介于 01 之间的任何值)

  • line_color:圆形轮廓的线条颜色

  • size:圆形的大小(以屏幕单位为单位)

  • legend_label:圆形的图例条目

scatter() 函数也支持其他标记类型

  • marker:所选形状的名称,例如“circle”、“quad”、“y”。 有关完整列表,请参见 MarkerType

请注意,在前面的示例中,你使用了 color 属性来定义对象的颜色。color 是一个别名,会自动将对象的所有颜色属性设置为相同的颜色。例如,将 "yellow" 传递给圆形的 color 属性与分别将 fill_colorline_color 设置为黄色是相同的。

在 Bokeh 中,你可以通过几种方式指定颜色。例如

  • 使用命名的 CSS 颜色之一(例如,"firebrick"

  • 使用十六进制值,以 # 开头(例如 "#00ff00"

  • 使用 RGB 颜色的 3 元组(例如,(100, 100, 255)

  • 使用 RGBA 颜色的 4 元组(例如 (100, 100, 255, 0.5)

创建图例标签为“Objects”的圆形,并使圆形看起来稍微透明,填充颜色为红色,轮廓为蓝色

from bokeh.plotting import figure, show

# prepare some data
x = [1, 2, 3, 4, 5]
y = [4, 5, 5, 7, 2]

# create a new plot with a title and axis labels
p = figure(title="Glyphs properties example", x_axis_label="x", y_axis_label="y")

# add circle renderer with additional arguments
p.scatter(
    x,
    y,
    marker="circle",
    size=80,
    legend_label="Objects",
    fill_color="red",
    fill_alpha=0.5,
    line_color="blue",
)

# show the results
show(p)

更改现有字形的属性#

如果要在创建对象后更改任何属性,你可以直接定义和覆盖对象的属性。

以上面的圆形为例。你通过传递参数 fill_color="red" 将圆形定义为红色。

要将圆形的颜色从红色更改为蓝色,你首先需要在调用 scatter() 函数时为新对象分配一个变量名(例如 scatter)。

scatter = p.scatter(
    marker="circle",
    x,
    y,
    size=80,
    legend_label="Objects",
    fill_color="red",
    fill_alpha=0.5,
    line_color="blue",
  )

接下来,使用该变量访问对象的 glyph 属性并更改其属性

glyph = scatter.glyph
glyph.fill_color = "blue"

再次生成红色圆形,但这次在输出绘图之前将其颜色更改为蓝色

from bokeh.plotting import figure, show

# prepare some data
x = [1, 2, 3, 4, 5]
y = [4, 5, 5, 7, 2]

# create a new plot with a title and axis labels
p = figure(title="Glyphs properties example", x_axis_label="x", y_axis_label="y")

# add circle renderer with additional arguments
circle = p.scatter(
    x,
    y,
    marker="circle",
    size=80,
    legend_label="Objects",
    fill_color="red",
    fill_alpha=0.5,
    line_color="blue",
)

# change color of previously created object's glyph
glyph = circle.glyph
glyph.fill_color = "blue"

# show the results
show(p)

另请参阅

有关各种视觉属性的更多信息,请参阅用户指南中的字形样式通用视觉属性

每种类型的字形都有不同的属性。请参阅参考指南中的 figure() 以查看每种字形方法的所有可用属性。