Get timestamps between two timestamp type columns , explode and plot as a shaded area between the given timestamps [duplicate]

Here is what my dataframe looks like:
Switch| start| end |

OFF |2021-11-14 16:00:22    |2021-11-14 16:00:46
OFF |2021-11-14 16:01:19    |2021-11-14 16:01:42
ON  |2021-11-14 16:02:13    |2021-11-14 16:02:37
ON  |2021-11-14 16:03:15    |2021-11-14 16:03:35    
ON  |2021-11-14 16:04:41    |2021-11-14 16:04:54    
OFF |2021-11-14 16:04:04    |2021-11-14 16:04:16    
ON  |2021-11-14 16:11:07    |2021-11-14 16:11:16    

I want to plot this using matplotlib or seaborn something like here text where I want to display the shaded regions as in enter image description here
but I also want two different colors for ON and off.
How do I do this using Pandas and Seaborn?

Expected output will have:
Timestamps on x axis and y axis will have a lineplot using code like this

y_values = np.random.rand(20)

x_values = pd.date_range("2023-11-14T16:00:00", "2023-11-14T16:12:00", 20)

plt.plot(x_values, y_values)

And then I want shaded regions using the dataframe shown above to show different positions of the switch
image output

I tried exploding and unnesting like in SQL but having a hard time.

  • What does your output plot look like for this dataset?

    – 

  • Added a plot link. You wull see a grade shaded region, I want to add such shaded areas with two colors , one for ON and one for OFF. @SurajShourie

    – 

  • What did you try? What is on your x-axis and y-axis of the plot that you want? you only have datetime values

    – 




Leave a Comment