Automatically adjust widths of a `Bar` trace to fill white gap

I’m using the Bar trace to plot a timeseries which has data with different time frequencies.

The width of the bars is computed by plotly probably using the initial part of the data, which means that the data with coarser time resolution will have some white gaps in between the bars.

Is there any way to tell plotly to set the bar widths so that there’s no left space in between? I know that you can control the bar widths manually, I just want to avoid having to compute this myself every time…

Hi @guidocioni ,

Try to use sets the axis type as category to make your make xaxis have same space each bar.

import as px

y = [0.8, 0.2, 0.7, 0.7, 0.2, 0.7, 0.9, 0.2, 0.1, 0.7, 0.2, 0.2, 0.4, 0.5, 0.8]

fig =, y=y)
# set x axis type to `category`, the default value is "-".

Hope this help

I mean, it kind of works, but interpreting a time-series axis as category is a recipe for disaster, I think :smiley:

I ended up computing the widths manually.
When the x-axis is a datetime you have to provide the widths in milliseconds so, assuming you have a pandas dataframe with a time column that you’re using as x axis, you can do something like this

  width=(data['time'].diff().dt.seconds * 850).fillna(method='bfill').values

Note that I multiply by 850 (and not 1000) to simulate a small gap between the bars.

This seems to work pretty well for most of the cases.

1 Like