Bokeh 入门
在两个 numpy 数组之间创建一个简单的线图非常简单。首先,从 Bokeh 绘图 modules:
from bokeh.plotting import figure, output_file, show
The figure() 函数创建一个用于绘图的新图形。
The 输出文件() 函数用于指定一个 HTML 文件来存储输出。
The show() 功能在笔记本上的浏览器中显示Bokeh 图。
接下来,设置两个 numpy 数组,其中第二个数组是第一个的正弦值。
import numpy as np import math x = np.arange(0, math.pi*2, 0.05) y = np.sin(x)
要获取 Bokeh Figure 对象,请指定标题以及 x 和 y 轴标签,如下所示:
p = figure(title = "sine wave example", x_axis_label = 'x', y_axis_label = 'y')
Figure 对象包含一个 line() 方法,该方法将线条字形添加到图形。它需要 x 和 y 轴的数据系列。
p.line(x, y, legend = "sine", line_width = 2)
最后,设置输出文件并调用 show() 函数。
output_file("sine.html") show(p)
这将在“sine.html”中呈现线图,并将显示在浏览器中。
完整代码及其输出如下
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)