I have a time series with
- a datetime64 index
- of a 5 minute interval
- that has gaps without any rows for when there are no values.
This is the closing price of each 5 minute period of a Hong Kong listed stock, where they also have a lunch break in the trading day. Thus, nighttime as well as weekends and trading holidays are not part of the timeseries, as there is no data.
When plotting, Plotly picks up that the index is datetime64, and sets the xaxis to cover every 5 minute period from the start to end of my data. A line is drawn horizontally to connect the datapoints when there is no data. Instead, I want the xaxis to be contracted, so for every point on the xaxis there is a new value from the time series.
Iβve seen suggestions of using rangebreaks to exclude weekends, and to set which time periods to render - which could for instance account for the lunch break. However, these are not very robust methods as they do not account for trading holidays, half days, changes in daylight savings in different parts of the world etc.
Iβm of the opinion that the best solution would be to render only based upon the data in the time series itself. How can I configure the xaxis to contract?
Hereβs the pandas dataframe head and tail:
Close_A RollingCorrelation
DateTime
2022-07-04 03:20:00 79.50 NaN
2022-07-04 03:30:00 79.60 NaN
2022-07-04 03:35:00 82.80 NaN
2022-07-04 03:40:00 84.90 NaN
2022-07-04 03:45:00 83.95 NaN
Close_A RollingCorrelation
DateTime
2022-11-24 08:40:00 63.60 0.115144
2022-11-24 08:45:00 63.55 0.017002
2022-11-24 08:50:00 63.50 -0.002246
2022-11-24 08:55:00 63.55 -0.016065
2022-11-24 09:05:00 63.55 0.061095
And my code:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(rows=2, cols=1)
fig.add_trace(go.Scatter(x=merged_df.index,
y=merged_df['Close_A'], name='A'), row=1, col=1)
fig.add_trace(go.Scatter(x=merged_df.index, y=merged_df['RollingCorrelation'],
name='Rolling Correlation 50'), row=2, col=1)
fig.show()