[英]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.