Bokeh 简明教程

Bokeh - Getting Started

在两个 numpy 数组之间创建简单的折线图非常简单。首先,从 bokeh.plotting 模块导入以下函数 -

Creating a simple line plot between two numpy arrays is very simple. To begin with, import following functions from bokeh.plotting modules −

from bokeh.plotting import figure, output_file, show

figure() 函数创建了用于绘制的新图形。

The figure() function creates a new figure for plotting.

output_file() 函数用于指定来存储输出的 HTML 文件。

The output_file() function is used to specify a HTML file to store output.

show() 函数在笔记本的浏览器中显示 Bokeh 图形。

The show() function displays the Bokeh figure in browser on in notebook.

接下来,设置两个 numpy 数组,其中第二个数组是第一个数组的正弦值。

Next, set up two numpy arrays where second array is sine value of first.

import numpy as np
import math
x = np.arange(0, math.pi*2, 0.05)
y = np.sin(x)

要获取 Bokeh 图形对象,请指定标题和 x 和 y 轴标签,如下所示 −

To obtain a Bokeh Figure object, specify the title and x and y axis labels as below −

p = figure(title = "sine wave example", x_axis_label = 'x', y_axis_label = 'y')

图形对象包含一个 line() 方法,该方法将线形图形添加到图形中。它需要 x 和 y 轴的数据序列。

The Figure object contains a line() method that adds a line glyph to the figure. It needs data series for x and y axes.

p.line(x, y, legend = "sine", line_width = 2)

最后,设置输出文件并调用 show() 函数。

Finally, set the output file and call show() function.

output_file("sine.html")
show(p)

这将在 ‘sine.html’ 中渲染线形图表,并将其显示在浏览器中。

This will render the line plot in ‘sine.html’ and will be displayed in browser.

Complete code and its output is as follows

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)
output_file("sine.html")
p = figure(title = "sine wave example", x_axis_label = 'x', y_axis_label = 'y')
p.line(x, y, legend = "sine", line_width = 2)
show(p)

Output on browser

figure