When I use the following code for xaxes rangebreaks the charts shows all data in the interval Feb 13 to Feb 22 excluding the weekend Feb 18-19 and the holiday on Feb 20:
Below is some code that generates dummy data and says there is some time gap (or holiday in the case of the stock market) which is filled with NaNs that I would like to remove from the plot. This works fine as seen in (2) but when you introduce other rangebreaks such as only using weekdays and trading hours (as my use case is for financial plotting) it results in some overlapping of the data. I will say this data looks strange because itβs not real market data but I have also attached a real-world example screenshot that should show what Iβm seeing.
Minimum reproducible example:
def generate_random_data(n, mu=0.001, sigma=0.01, start_price=5):
np.random.seed(0)
ts = pd.date_range("2018-01-01", periods=n, freq="H")
returns = np.random.normal(loc=mu, scale=sigma, size=n)
close = start_price * (1 + returns).cumprod()
return pd.DataFrame({"timestamp": ts, "close": close})
data = generate_random_data(n=500)
# Say there is a holiday here that I don't have data for
holiday = "2018-01-04"
data[pd.to_datetime(data["timestamp"].dt.date) == pd.to_datetime(holiday)] = np.nan
fig = px.line(x=data["timestamp"], y=data["close"])
fig.update_xaxes(rangebreaks=[
dict(bounds=[20, 13.5], pattern="hour"), # Remove non-trading hours
dict(bounds=["sat", "mon"]), # Remove weekends
dict(values=[holiday]), # Date I would like to remove
])
fig.update_layout(
title=dict(
text="Rangebreaks Issue On Time-Series Data",
x=0.5,
),
xaxis=dict(title="Time"),
yaxis=dict(title="Price ($)"),
font=dict(size=16),
)
fig.show()
(1): Time series without any range breaks added (working as expected).
Hi @adamschroeder , the code snippet above generates some random data which I used in the screenshots (1-3). If you want the real market data I provided in (4), I can put that into a pickle file when I get home tonight and attach it here. That code snippet should be all you need to reproduce screenshots (1-3). You just need to comment/uncomment out some of these lines to generate the differences seen in (1-3).
...
fig.update_xaxes(rangebreaks=[
dict(bounds=[20, 13.5], pattern="hour"), # Remove non-trading hours
dict(bounds=["sat", "mon"]), # Remove weekends
dict(values=[holiday]), # Date I would like to remove
])
...