第一步 1:创建折线图#
仅需几行 Python 代码,Bokeh 即可让您创建可在 Web 浏览器中显示的交互式、JavaScript 驱动的可视化效果。
Bokeh 的基本思想是一个两步过程:首先,您从 Bokeh 的构建块中选择来创建您的可视化效果。其次,您自定义这些构建块以满足您的需求。
为此,Bokeh 结合了两个要素
一个 Python 库,用于定义您的可视化效果的内容和交互功能。
一个名为 BokehJS 的 JavaScript 库,在后台工作以在 Web 浏览器中显示您的交互式可视化效果。
基于您的 Python 代码,Bokeh 会自动为您生成所有必要的 JavaScript 和 HTML 代码。在其默认设置中,Bokeh 会自动从 Bokeh 的 CDN(内容分发网络)加载任何额外的 JavaScript 代码。
Bokeh 的文档包含多个部分,包括用户指南(包含详细的解释和示例)以及参考指南(系统地描述了 Bokeh 的每个要素)。在本指南中,您将找到指向这两种资源的链接。
注意
“第一步”部分中的所有代码都可以作为标准 Python 脚本运行。运行时,这些脚本将创建可在 Web 浏览器中查看的 HTML 输出。
创建简单的折线图#
您的第一个可视化效果将是一个带有单条线的绘图,如下所示
即使是像这样的简单图表也具有交互功能。使用绘图右侧的工具进行探索
按照以下步骤重新创建这个简单的折线图
在您的机器上创建一个新的 Python 文件(例如
simple_line_chart.py
),并在您选择的代码编辑器(例如 Sublime Text、Visual Studio Code 等)中打开它。
作为新的 Python 脚本的第一行,从bokeh.plotting模块导入必要的函数
from bokeh.plotting import figure, show
定义两个列表,其中包含折线图的数据
# prepare some data x = [1, 2, 3, 4, 5] y = [6, 7, 2, 4, 5]
使用
figure()
函数创建您的绘图。传递以下参数title
:折线图的标题(可选)x_axis_label
:放置在图表 x 轴上的文本标签(可选)y_axis_label
:放置在图表 y 轴上的文本标签(可选)
# create a new plot with a title and axis labels p = figure(title="Simple line example", x_axis_label='x', y_axis_label='y')
使用
line()
函数,将折线图添加到您刚刚创建的绘图中。传递以下参数您的列表
x
和y
,其中包含数据legend_label
:用于标记折线图的字符串(可选)line_width
:定义线宽(以像素为单位,可选)
# add a line renderer with legend and line thickness to the plot p.line(x, y, legend_label="Temp.", line_width=2)
最后,使用
show()
函数生成您的图形并打开 Web 浏览器以显示生成的 HTML 文件。# show the results show(p)
从命令行,运行您刚刚创建的 Python 脚本。例如
python simple_line_chart.py
当您执行这些代码行时,Bokeh 会创建一个输出文件 "lines.html"
。Bokeh 还会打开一个浏览器来显示它。
这是您的折线图的完整代码应如下所示
from bokeh.plotting import figure, show
# prepare some data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]
# create a new plot with a title and axis labels
p = figure(title="Simple line example", x_axis_label="x", y_axis_label="y")
# add a line renderer with legend and line thickness
p.line(x, y, legend_label="Temp.", line_width=2)
# show the results
show(p)
组合多个图形#
使用 Bokeh 的bokeh.plotting接口,您可以向绘图添加更多字形
要向绘图添加更多折线图,您只需多次调用line()
函数。
首先,添加更多数据作为附加图形的基础
# 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]
接下来,通过更改figure()
函数中 title
参数的字符串来更新绘图的标题
# create a new plot with a title and axis labels
p = figure(title="Multiple line example", x_axis_label='x', y_axis_label='y')
最后,向line()
函数添加更多调用
# add multiple renderers
p.line(x, y1, legend_label="Temp.", color="blue", line_width=2)
p.line(x, y2, legend_label="Rate", color="red", line_width=2)
p.line(x, y3, legend_label="Objects", color="green", line_width=2)
在此示例中,您还可以通过将不同的命名颜色传递给每条线的 color
参数,为每条线分配不同的颜色。
这是您的多线绘图的完整代码应如下所示
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 line 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.line(x, y2, legend_label="Rate", color="red", line_width=2)
p.line(x, y3, legend_label="Objects", color="green", line_width=2)
# show the results
show(p)
回顾:构建可视化效果#
您刚刚完成了使用 Bokeh 的bokeh.plotting接口构建大多数基本可视化效果的所有基本步骤