您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用datetime作为x轴时,如何使bokeh忽略缺少的日期

使用datetime作为x轴时,如何使bokeh忽略缺少的日期

更新:从散景开始,0.12.6您可以为轴上的主要刻度标签指定替代。

import pandas as pd

from bokeh.io import show, output_file
from bokeh.plotting import figure
from bokeh.sampledata.stocks import MSFT

df = pd.DataFrame(MSFT)[:50]
inc = df.close > df.open
dec = df.open > df.close

p = figure(plot_width=1000, title="MSFT Candlestick with Custom X-Axis")

# map dataframe indices to date strings and use as label overrides
p.xaxis.major_label_overrides = {
    i: date.strftime('%b %d') for i, date in enumerate(pd.to_datetime(df["date"]))
}

# use the *indices* for x-axis coordinates, overrides will print better labels
p.segment(df.index, df.high, df.index, df.low, color="black")
p.vbar(df.index[inc], 0.5, df.open[inc], df.close[inc], fill_color="#D5E1DD", line_color="black")
p.vbar(df.index[dec], 0.5, df.open[dec], df.close[dec], fill_color="#F2583E", line_color="black")

output_file("custom_datetime_axis.html", title="custom_datetime_axis.py example")

show(p)

在此处输入图片说明

如果您有很多日期,则此方法可能会变得笨拙,并且可能需要自定义扩展

其他 2022/1/1 18:28:22 有451人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶