简体   繁体   中英

Constant width in bokeh graph with datetime axis

I set up a stacked bar graph in bokeh, each bar indicating one month:

在此处输入图像描述

The respective code bit looks as follows:

p.vbar_stack(
        categories,
        x="zeit",
        width=2629800000 * 0.9,
        color=Category20c[len(categories)],
        source=plot_data,
    )

The width is specified in milliseconds, corresponding to the average length of a month. I multiply this by 0.9 to create a gap between bars. The problem is that due to actual differences in month lengths, the gaps between bars differ. This is most striking between February and March (eg the third and fourth bar from the left in the picture).

How can I make bar widths (and gaps) robust to lenghts of months?

In your case, semantically it's not really a datetime axis, it's a categorical axis. If you switch to a proper categorical axis, your issue should be resolved.

If you are happy to have variable width bars you can use a timedelta for the width. We have to create one for each month. This deals with the Feb/March issue. I've used line_width for the separation between the bars.

df['days'] = df.index.to_series().apply(lambda x: datetime.timedelta(days=x.days_in_month))

plot_data = ColumnDataSource(df)

p.vbar_stack(
    categories,
    x="zeit",
    width='days',
    line_width=1, 
    line_color='white',
    color=Category20c[len(categories)],
    source=plot_data,
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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