简体   繁体   English

当 x 轴在日期时间时用散景绘制光线

[英]Draw a Ray with Bokeh when x axis is in datetime

Trying to draw a vertical ray at an important date in my dataset to highlight trends before/after that date.尝试在我的数据集中的重要日期绘制垂直光线以突出显示该日期之前/之后的趋势。 I understand how to draw the ray with an x-axis that isn't in datetimes, but I've been struggling with getting it to work when x is in datetime.我了解如何使用不在日期时间中的 x 轴绘制光线,但是当 x 处于日期时间时,我一直在努力使其工作。 Here's what I tried:这是我尝试过的:

from bokeh.plotting import figure, output_file, show
from datetime import datetime as dt
from math import pi

p = figure(title = 'stuff',
           x_axis = 'date',
           y_axis = datapoints,
           x_axis_type = "datetime",
           tools='pan, wheel_zoom, box_zoom, reset, resize, previewsave',
           plot_width=1000)
#dates and data are lists containing datetime objects and y values
p.line(dates, data, line_width=1)
p.xaxis.major_label_orientation = pi/4
p.ray(x = dt(year, month, day), y = 0, length = 0, angle_units = "deg", 
      angle = 90, color = 'black')
output_file('data.html')
show(p)

This yields a long stacktrace, with the following error:这会产生很长的堆栈跟踪,并出现以下错误:

ValueError: expected an element of either String, Dict(String, Either(String, Instance(Transform), Instance(ColorMapper), Float)) or Float, got datetime.datetime(2014, 9, 18, 0, 0)

Is this simply not supported when x axis is in datetimes, or have I missed something in documentation?当 x 轴处于日期时间时,这是否根本不受支持,或者我错过了文档中的某些内容?

Figured it out.弄清楚了。 I originally passed in a list of datetime objects for my x axis.我最初为我的 x 轴传入了一个日期时间对象列表。 I replaced that list with a list of timestamps in milliseconds, then got the corresponding timestamp in milliseconds for the call-out date and used that to define the ray.我用一个以毫秒为单位的时间戳列表替换了该列表,然后为调用日期获取了相应的以毫秒为单位的时间戳,并用它来定义光线。 Revised code:修改后的代码:

p.line(lst_of_epoch_times_ms, data, line_width=1)
p.xaxis.major_label_orientation = pi/4
p.ray(x = ms_timestamp_callout, y = 0, length = 0, angle_units = "deg",
      angle = 90, color = 'black')

Hope this helps anyone who stumbles on this question.希望这可以帮助任何偶然发现这个问题的人。

使用此处指示的 Span

Span annotations are lines that are orthogonal to the x or y axis of a plot. They have a single dimension (width or height) and go from one edge of the plot area to the opposite edge.

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

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