我正在使用Bokeh、DataShader和HoloViews为大数据实现一个DataShader。ScatterPlot部分本身已经基本完成,但有一个要求我有问题:我需要能够捕获数据坐标,用户做双击。
我在木星笔记本中找到了一些适用于我的代码,但出于某种原因,在迁移到Python和Bokeh时,它没有。
以下是代码:
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代码创建一个输入框来完成这个任务,但我还没有。
任何帮助都将不胜感激。
发布于 2018-01-18 00:18:12
使用1.9.2版本修复了philippjfr指出的问题
我怀疑这是最近解决的一个bug (参见github.com/ioam/holoview/pull/2239)。你介意试试HoloViews大师吗?我们将尝试尽快发布v1.9.3错误修复版本。- philippjfr
https://stackoverflow.com/questions/48307737
复制相似问题