Bokeh - 初學者指南



在兩個 numpy 陣列之間建立簡單的折線圖非常簡單。首先,從 bokeh.plotting 模組匯入以下函式 -

from bokeh.plotting import figure, output_file, show

figure() 函式建立一個新的圖形用於繪圖。

output_file() 函式用於指定一個 HTML 檔案來儲存輸出。

show() 函式在瀏覽器或筆記型電腦中顯示 Bokeh 圖形。

接下來,設定兩個 numpy 陣列,其中第二個陣列是第一個陣列的正弦值。

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

要獲取一個 Bokeh 圖形物件,請指定標題以及 x 和 y 軸標籤,如下所示 -

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

圖形物件包含一個 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)

瀏覽器輸出

Create model
廣告
© . All rights reserved.