python - Bokeh Server:点击 Scatter 以在条形图中显示其他数据
问题描述
我有一个 DataFrame,它包含我想在散点图中绘制的数据。DataFrame 包含的信息远远多于散点图 x 和 y 数据所需的列。
我想将附加数据显示为悬停(这不是问题),但如果我在散点图中点击选择一个数据点,则 ColumnDataSource 其他列中的附加数据应在条形图中绘制。
我的主要问题是让条形图接受存储在 ColumnDataSource 的一个选定行中的数据。我所看到的一切都使用基于列的数据将其提供给条形图。
我已经完成了一个解决方法的中途,我使用 ColumnDatasource 的选定行将其转换回 DataFrame,然后将其转置(因此它是基于列的),然后返回到 ColumnDataSource 但这不是创建者的意图散景,对吧?
我将我的问题简化为一个简约的代码片段:
df = pd.DataFrame({"x": [1,2,3,4,5,6],
"y": [6,5,4,3,2,1],
"cat1": [11,12,13,14,15,16],
"cat2": [100,99,98,97,96,95]})
SRC = ColumnDataSource(df)
def Plot(doc):
def callback(event):
SELECTED = SRC.selected.indices
bplot = make_bPlot(SELECTED)
def make_bPlot(selected):
#Here is my question:
#How to feed the row-wise data of the SRC to the barplot?
b = figure(x_range=["cat1", "cat2"])
b.vbar(x=["cat1", "cat2"], top=["cat1", "cat2"], source=SRC)
return b
TOOLTIPS = [
("x", "@x"),
("y", "@y"),
("Category 1", "@cat1"),
("Category 2", "@cat2")]
TOOLS="pan,wheel_zoom,zoom_in,zoom_out,box_zoom,reset,tap"
cplot = figure(tools = TOOLS, tooltips=TOOLTIPS)
cplot.circle("x", "y", source=SRC)
bplot = make_bPlot(None) # init
taptool = plot.select(type=TapTool)
cplot.on_event(Tap, callback)
layout = column(cplot, bplot)
doc.add_root(layout)
提前致谢。
解决方案
我从 Bokeh Discourse 论坛得到了答案: https ://discourse.bokeh.org/t/tap-on-scatter-to-show-additional-data-in-bar-plot/6939
推荐阅读
- python - 如何使 y 轴上的数字显示以百万为单位的值,而不是 matplotlib 中的科学计数法?
- rust - 跟踪工厂及其产品之间所有权的惯用方式是什么?
- ubuntu - Ubuntu 为未连接的 WiFi 适配器应用网络计划
- hive - 如何将 Google Cloud Dataproc 上的 Hive 数据库连接到 Tableau Online,Tableau Bridge 是否有助于实时连接?
- git - 替代 git lfs
- javascript - NestJs 中的子路由
- bash - 使用 .tarignore 文件从 tar 中排除目录 (#2)
- elasticsearch - Elasticsearch 嵌套匹配查询不返回任何结果
- pytorch - 如何解决 RuntimeError: cuda 运行时错误 (59) : 设备端断言触发错误
- java - 在使用 Java 的 DynamoDB 扫描中使用包含过滤器 - android