Skip to content
Advertisement

Plotly: How to highlight weekends without looping through the dataset?

I am trying to plot three different timeseries dataframes (each around 60000 records) using plotly, while highlighting weekends (and workhours) with a different background color.

Is there a way to do it without looping through the whole dataset as mentioned in this solution. While this method might work, the performance can be poor on large datasets

Advertisement

Answer

I would consider using make_subplots and attach a go.Scatter trace to the secondary y-axis to act as a background color instead of shapes to indicate weekends.

Essential code elements:

JavaScript

Plot:

enter image description here

Complete code:

JavaScript

Speed tests:

For nperiods = 2000 in the code snippet below on my system, %%timeit returns:

JavaScript

The approach in my original suggestion using fig.add_shape() is considerably slower:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement