亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法將 Bokeh 中的 CrossHairTool 鏈接到多個圖

無法將 Bokeh 中的 CrossHairTool 鏈接到多個圖

慕蓋茨4494581 2023-06-20 10:45:26
在我的實現中,十字準線僅在懸停的情節中顯示。我目前正在使用 Bokeh 版本2.1.1和 Python Anaconda 版本3.7.6,使用 VSCode 版本 1.48 中的 Python 擴展。我不熟悉 Javascript,因此歡迎任何有助于調試我的代碼以正確顯示跨兩個圖的十字準線的幫助。我的代碼:# Importing libraries:import pandas as pdimport randomfrom datetime import datetime, timedeltafrom bokeh.models import CustomJS, CrosshairTool, ColumnDataSource, DatetimeTickFormatter, HoverToolfrom bokeh.layouts import gridplotfrom bokeh.plotting import figure, output_file, show# Function wrote by Hamid Fadishei to enable a linked crosshair within gridplot:def add_vlinked_crosshairs(figs):? ? js_leave = ''? ? js_move = 'if(cb_obj.x >= fig.x_range.start && cb_obj.x <= fig.x_range.end &&\n'? ? js_move += 'cb_obj.y >= fig.y_range.start && cb_obj.y <= fig.y_range.end){\n'? ? for i in range(len(figs)-1):? ? ? ? js_move += '\t\t\tother%d.spans.height.computed_location = cb_obj.sx\n' % i? ? js_move += '}else{\n'? ? for i in range(len(figs)-1):? ? ? ? js_move += '\t\t\tother%d.spans.height.computed_location = null\n' % i? ? ? ? js_leave += '\t\t\tother%d.spans.height.computed_location = null\n' % i? ? js_move += '}'? ? crosses = [CrosshairTool() for fig in figs]? ? for i, fig in enumerate(figs):? ? ? ? fig.add_tools(crosses[i])? ? ? ? args = {'fig': fig}? ? ? ? k = 0? ? ? ? for j in range(len(figs)):? ? ? ? ? ? if i != j:? ? ? ? ? ? ? ? args['other%d'%k] = crosses[j]? ? ? ? ? ? ? ? k += 1? ? ? ? fig.js_on_event('mousemove', CustomJS(args=args, code=js_move))? ? ? ? fig.js_on_event('mouseleave', CustomJS(args=args, code=js_leave))
查看完整描述

1 回答

?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

這是一個適用于 Bokeh 2.2.1 的解決方案:只需對所有需要它鏈接的圖使用相同的十字準線工具對象。像這樣:

import numpy as np

from bokeh.plotting import figure, show

from bokeh.layouts import gridplot

from bokeh.models import CrosshairTool


plots = [figure() for i in range(6)]

[plot.line(np.arange(10), np.random.random(10)) for plot in plots]


linked_crosshair = CrosshairTool(dimensions="both")


for plot in plots:

? ? plot.add_tools(linked_crosshair)


show(gridplot(children=[plot for plot in plots], ncols=3))


查看完整回答
反對 回復 2023-06-20
  • 1 回答
  • 0 關注
  • 224 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號