With go.Bar()
one can pass an offset parameter that shifts the position of a bar within the group (barmode is set to 'overlay' for traces with an offset).
This works fine when the x-axis is numeric: but seems to not work when the x-axis is of datetime format:
Is this a bug or a feature? and can offsets work with datetime axes?
import plotly.graph_objects as go
import datetime
from datetime import timedelta
import pandas as pd
from random import random
# generage a datetime index
dates = pd.DatetimeIndex([datetime.date(2022,10,10) + timedelta(x) for x in range(10)]) # datetime index
# dates = range(10) # a list of int; uncomment to see offset
fig = go.Figure()
for i in range(3):
s = pd.Series(
[5*(random()-0.5) for x in range(len(dates))], # random values
index = dates
)
fig.add_trace(
go.Bar(
x = s.index,
y = s,
offset = 0.15*(i-1), # has no effect with datetime index
)
)
fig.show()
The offset is given in axis units.
When the axis is of type datetime, apparently the unit is a nanosecond (1ns = 1e-9 seconds)
So to notice an offset, you should adjust the magnitude of the offset accordingly.
If you set
offset = 1e7*(i-1),
width = 2e7 # also in axis units