基本上,这是一个交互式的热图,但不同之处在于,通过从定期更新的文件中读取值来更新源。
不要为类“ generator”而烦恼,它只是为了保留数据并且定期运行线程
在执行bokeh脚本之前,请确保脚本的同一目录中存在一个名为“ Server_dump.txt”的文件,并且文件内部的数字大于0。
基本上发生的是,我在bash上使用echo 4 > Server_dump.txt
来更改名为“ Server_dump.txt”的文件中的数字,
您可以输入4
以外的任何数字,脚本会自动检查文件并绘制新点。
如果您不使用bash,则可以使用文本编辑器,替换数字并保存,所有内容都相同。
生成器类内部的run函数是检查该文件是否被修改,读取数字,将其转换为x&y坐标并增加与这些坐标相关联的抽头数量并给出source
x的函数。 ,y,根据该数字点击值。
该函数正常工作,每次我回显一个数字时,都会绘制出正确的矩形,但是
*如果我通过多次将数字写入文件来增加某个矩形的self.taps,颜色会更新,但是如果我将鼠标悬停在rect上,它将显示过去的值,而不仅仅是我的最新值。
我的bokeh版本是1.0.4
from functools import partial
from random import random,randint
import threading
import time
from tornado import gen
from os.path import getmtime
from math import pi
import pandas as pd
from random import randint, random
from bokeh.io import show
from bokeh.models import LinearColorMapper, BasicTicker, widgets, PrintfTickFormatter, ColorBar, ColumnDataSource, FactorRange
from bokeh.plotting import figure, curdoc
from bokeh.layouts import row, column, gridplot
source = ColumnDataSource(data=dict(x=[], y=[], taps=[]))
doc = curdoc()
#sloppy data receiving function to change data to a plottable shape
class generator(threading.Thread):
def __init__(self):
super(generator, self).__init__()
self.chart_coords = {'x':[],'y':[],'taps':[]}
self.Pi_coords = {}
self.coord = 0
self.pos = 0
self.col = 0
self.row = 0
self.s = 0
self.t = 0
def chart_dict_gen(self,row, col):
self.col = col
self.row = row+1
self.chart_coords['x'] = [i for i in range(1,cla.row)]
self.chart_coords['y'] = [i for i in range(cla.col, 0, -1)] #reversed list because chart requires that
self.chart_coords['taps']= [0]*(row * col)
self.taps = [[0 for y in range(col)] for x in range(row)]
def Pi_dict_gen(self,row,col):
key = 1
for x in range(1,row):
for y in range(1,col):
self.Pi_coords[key] = (x,y)
key = key + 1
def Pi_to_chart(self,N):
x,y = self.Pi_coords[N][0], self.Pi_coords[N][1]
return x,y
def run(self):
while True:
if(self.t == 0):
self.t=1
continue
time.sleep(0.1)
h = getmtime("Server_dump.txt")
if self.s != h:
self.s = h
with open('Server_dump.txt') as f:
m = next(f)
y,x = self.Pi_to_chart(int(m))
self.taps[x][y] += 1
# but update the document from callback
doc.add_next_tick_callback(partial(update, x=x, y=y, taps=self.taps[x][y]))
cla = generator()
cla.chart_dict_gen(15,15)
cla.Pi_dict_gen(15, 15)
x = cla.chart_coords['x']
y = cla.chart_coords['y']
taps = cla.chart_coords['taps']
@gen.coroutine
def update(x, y, taps):
taps += taps
print(x,y,taps)
source.stream(dict(x=[x], y=[y], taps=[taps]))
colors = ["#CCEBFF","#B2E0FF","#99D6FF","#80CCFF","#66c2FF","#4DB8FF","#33ADFF","#19A3FF", "#0099FF", "#008AE6", "#007ACC","#006BB2", "#005C99", "#004C80", "#003D66", "#002E4C", "#001F33", "#000F1A", "#000000"]
mapper = LinearColorMapper(palette=colors, low= 0, high= 15) #low = min(cla.chart_coords['taps']) high = max(cla.chart_coords['taps'])
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
p = figure(title="Tou",
x_range=list(map(str,x)),
y_range=list(map(str,reversed(y))),
x_axis_location="above",
plot_width=900, plot_height=400,
tools=TOOLS, toolbar_location='below',
tooltips=[('coords', '@y @x'), ('taps', '@taps%')])
p.grid.grid_line_color = "#ffffff"
p.axis.axis_line_color = "#ef4723"
p.axis.major_tick_line_color = "#af0a36"
p.axis.major_label_text_font_size = "7pt"
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
p.rect(x="x", y="y",
width=0.9, height=0.9,
source=source,
fill_color={'field': 'taps', 'transform': mapper},
line_color = "#ffffff",
)
color_bar = ColorBar(color_mapper=mapper,
major_label_text_font_size="7pt",
ticker=BasicTicker(desired_num_ticks=len(colors)),
formatter=PrintfTickFormatter(format="%d%%"),
label_standoff=6, border_line_color=None, location=(0, 0))
curdoc().theme = 'dark_minimal'
def ck(attr, old, new):
print('here') #doesn't even print hi in the terminal if i click anywhere
source.selected.on_change('indices', ck)
p.add_layout(color_bar, 'right')
doc.add_root(p)
thread = cla
thread.start()
我什至想在终端上打个喜字,但什么都没有
答案 0 :(得分:1)
您实际上并没有在绘图中添加任何选择工具,因此永远不会进行选择。您已指定:
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
只有这些工具会被添加,并且它们都没有做出选择,没有任何东西会导致source.selection.indices
被更新。如果您要根据点击来寻找选择,则必须添加TapTool
,例如与
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom,tap"
请注意,如果您多次点击相同的矩形,将不会重复出现回调。仅当选择更改并连续两次单击同一字形导致选择相同时,才会触发回调。