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

之前的第一步指南中,您使用了Bokeh的 figure()函数来渲染折线图。

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

渲染不同的字形#

Bokeh的绘图接口支持许多不同的图形符号,如线条、条形、六边形瓦片或其他多边形。

另请参阅

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

渲染圆圈#

使用circle()函数而不是 line()来渲染圆形:

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

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

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处理分类数据的其他方法,请参阅用户指南中的条形图

自定义字形#

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

定义新图标的属性#

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

  • fill_color: 圆圈的填充颜色

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

  • line_color: 圆圈轮廓的填充颜色

  • size: 圆圈的大小(以屏幕单位数据单位为单位)

  • legend_label: 图例中圆圈的标签

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

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

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

  • 使用十六进制值,前面加上#(例如"#00ff00"

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

  • 使用4元组表示RGBA颜色(例如 (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" 定义了圆圈的颜色为红色。

要将圆圈的颜色从红色更改为蓝色,首先需要在调用circle()函数时为新对象分配一个变量名(例如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() 在参考指南中查看每种字形方法的所有可用属性。