Black Lives Matter. Please consider donating to Black Girls Code today.

Bar widths / bargap not working

Hi,

I made a plot which has dates (end of month dates) on the x-axis.

The plot has one trace that is a scatter,

and two traces that are bars.

In layout I set barmode: “stacked”

The desired result:

I would like the bars to have no gaps between them.

The problems:

  1. I tried using bargap: 0 in layout. It does work almost ok, but there is still a gap between most of the bars. I think the gap width is being set to 28 days (the minimum), because January-February bars are displayed ok, but the rest not.

bar example 1

  1. I tried instead setting width: 1 or 31 in the data traces, but then there is no bar whatsoever being displayed

Any suggestions or hints?

Best regards,

Antonio

That shouldn’t happen. Would you mind sharing a reproducible example to help us debug?

Yes, I will.

I’ll be out for some days, but as soon as I’m back I’ll prepare an example.

1 Like

Hi,

Here is a working example of the problem described:

Best regards,

Antonio

I’ve hit this error too. Would be good to know if this was ever resolved.

1 Like

I currently have this problem. Was it fixed by another setting than bargap or is it still relevant?

Seems like the bars should always have the same width. Since the February one is only 28 days large, the others scale with it.

If you are willing to aggregate per month, here is one way to solve it:
Give a width of 1000 * 3600 * 24 * 31, which is the maximum of milliseconds in the month.

Edit: As pointed by ajoposor, there is now an overlap of the bars.
To get rid of it, provide the width independently for each bar. Quite annoying if you take leap years into account, but doasabe.

It is a way to deal with the gaps. However, now instead of gaps, there are overlaps in the bars.

Based on your example, I noticed that there is some bug in the math for setting the mid point of the next month. For instance, the mid-point for February is set based on the number of days of the preceding month, i.e., 31, That’s why the overlap is greater on the right side than the left side if you set February to have 35 days (when set to 31, there’s only a right side overlap.

I think your problem is caused by type of column which is set to xaxis. If it is datetime and you have values only for first day of month it is not possible to have the same bargaps, since there is different distance between bars. Try to change your datetime column to string.

Marcin