使用datetime作为x轴时,如何使bokeh忽略缺少的日期 [英] How do I make bokeh omit missing dates when using datetime as x-axis

查看:167
本文介绍了使用datetime作为x轴时,如何使bokeh忽略缺少的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看bokeh文档中的烛台示例,位于此处:

I am looking at the candlestick example in the bokeh docs, found here:

https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/candlestick.py

,我正在努力寻找一种消除x轴上没有数据的空格"的好方法.

and I am trying to figure out a good way to eliminate the "spaces" in the x-axis where there is no data.

具体地说,对于示例中使用的MSFT这样的财务数据,没有周末和节假日的数据.当没有日期数据时,是否有办法告诉bokeh不要在图表中留出空白?

Specifically, for financial data like MSFT used in the example, there is no data for weekends and holidays. Is there a way to tell bokeh not to leave an empty space in the chart when there is no data for a date?

为方便起见,以下是在上述链接中找到的示例代码的粘贴:

Here is a paste of the example code found at the above link for convenience:

from math import pi
import pandas as pd

from bokeh.sampledata.stocks import MSFT
from bokeh.plotting import *

df = pd.DataFrame(MSFT)[:50]
df['date'] = pd.to_datetime(df['date'])

mids = (df.open + df.close)/2
spans = abs(df.close-df.open)

inc = df.close > df.open
dec = df.open > df.close
w = 12*60*60*1000 # half day in ms

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

figure(x_axis_type = "datetime", tools="pan,wheel_zoom,box_zoom,reset,previewsave",
   width=1000, name="candlestick")

hold()

segment(df.date, df.high, df.date, df.low, color='black')
rect(df.date[inc], mids[inc], w, spans[inc], fill_color="#D5E1DD", line_color="black")
rect(df.date[dec], mids[dec], w, spans[dec], fill_color="#F2583E", line_color="black")

curplot().title = "MSFT Candlestick"
xaxis().major_label_orientation = pi/4
grid().grid_line_alpha=0.3

show()  # open a browser

推荐答案

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

UPDATE: As of Bokeh 0.12.6 you can specify overrides for major tick labels on axes.

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)

如果您有很多日期,则此方法可能会变得很笨拙,并且

If you have a very large number of dates, this approach might become unwieldy, and a Custom Extension might become necessary.

这篇关于使用datetime作为x轴时,如何使bokeh忽略缺少的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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