散景中X轴上的日期时间 [英] Datetime on an X Axis in Bokeh

查看:272
本文介绍了散景中X轴上的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解日期时间在Bokeh中的工作方式.

I'm having trouble understanding how datetime works in Bokeh.

我的代码在x轴上输出毫秒,如果轴类型为"datetime",这似乎是默认值,但是我希望每5年将其间隔一次.

My code is outputting milliseconds on the x axis, which seems to be the default if the axis type is 'datetime', but I would like to have it separated by every 5 years.

如何在Bokeh中做到这一点?日期是在1950年之前,所以我不能使用时间戳记.当我排除datetime轴类型时,它将在x轴上将每个单个值显示为刻度.当我尝试将输入的数据更改为日期时间对象时,Bokeh拒绝了它.

How can I accomplish this in Bokeh? The dates are before 1950 so I can't use a timestamp. When I exclude the datetime axis type, it displays every single value as a tick on the x axis. When I try to change the inputted data as a datetime object, Bokeh rejects it.

我还阅读了有关该主题的其他文章,以及Bokeh关于以下内容的文档: DatetimeTickFormatter,这似乎也不适合我.

I've read other posts on the subject, as well as Bokeh's documentation on DatetimeTickFormatter, which doesn't seem to be working for me either.

Bokeh中的日期时间轴

具有自定义Datetime/Timestamp值的Bokeh FixedTicker

https://docs.bokeh.org/en/latest/docs/reference/models/formatters.html?highlight=formatters#module-bokeh.models.formatters

我所有的情节代码如下:

All my code for the plot is below:

r = requests.get('https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/GDP-data.json')
    data = json.loads(r.text)

chart_data = {
    'date': [a[0] for a in data['data']], #this is the problem data
    'gdp': [a[1] for a in data['data']],
}

source = ColumnDataSource(chart_data)
hover = HoverTool(
    tooltips=[
        ('GDP', '@gdp{$0,0.00}'),
        ('Date', '@date{%Y - %B}'),
    ],
    formatters={
        'date' : 'datetime',
    }
)

bar = figure(x_range=chart_data['date'],
             x_axis_type='datetime',
             plot_height=750,
             plot_width=1000,
             tools='wheel_zoom, reset, save',
             title=data['source_name'])
bar.vbar(x='date', top='gdp', width=0.9, source=source)
bar.add_tools(hover)

#visual settings

bar.xaxis.formatter = DatetimeTickFormatter(years = ['%Y'])
bar.xaxis.major_label_orientation = 'vertical'
bar.xaxis.minor_tick_line_color = None
bar.xgrid.grid_line_color = None

bar.yaxis.formatter = NumeralTickFormatter(format='$0,0.00')
bar.ygrid.grid_line_color = None

show(bar)

谢谢您的帮助,这让我发疯了!

Thank you for the help, this has been driving me crazy!

推荐答案

感谢bigreddot的帮助.我的问题是,由于我设置了x_range属性,而不是将其默认为datetime,因此我的情节充当了分类范围.

Thanks to help from bigreddot. My issue was that my plot was acting as a Categorical range due to me setting the x_range property, instead of defaulting to datetime.

这篇关于散景中X轴上的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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