Bokeh 简明教程
Bokeh - ColumnDataSource
Bokeh API 中的大多数绘制图形方法都能够通过 ColumnDatasource 对象接收数据源参数。它可以在绘制图形和“数据表”之间共享数据。
Most of the plotting methods in Bokeh API are able to receive data source parameters through ColumnDatasource object. It makes sharing data between plots and ‘DataTables’.
可以将 ColumnDatasource 视为列名称和数据列表之间的映射。通过字符串键及列表或 numpy 数组作为值,将 Python dict 对象传递给 ColumnDataSource 构造函数。
A ColumnDatasource can be considered as a mapping between column name and list of data. A Python dict object with one or more string keys and lists or numpy arrays as values is passed to ColumnDataSource constructor.
Example
以下为示例
Below is the example
from bokeh.models import ColumnDataSource
data = {'x':[1, 4, 3, 2, 5],
'y':[6, 5, 2, 4, 7]}
cds = ColumnDataSource(data = data)
然后将此对象用作符号方法中 source 属性的值。以下代码使用 ColumnDataSource 生成散点图。
This object is then used as value of source property in a glyph method. Following code generates a scatter plot using 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)
Output

除了向 ColumnDataSource 分配 Python 字典之外,我们还可以为它使用 Pandas DataFrame。
Instead of assigning a Python dictionary to ColumnDataSource, we can use a Pandas DataFrame for it.
让我们使用“test.csv”(在本节的前面使用)来获取 DataFrame,并使用它来获取 ColumnDataSource 并呈现线形绘制图形。
Let us use ‘test.csv’ (used earlier in this section) to obtain a DataFrame and use it for getting ColumnDataSource and rendering line plot.
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)