Bokeh - 列資料來源



Bokeh API 中的大多數繪圖方法都可以透過 ColumnDatasource 物件接收資料來源引數。這使得繪圖和“資料表”之間共享資料成為可能。

ColumnDatasource 可被視為列名和資料列表之間的對映關係。一個包含一個或多個字串鍵以及列表或 numpy 陣列作為值的 Python dict 物件傳遞給 ColumnDataSource 建構函式。

示例

如下示例

from bokeh.models import ColumnDataSource
data = {'x':[1, 4, 3, 2, 5],
   'y':[6, 5, 2, 4, 7]}
cds = ColumnDataSource(data = data)

然後使用此物件作為字元方法中 source 屬性的值。下面的程式碼使用 ColumnDataSource 生成了一個散點圖。

from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource
data = {'x':[1, 4, 3, 2, 5],
   'y':[6, 5, 2, 4, 7]}
cds = ColumnDataSource(data = data)
fig = figure()
fig.scatter(x = 'x', y = 'y',source = cds, marker = "circle", size = 20, fill_color = "grey")
show(fig)

輸出

ColumnDataSource

除了向 ColumnDataSource 指定 Python 字典之外,我們還可以使用 Pandas DataFrame。

讓我們使用“test.csv”(本節前面已使用過)來獲取一個 DataFrame,並使用它獲取 ColumnDataSource 和呈現折線圖。

from bokeh.plotting import figure, output_file, show
import pandas as pd
from bokeh.models import ColumnDataSource
df = pd.read_csv('test.csv')
cds = ColumnDataSource(df)
fig = figure(y_axis_type = 'log')
fig.line(x = 'x', y = 'pow',source = cds, line_color = "grey")
show(fig)

輸出

rendering
廣告
© . All rights reserved.