繁体   English   中英

Bokeh自动填充日期时间轴缺失值:如何阻止它?

[英]Bokeh autofill datetime axis missing values: how to stop it?

我正在尝试从pandas dataframe财务数据数据pandas dataframe构建一个交互式图。 当市场关闭或周末时,数据框索引有一些差距。 问题是当我使用数据帧索引作为xasis时,Bokeh总是自动填充这些空白,使其视觉上难看。

日期时间轴中散景自动填充间隙的示例。

如何无间隙地打印数据?

如果在部分之间插入NaN,则Bokeh不会填补空白:

source = ColumnDataSource(data=dict(x=[1, 2, 3, np.nan, 10, 20], y=[1, 2, 3, np.nan, 3, 4]))
fig = figure(plot_height=250)
fig.line("x", "y", source=source)
show(fig)

输出:

在此输入图像描述

这是一个来自回购的例子,它绘制没有间隙的烛台。 您必须使用点的整数索引作为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)

暂无
暂无

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

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