javascript - 在 Bokeh 交互式线图中从 ColumnDataSource 选择行

标签 javascript python bokeh

我一直在尝试选择要在简单的 Bokeh 线图中绘制的行。 所需的结果是一个简单的线图,其中 x 轴为 Date,y 轴为 Value。 使用 2 个选择小部件,我想选择 CountryType

非常欢迎任何建议!

到目前为止我的代码:

import pandas as pd
import numpy as np
from bokeh.models.widgets import Select
from bokeh.models import ColumnDataSource, Select, CDSView, GroupFilter
from bokeh.io import show, output_notebook
from bokeh.plotting import figure

output_notebook()

# base
df = pd.DataFrame({'Country': ['A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'B'],
                   'Date': ['01-01-2020', '01-02-2020', '01-03-2020', '01-01-2020', '01-02-2020', '01-03-2020', '01-01-2020', '01-02-2020', '01-03-2020', '01-01-2020', '01-02-2020', '01-03-2020'],
                   'Type': ['X', 'X', 'X', 'Y', 'Y', 'Y', 'X', 'X', 'X', 'Y', 'Y', 'Y'],
                   'Value': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]})
df['Date'] = pd.to_datetime(df['Date'])

source = ColumnDataSource(df)

country_filter = CDSView(source=source, filters=[GroupFilter(column_name='Country', group='A')])
type_filter = CDSView(source=source, filters=[GroupFilter(column_name='Type', group='X')])

country_select = Select(title="Country:", value="A", options=np.unique(source.data['Country']).tolist())
country_select.js_link('value', country_filter, 'group')

type_select = Select(title="Type:", value="X", options=np.unique(source.data['Type']).tolist())
type_select.js_link('value', type_filter, 'group')

p = figure()
p.line(x='Date', y='Value', source=source, view=view)

layout = row(p, column(country_select, type_select))

show(layout)

最佳答案

你快明白了。这是一个工作版本。有两点需要注意:

  1. 我必须手动将组过滤器的更改链接到 View 的 change 信号 - 目前 Bokeh 不会在内部建立该链接
  2. 由于您使用的是线条,因此此代码将生成有关使用带有连接拓扑的字形的过滤器的警告。在您的情况下可以安全地忽略它
import numpy as np
import pandas as pd
from bokeh.io import show
from bokeh.layouts import row, column
from bokeh.models import ColumnDataSource, Select, CDSView, GroupFilter, CustomJS
from bokeh.plotting import figure

df = pd.DataFrame({'Country': ['A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'B'],
                   'Date': ['01-01-2020', '01-02-2020', '01-03-2020', '01-01-2020', '01-02-2020', '01-03-2020',
                            '01-01-2020', '01-02-2020', '01-03-2020', '01-01-2020', '01-02-2020', '01-03-2020'],
                   'Type': ['X', 'X', 'X', 'Y', 'Y', 'Y', 'X', 'X', 'X', 'Y', 'Y', 'Y'],
                   'Value': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]})
df['Date'] = pd.to_datetime(df['Date'])

source = ColumnDataSource(df)

country_filter = GroupFilter(column_name='Country', group='A')
type_filter = GroupFilter(column_name='Type', group='X')
view = CDSView(source=source, filters=[country_filter, type_filter])

# Alas, we need this manual step to make the view know about the filters' changes.
for f in view.filters:
    f.js_on_change('group', CustomJS(args=dict(view=view),
                                     code="view.properties.filters.change.emit();"))

country_select = Select(title="Country:", value="A", options=np.unique(source.data['Country']).tolist())
country_select.js_link('value', country_filter, 'group')

type_select = Select(title="Type:", value="X", options=np.unique(source.data['Type']).tolist())
type_select.js_link('value', type_filter, 'group')

p = figure()
p.line(x='Date', y='Value', source=source, view=view)

show(row(p, column(country_select, type_select)))

关于javascript - 在 Bokeh 交互式线图中从 ColumnDataSource 选择行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60776849/

相关文章:

python - 为什么我的 Bokeh 图在 github 上不起作用?

javascript - 日/夜模式 - CSS + JQuery - Cookies?

javascript - Bokeh slider 堆叠条形图

javascript - DC.js 范围/焦点图表行为不正确

python - 如何在 PyTorch 中初始化权重?

php - 在 Trac 中设置身份验证

python - 如何更快地实现贪心集覆盖?

python - 没有名为对象的模块 [背景虚化]

javascript - 带有 ui-router 的可重用子状态

javascript - 如何验证电子邮件地址在java脚本中只允许两个域