Being all new to pandas, I have a probably very basic question. I have a camera in a tawny owl nest box and I noted down the time the owl spends in the nestbox. I want to visualize the time in the nestbox using Python. I made a pandas DatetimeIndex, and used it to make a pandas dataframe as follows. However, this shows the owl being present all the time. Is there a way to plot a 1 only in the time ranges specified by the DatetimeIndex?
import pandas as pd
import matplotlib.pyplot as plt
presence = pd.DatetimeIndex(["2021-12-01 18:08","2021-12-01 18:11",
"2021-12-02 05:27","2021-12-02 05:29",
"2021-12-02 22:40","2021-12-02 22:43",
"2021-12-03 19:24","2021-12-03 19:27",
"2021-12-06 18:04","2021-12-06 18:06",
"2021-12-07 05:28","2021-12-07 05:30",
"2021-12-10 03:05","2021-12-10 03:10",
"2021-12-10 07:11","2021-12-10 07:13",
"2021-12-10 20:40","2021-12-10 20:41",
"2021-12-12 19:42","2021-12-12 19:45",
"2021-12-13 04:13","2021-12-13 04:17",
"2021-12-15 04:28","2021-12-15 04:30",
"2021-12-15 05:21","2021-12-15 05:25",
"2021-12-15 17:40","2021-12-15 17:44",
"2021-12-15 22:31","2021-12-15 22:37",
"2021-12-16 04:24","2021-12-16 04:28",
"2021-12-16 19:58","2021-12-16 20:09",
"2021-12-17 17:42","2021-12-17 18:04",
"2021-12-17 22:19","2021-12-17 22:26",
"2021-12-18 05:41","2021-12-18 05:44",
"2021-12-19 07:40","2021-12-19 16:55",
"2021-12-19 20:39","2021-12-19 20:52",
"2021-12-19 21:56","2021-12-19 23:17",
"2021-12-21 04:53","2021-12-21 04:59",
"2021-12-21 05:37","2021-12-21 05:39",
"2021-12-22 08:06","2021-12-22 18:00",
])
df = pd.DataFrame({'owl presence' : np.ones(len(presence))}, index=presence)
df.plot()
``
CodePudding user response:
The default plot is a line plot. It will connect all the points and so you won't see the discontinuities. Instead, you need a scatter plot.
plt.scatter(df.index, df['owl presence'])
You can check how to customize the plot
Of course this needs some customization to make it pretty ;)