0

I have three large timeseries (each around 60000 records). I want to plot them together while highlighting weekends (and maybe workhours) with a different background color.

The solution I found was the one mentioned here https://stackoverflow.com/a/65174049/1831518 which loops over all records and draws a rectangle that starts @ first day of the weekend and end @ last day of the weekend.

While this might work fine for a small dataset but looping through a long series kept running for 20+ hours (taking into consideration that the loop is checking for weekends and work-hours in each iteration)

Is there a faster way to achieve this through plotly or other alternative (with the same interactive plotting capabilities)?

For reference, following is my code

fig = make_subplots(rows=1, cols=1)

fig.add_scatter(x=fl15['time'], y=fl15['ThreePhaseElectricityMeasurement.Active_power_Total_Minute_Max'], mode='lines', name='Floor 15')
fig.add_scatter(x=fl16['time'], y=fl16['ThreePhaseElectricityMeasurement.Active_power_Total_Minute_Max'], mode='lines', name='Floor 16')
fig.add_scatter(x=fl17['time'], y=fl17['ThreePhaseElectricityMeasurement.Active_power_Total_Minute_Max'], mode='lines', name='Floor 17')

fig.add_hrect(y0=3146, y1=4836, line_width=0, fillcolor="yellow", opacity=0.35)

for index, row in fl15.iterrows():
    if row['time'].weekday() == 4 : # or row['Date_Hour'].weekday() == 4:
        fig.add_shape(type="rect",
                        xref="x",
                        yref="paper",
                        x0=row['time'],
                        y0=0,
#                         x1=row['date'],
                        x1=row['time'] + pd.DateOffset(1),
                        y1=1,
                        line=dict(color="rgba(0,0,0,0)",width=3,), opacity=0.2,
                        fillcolor="rgba(189, 186, 183, 1)",
                        layer='below')
    else:
        if row['time'].hour== 6 : # and row['Date_Hour'].dt.hour<= 18:
                fig.add_shape(type="rect",
                    xref="x",
                    yref="paper",
                    x0=row['time'],
                    y0=0,
#                         x1=row['date'],
                    x1=row['time'] + pd.DateOffset(hours=12),
                    y1=1,
                    line=dict(color="rgba(0,0,0,0)",width=3,), opacity=0.2,
                    fillcolor="rgba(135, 190, 238, 1)",
                    layer='below') 

fig.update_layout(
    title="Minute Max Power Vs. Time",
    xaxis_title="Time",
    yaxis_title="Minute Max Power (W)"
    )
        
        
fig.show()
Abdelrahman Shoman
  • 2,882
  • 7
  • 36
  • 61
  • 1
    I am not sure why this question got two votes to close because it will `This question is likely to lead to opinion-based answers.`. I am looking for a faster solution, and speed is no subjective. `timeit` can be used to evaluate time complexity of any given answer – Abdelrahman Shoman Sep 13 '21 at 10:42
  • So this question was reopened after it was closed and voted down. Anyhow, I already had the question paraphrased and answered (and timed using `timeit`) here https://stackoverflow.com/questions/69162561/how-to-highlight-weekends-without-looping-through-the-dataset/69177656#69177656 – Abdelrahman Shoman Sep 15 '21 at 06:16

0 Answers0