简体   繁体   English

具有日期时间轴的bokeh悬停多行

[英]bokeh hover multiline with datetime axis

I want to create a multiline Bokeh plot with datetime axis and a hover tool that shows the datetime of the data point. 我想用日期时间轴和显示数据点日期时间的悬停工具创建多线散景图。 This should be supported and I have tried to obtain the intended behaviour in two ways: 这应该得到支持,并且我尝试通过两种方式来获得预期的行为:

  • Use hover.formatters to format the x-value. 使用hover.formatters格式化x值。 This has no effect on the plot. 这对情节没有影响。
  • Add a description variable with the correctly formatted date/time values. 添加具有正确格式的日期/时间值的描述变量。 This results in a hover tool where all date/time values are displayed in a list for each point. 这将产生一个悬停工具,其中所有日期/时间值都显示在每个点的列表中。

I have included a smaller example of my code that illustrates my approach and the result. 我提供了一个较小的代码示例,用于说明我的方法和结果。 It is used in conjunction with a checkboxgroup that updates the data. 它与更新数据的复选框组结合使用。 This is why a new ColumnDataSource is made from the dataframe. 这就是为什么从数据框中创建新的ColumnDataSource的原因。

import pandas as pd
import numpy as np
from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.models import HoverTool, ColumnDataSource
from bokeh.palettes import Spectral4
from bokeh.layouts import column

#output_file("demo.html")

available_quant = ["LACTIC_ACID", "GLUCOSE", "XYLOSE", "FORMIC_ACID"]
quant_legend = ["Lactic acid", "Glucose", "Xylose", "Formic acid"]

Create a dataframe with 4 quantities and the time 创建一个具有4个数量和时间的数据框

datelist = pd.date_range(end = pd.datetime.today(), periods=100).tolist()
desc = datelist
for i, date in enumerate(datelist):
    desc[i] = str(date)
RT_x = np.linspace(-5, 5, num=100)
lactic = RT_x**2
data = {'time': datelist, 'desc': desc, 'LACTIC_ACID': RT_x**2 + 2, 'GLUCOSE': RT_x**2, 'XYLOSE': RT_x**2 - 2, 'FORMIC_ACID': RT_x**2 - 4}
df = pd.DataFrame.from_dict(data)
df['time'] = pd.to_datetime(df['time'], format = "%Y-%m-%d %H:%M:%S")

Copy the relevant data to a columndatasource 将相关数据复制到columndatasource

substance_colors = Spectral4
quant_to_plot = available_quant
xs = []
ys = []
xsprint = []
colors = []
labels = []

for i, substance in enumerate(quant_to_plot):
    xs.append(list(df['time']))
    ys.append(list(df[substance]))
    xsprint.append(list(df['desc']))
    index = available_quant.index(substance)
    colors.append(substance_colors[index])
    labels.append(quant_legend[index])

new_src = ColumnDataSource(data={'x': xs, 'y': ys, 'desc': xsprint, 'color': colors, 'label': labels})

Make the first plot using hover.formatters 使用hover.formatters制作第一个图

p = figure(plot_width=800, plot_height=400, x_axis_type="datetime", title = 'Demo', x_axis_label = 'Time', y_axis_label = 'c [g/mL]')

p.multi_line('x','y', color = 'color', legend = 'label', source = new_src)

hover = HoverTool(tooltips=[('Type','@label'), 
                            ('Time','$x'), 
                            ('Conc','$y')], 
                  formatters={'Time': 'datetime'}, 
                  mode = 'mouse',
                 line_policy='next')
p.add_tools(hover)
p.legend.location = "top_left"
p.legend.orientation = "horizontal"

Make second plot using description variable 使用描述变量制作第二张图

p2 = figure(plot_width=800, plot_height=400, x_axis_type="datetime", title = 'Demo', x_axis_label = 'Time', y_axis_label = 'c [g/mL]')

p2.multi_line('x','y', color = 'color', legend = 'label', source = new_src)

hover = HoverTool(tooltips=[('Type','@label'), 
                            ('Time','@desc'), 
                            ('Conc','$y')], 
                  mode = 'mouse',
                 line_policy='nearest')
p2.add_tools(hover) 

mylayout = column(p, p2)
show(mylayout)

Am I missing something trivial? 我错过了一些琐碎的事情吗? I am running Bokeh 0.13.0 and python 3.6.4. 我正在运行Bokeh 0.13.0和python 3.6.4。

The first approach works with the following modification of the hovertool: 第一种方法适用于hovertool的以下修改:

hover = HoverTool(tooltips=[('Type','@label'), 
                        ('Time','$x{%F}'), 
                        ('Conc','$y')], 
              formatters={'$x': 'datetime'},
              mode = 'mouse',
             line_policy='nearest')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM