我正在使用Bokeh,DataShader和HoloViews为大数据实现ScatterPlot。 ScatterPlot部分本身已经完成,但有一个要求我遇到了问题:我需要能够捕获用户双击的数据坐标。
我在Jupyter笔记本中发现了一些对我有用的代码,但出于某种原因,在迁移到Python和Bokeh Server时,它没有。
以下是代码:
from os.path import dirname, join
import csv
import holoviews as hv
import datashader as ds
import pandas as pd
#import parambokeh
from holoviews.operation.datashader import aggregate, shade, datashade, dynspread
from holoviews.operation import decimate
from holoviews.streams import RangeXY
from holoviews import streams
from bokeh.plotting import curdoc
hv.extension('bokeh')
class MyScatterPlotBokeh:
#
# PARAMETERS:
# - keyFieldName : Field that uniquely identify a row
# - xFieldName : Field Name used for x-Axis
# - yFieldName : Field Name used for y-Axis
# - colorFieldName : Field Name used to select circle colors
# - otherFieldNames : Comma separated value variable with the name of all fields we want to show data
#
def __init__(self, filePath, title, screenWidth, screenHeight, keyFieldName, xFieldName, yFieldName, colorFieldName, otherFieldNames):
self._screenHeight = screenHeight
self._screenWidth = screenWidth
# Read data from file
df = pd.read_csv(filePath)
df[colorFieldName]=df[colorFieldName].astype("category")
# Creating graphic
hover_opts = hv.opts("QuadMesh [tools=['hover']] (alpha=0 hover_alpha=0.2)")
self._points = hv.Points(df,kdims=[xFieldName,yFieldName])
self._dynamic_hover = datashade(self._points) * dynspread(datashade(self._points, aggregator=ds.count_cat(colorFieldName))) * \
hv.util.Dynamic(aggregate(self._points, width=50, height=50, streams=[RangeXY]),operation=hv.QuadMesh)
# Interaction - Point selection
double_tap = streams.DoubleTap(transient=True, source=self._points)
self._countTap = 0
self._taps = []
def record_taps(x,y):
print "clicked"
self._countTap += 1
if self._countTap>2:
self._countTap = 0
if None not in [x,y]:
if self._countTap == 1:
self._taps.insert(0,(x, y))
else:
self._taps.append((x, y))
print "TAPS LOOK LIKE " + str(self._taps)
return hv.Points(self._taps, vdims='Taps')
self._finalPlot = self._dynamic_hover + hv.DynamicMap(record_taps, streams=[double_tap])
def draw(self):
hv.renderer('bokeh').server_doc(self._finalPlot)
一旦服务器执行,我就会看到“点击”消息显示,但从未在浏览器上双击之后。就像双击事件没有响应。 最后我需要发送点击的X& Y坐标到浏览器,我认为我可以通过从python代码创建一个输入框来实现,但我还没有。
任何帮助将不胜感激。
答案 0 :(得分:1)
使用版本1.9.2解决了 philippjfr
指出的问题我怀疑这是最近解决的错误(请参阅github.com/ioam/holoviews/pull/2239)。你介意尝试HoloViews大师吗?我们将尝试尽快发布v1.9.3错误修复版本。 - philippjfr