简体   繁体   English

使用Bokeh Datetime Axis绘制周数

[英]Plotting Weeks with Bokeh Datetime Axis

I am trying to make a bar chart from Bokeh with a Datetime x-axis. 我正在尝试使用日期时间x轴从Bokeh制作条形图。 I am using several time resolutions, and I can get all of them to work except for the weeks. 我正在使用几个时间的解决方案,除了几周之外,我可以让它们全部工作。 I have a pandas dataframe that looks like this (I added the 'Week' column for reference, it shouldn't stay) 我有一个看起来像这样的pandas数据框(我添加了'Week'列供参考,它不应该留下)

Timestamp   ResultID
2017-12-03  106
2017-11-26  381
2017-11-19  406
2017-11-12  662
2017-11-05  656
2017-10-29  638
2017-10-22  429
2017-10-15  784

Output with date 输出日期

You can see that the labels are misaligned and sometimes missing. 您可以看到标签未对齐,有时会丢失。 It looks like Bokeh is always displaying the first day of the month regardless of if it is the first day of the week or not. 看起来Bokeh总是显示一个月的第一天,无论它是否是一周的第一天。 this then shifts the other labels out of position. 然后将其他标签移出位置。 I can't seem to find any documentation that would help to do this. 我似乎无法找到任何有助于这样做的文档。 I also can't use a categorical axis because the way that I am setting ranges to only show the 7 most recent bins will break. 我也不能使用分类轴,因为我设置范围的方式只显示最近的7个分箱会中断。 I've also tried changing the format of the labels to show the calendar week labels but no luck there either. 我也尝试更改标签的格式以显示日历周标签,但也没有运气。 Any ideas on how to get the labels to match with the appropriate columns? 有关如何使标签与相应列匹配的任何想法?



    bintime = 'W'
    bins = {'h' : '%Y-%m-%d %H:%M', 'D' : '%F', 'W' : '%F', 'M' : '%Y-%m'}
    widths = {'h' : 3600000, 'D' : 86400000, 'W' : 604800000, 'M' : 2419200000}
    pad = 0.9

    source = ColumnDataSource(df)

    now = np.datetime64(datetime.datetime.now())

    if bintime == 'M':
        padspace = now + np.timedelta64(1*30, 'D')
        dispdelta = now - np.timedelta64(7*30, 'D')
    else:
        padspace = now
        dispdelta = now - np.timedelta64(7, bintime)

    ranges = ['hours', 'days', 'months']

    #xrange = df.DT.tolist()
    xrange = (dispdelta, padspace)

    output = figure(plot_height=300, width=800, title="MPL Output", x_range=xrange, tools ='xpan,reset,xzoom_in,xzoom_out')
    output.vbar(x='Timestamp', top='ResultID', width=widths[bintime]*pad, source=source)
    output.xaxis.formatter=DatetimeTickFormatter(hours=bins[bintime], days=bins[bintime], months=bins[bintime])
    show(output)

SO I figured it out, posting in case someone else has a similar problem. 所以我想出来,以防万一其他人有类似的问题。

You need to modify the ticker method on the xaxis. 您需要修改xaxis上的ticker方法。 When a your x_axis_type is a datetime, Bokeh automatically uses the DatetimeTicker method . 当您的x_axis_type是日期时间时,Bokeh会自动使用DatetimeTicker方法 This finds the type that best fits the data range, which in this case is a DaysTicker that adds ticks on the 1st, 8th, 15th, and 22nd of the month. 这将找到最适合数据范围的类型,在本例中是一个DaysTicker,它在该月的第1天,第8天,第15天和第22天添加刻度。 This would be fine for a line or other area graph, but to get it to line up with the weeks you need to force the ticks to line up with the bars. 这对于线条或其他区域图表来说可能没问题,但是为了使它与需要强制刻度线与条形线对齐的周数对齐。 You can do this by: 你可以这样做:

tickers = pd.to_datetime(df.index.values).astype(int) / 10**6

or taking your datetime field and converting it to a ms epoch integer (the 10**6 is because a numpy datetime is in nanoseconds), and then adding output.xaxis.ticker = FixedTicker(ticks = tickers) 或者将你的日期时间字段转换为ms epoch整数(10 ** 6是因为numpy日期时间是以纳秒为单位),然后添加output.xaxis.ticker = FixedTicker(ticks = tickers)

This forces the ticks to align with the centers of the bars. 这会强制刻度线与条形中心对齐。 This way, however you resample your dataframe, the labels will match automatically. 这样,无论您重新采样数据帧,标签都会自动匹配。

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

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