使用散景绘制图形时,如何在 x_axis 类型为 datetime 时删除丢失的日期,

唤醒

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

         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

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

在此处输入图片说明

有什么办法可以去掉两个空格键吗?我的代码在这里:

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)

我试图搜索解决方案,如:

使用日期时间作为 x 轴时,如何使散景忽略缺少的日期

我发现在散景的例子中,它也有这个问题。

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

大红点

从 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)

在此处输入图片说明

如果您有大量日期,这种方法可能会变得笨拙,并且可能需要自定义扩展

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Django-graphos绘制图形时为空模板

如何根据列表设置 x_axis?

如何使用R使用x轴上的所有日期绘制图形?

使用CubicSpline函数时如何绘制图形

使用networkx绘制图形时,如何在边缘上使用颜色?

将字符串转换/解析为Datetime Java时丢失日期

使用matplotlib绘制图形时,在光标位置看不到日期/时间。由于“ DateFormatter找到x = 0的值”而出现错误

在gnuplot中绘制图形时如何跳过COLUMN

使用 matplotlib 绘制图形时出错

删除时间序列之间的链接,并在ggplot中的x_axis上添加次要日期刻度

使用 x_axis 和 y_axis 生成多维数组

散景:绘制多个图形时的跨度

单击d3.js中树状图的节点时,为子节点绘制图形

如何检查变量类型是否为DateTime

如何使用ggplot为绘制图的x轴设置上限?

使用matplotlib绘制图时,X轴刻度标签太密

当列类型为DATE时,MYSQL在SELECT查询上返回完整的datetime字符串

导出时的 datetime64[ns] 列在 Oracle DB 中显示为 DATE 数据类型

创建新日期时,如何解决“类型对象'datetime.datetime'没有属性'timedelta'”?

当我使用 matplotlib 绘制图形时,如何增加其中一条线的线宽(或加粗)?

数据类型为数字时如何对Google图形X轴进行排序

无法散列的类型:绘制网络时为“设置”

使用Pandas和Matplotlib丢失数据并绘制图形

如何在X轴和y轴上绘制图形是R中的函数?

绘制图形时像素与记录之间的关系

在Ubuntu中绘制图形时出现问题

单击时绘制图形并使它们保持不变

在Matlab中绘制图形时如何指定轴尺寸?

当数据位于excel VBA中的每个备用列时如何绘制图形