'ploting a "timetable" with grouped bars for defined hours/timeboxes

I want to track my mobile devices at home so I can plot a "at home" and "not at home" diagram.

I collect the data as follows:

ip,device_name,start,end,length,date
192.168.178.123,aaa,2022-04-16 00:33:01.395443,2022-04-16 00:37:06.843443
192.168.178.123,aaa,2022-04-16 08:55:24.911787,2022-04-16 08:56:39.197196
192.168.178.123,aaa,2022-04-20 21:49:25.660712,2022-04-20 21:50:25.660712
192.168.178.123,aaa,2022-04-24 14:42:14.781557,2022-04-24 14:44:56.519343
192.168.178.234,bbb,2022-04-16 08:22:37.763442,2022-04-16 08:23:37.763442
192.168.178.234,bbb,2022-04-16 10:05:09.613899,2022-04-16 10:06:09.613899

Each entry of my csv-File represents the status of a device being not at home.

I want to have a diagram as shown enter image description here

I can't figure out how to do this in plotly. I tried to find a way using time series (https://plotly.com/python/time-series/) but I think there is nothing that helps me to do what I want.

This code brings me to an output which is quite near to what I want but I can not bring the y-axis to hours and show the gaps.

data_frame = pd.read_csv("awaytimes.csv", parse_dates=['start', 'end'])
data_frame['length'] = (data_frame['end'] - data_frame['start']) / pd.Timedelta(hours=1)
  
fig = px.bar(data_frame, 
             x="date", 
             y="length",
             color='device_name', 
             barmode='group',
             height=400)
fig.show()

enter image description here

I hope one of you can give me a hint.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source