第一步 3: 添加图例、文本和注释#
在之前的第一步指南中,您生成了不同的图形并定义了它们的外观。
在本节中,您将添加并设置图例 和标题的样式。您还将通过包含 注释来为您的图表添加更多信息。
添加和样式化图例#
如果你在调用渲染器函数时包含legend_label属性,Bokeh会自动为你的图表添加图例。例如:
p.circle(x, y3, legend_label="Objects")
这会在您的图表中添加一个名为“Objects”的图例。
使用Legend对象的属性来自定义图例。例如:
from bokeh.plotting import figure, show
# prepare some data
x = [1, 2, 3, 4, 5]
y1 = [4, 5, 5, 7, 2]
y2 = [2, 3, 4, 5, 6]
# create a new plot
p = figure(title="Legend example")
# add circle renderer with legend_label arguments
line = p.line(x, y1, legend_label="Temp.", line_color="blue", line_width=2)
circle = p.scatter(
x,
y2,
marker="circle",
size=80,
legend_label="Objects",
fill_color="red",
fill_alpha=0.5,
line_color="blue",
)
# display legend in top left corner (default is top right corner)
p.legend.location = "top_left"
# add a title to your legend
p.legend.title = "Obervations"
# change appearance of legend text
p.legend.label_text_font = "times"
p.legend.label_text_font_style = "italic"
p.legend.label_text_color = "navy"
# change border and background of legend
p.legend.border_line_width = 3
p.legend.border_line_color = "navy"
p.legend.border_line_alpha = 0.8
p.legend.background_fill_color = "navy"
p.legend.background_fill_alpha = 0.2
# show the results
show(p)
自定义标题#
到目前为止,大多数示例都包含了一个标题。您通过将title参数传递给figure()函数来实现这一点:
p = figure(title="Headline example")
有多种方式可以为您的标题设置文本样式。例如:
from bokeh.plotting import figure, show
# prepare some data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]
# create new plot
p = figure(title="Headline example")
# add line renderer with a legend
p.line(x, y, legend_label="Temp.", line_width=2)
# change headline location to the left
p.title_location = "left"
# change headline text
p.title.text = "Changing headline text example"
# style the headline
p.title.text_font_size = "25px"
p.title.align = "right"
p.title.background_fill_color = "darkgrey"
p.title.text_color = "white"
# show the results
show(p)
另请参阅
有关如何使用title的更多信息,请参阅用户指南中的
Titles。在参考指南中,
Title的条目包含所有可用属性的列表。
使用注解#
注释是您添加到图表中的视觉元素,以便更容易阅读。有关各种注释的更多信息,请参阅用户指南中的Annotations。
一个例子是框注释。你可以使用框注释来突出显示图表的某些区域:
要在你的图表中添加框注释,首先需要从bokeh.models导入BoxAnnotation类:
from bokeh.models import BoxAnnotation
接下来,创建BoxAnnotation对象。如果你没有为bottom或top传递值,Bokeh会自动将盒子的尺寸扩展到图表的边缘:
low_box = BoxAnnotation(top=20, fill_alpha=0.2, fill_color="#F0E442")
mid_box = BoxAnnotation(bottom=20, top=80, fill_alpha=0.2, fill_color="#009E73")
high_box = BoxAnnotation(bottom=80, fill_alpha=0.2, fill_color="#F0E442")
最后,您需要将BoxAnnotation对象添加到您现有的图表中。
使用add_layout()方法来添加您的框:
p.add_layout(low_box)
p.add_layout(mid_box)
p.add_layout(high_box)
这是完成后的代码的样子:
import random
from bokeh.models import BoxAnnotation
from bokeh.plotting import figure, show
# generate some data (1-50 for x, random values for y)
x = list(range(0, 51))
y = random.sample(range(0, 100), 51)
# create new plot
p = figure(title="Box annotation example")
# add line renderer
line = p.line(x, y, line_color="#000000", line_width=2)
# add box annotations
low_box = BoxAnnotation(top=20, fill_alpha=0.2, fill_color="#F0E442")
mid_box = BoxAnnotation(bottom=20, top=80, fill_alpha=0.2, fill_color="#009E73")
high_box = BoxAnnotation(bottom=80, fill_alpha=0.2, fill_color="#F0E442")
# add boxes to existing figure
p.add_layout(low_box)
p.add_layout(mid_box)
p.add_layout(high_box)
# show the results
show(p)
另请参阅
要了解更多关于Bokeh中不同类型的注释,请参阅用户指南中的 Annotations。