gerardcslabs
gerardcslabs

Reputation: 243

Set plotly Go line graph rangebreaks to exclude non-business hours AND weekends

I have a plotly line graph containing intraday closing stock prices for a particular stock. The data is stored in a df of which a sample looks as such:

                  date   close
169 2021-04-16 09:30:00  369.58
170 2021-04-16 10:00:00  370.73
171 2021-04-16 10:30:00  370.96
172 2021-04-16 11:00:00  371.14
173 2021-04-16 11:30:00  371.95

I am trying to set the x-axis so that it excludes both weekends and non-business hours but so far have only managed one. My graph looks as such:

graph = go.Figure()
graph.add_trace(go.Scatter(x=df['date'], y=df['close'], mode='lines', name=trace)
graph.update_layout(title_text=Prices, title_x=0.5, yaxis=dict(title_text=Price($), title_standoff=25))

The parameter rangebreaks sets this. This particular set excludes non-business hours:

price_graph.update_xaxes(rangebreaks=[dict(bounds=[16, 9.5], pattern="hour")])

and this excludes weekends:

price_graph.update_xaxes(rangebreaks=[dict(bounds=['sat', 'mon'])])

Is there a way to use both of these together?

Upvotes: 2

Views: 2079

Answers (1)

r-beginners
r-beginners

Reputation: 35155

The official reference says that you can exclude multiple ranges by setting multiple settings in the list.

fig.update_xaxes(rangebreaks=[dict(bounds=[16, 9.5], pattern="hour"),
                              dict(bounds=['sat', 'mon'])])

Upvotes: 3

Related Questions