Bokeh自动填充日期时间轴缺少值:如何停止? [英] Bokeh autofill datetime axis missing values: how to stop it?

查看:18
本文介绍了Bokeh自动填充日期时间轴缺少值:如何停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从pandas dataframe财务数据构建交互图。数据帧指数在休市时或周末有一些缺口。问题是,当我使用DataFrame索引作为XASIS时,Bokeh总是自动填充这些空白,这让它看起来很难看。

如何才能无间隙地打印数据?

推荐答案

这里有一个来自repo的示例,它绘制了没有间隙的烛台。您必须使用点的整数索引作为x轴,然后使用自定义刻度格式化工具或刻度覆盖将这些日期显示为正确的日期:

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)[:51]
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"]))
}
p.xaxis.bounds = (0, df.index[-1])
p.x_range.range_padding = 0.05

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)

这篇关于Bokeh自动填充日期时间轴缺少值:如何停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆