使用bokeh绘制图形时,如何在x_axis类型为datetime的情况下删除缺失的日期, [英] when plotting a graph using bokeh, how to remove missing date while x_axis type is datetime ,

查看:139
本文介绍了使用bokeh绘制图形时,如何在x_axis类型为datetime的情况下删除缺失的日期,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在尝试使用bokeh绘制股票数据,要绘制的数据是熊猫的数据框,例如

I'm recently trying to plot stock data using bokeh, the data to plot is dataframe of pandas, like

         date  value
0  2017-01-01     10
1  2017-01-02     20
2  2017-01-03     15
3  2017-01-06     30
4  2017-01-07     25

由于周六和周日没有交易,因此此数据框中没有记录.这就是我画的图片

Since there are not trades on Saturday and Sunday, there are not records in this dataframe. So this is the image I drawed

是否可以删除两个空格键? 我的代码在这里:

Is there any way to remove the two space bar? My codes here:

import pandas as pd
from bokeh.plotting import figure
from bokeh.io import show


df = pd.DataFrame({"date":['2017-01-01','2017-01-02','2017-01-03','2017-01-06','2017-01-07']})
df["value"] = [10,20,15,30,25]
print(df)
width = 24*60*60*100
TOOLS = 'hover,crosshair,pan,wheel_zoom,box_zoom,reset,save,box_select'
picture = figure(width=1000, height=400, tools=TOOLS, x_axis_type='datetime')
picture.vbar(pd.to_datetime(df.date), width,df['value'],0, color='blue', alpha=0.5)
show(picture)

我尝试搜索以下解决方案:

I have tried to search the solutions like:

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

我发现在bokeh的示例中,它也存在此问题.

I found that in the bokeh's example, it also has this problem.

http://docs.bokeh.org/en/0.12.6/docs/gallery/candlestick.html

推荐答案

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

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.

这篇关于使用bokeh绘制图形时,如何在x_axis类型为datetime的情况下删除缺失的日期,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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