繁体   English   中英

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

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

尝试在我的数据集中的重要日期绘制垂直光线以突出显示该日期之前/之后的趋势。 我了解如何使用不在日期时间中的 x 轴绘制光线,但是当 x 处于日期时间时,我一直在努力使其工作。 这是我尝试过的:

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)

这会产生很长的堆栈跟踪,并出现以下错误:

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)

当 x 轴处于日期时间时,这是否根本不受支持,或者我错过了文档中的某些内容?

弄清楚了。 我最初为我的 x 轴传入了一个日期时间对象列表。 我用一个以毫秒为单位的时间戳列表替换了该列表,然后为调用日期获取了相应的以毫秒为单位的时间戳,并用它来定义光线。 修改后的代码:

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')

希望这可以帮助任何偶然发现这个问题的人。

使用此处指示的 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