如何可视化 absence/presence 数据
How to visualize absence/presence data
作为 pandas 的新手,我有一个可能非常基本的问题。我在黄褐色的 owl 巢箱中有一个相机,我记下了 owl 在巢箱中花费的时间。我想使用 Python 可视化巢箱中的时间。我制作了一个 pandas DatetimeIndex,并用它制作了一个 pandas 数据框,如下所示。但是,这表明 owl 一直存在。有没有办法只在 DatetimeIndex 指定的时间范围内绘制 1?
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()
``
默认图是线图。它将连接所有点,因此您不会看到不连续点。相反,您需要一个散点图。
plt.scatter(df.index, df['owl presence'])
您可以查看如何自定义绘图here
如果您的存在价值是:
start observation, end observation, start observation, end
observation, etc.
您可能希望每分钟生成一个时间范围,其中 value = 1
如果 owl 存在,value = 0
如果 owl 不存在。
您可以通过创建一个数据框来做到这一点:
presence_df=pd.DataFrame(data={'value':[1,0]*int(len(presence)/2)}, index=presence)
df = pd.DataFrame(index=pd.date_range(presence.min(), presence.max(), freq='min'))
df2 = df.merge(presence_df, left_index=True, right_index=True, how='left').ffill()
df2.plot()
df2
看起来像:
value
2021-12-01 18:08:00 1.0
2021-12-01 18:09:00 1.0
2021-12-01 18:10:00 1.0
2021-12-01 18:11:00 0.0
2021-12-01 18:12:00 0.0
... ...
2021-12-22 17:56:00 1.0
2021-12-22 17:57:00 1.0
2021-12-22 17:58:00 1.0
2021-12-22 17:59:00 1.0
2021-12-22 18:00:00 0.0
df2.plot()
看起来像这样:
当然,这需要一些定制才能让它更漂亮 ;)
作为 pandas 的新手,我有一个可能非常基本的问题。我在黄褐色的 owl 巢箱中有一个相机,我记下了 owl 在巢箱中花费的时间。我想使用 Python 可视化巢箱中的时间。我制作了一个 pandas DatetimeIndex,并用它制作了一个 pandas 数据框,如下所示。但是,这表明 owl 一直存在。有没有办法只在 DatetimeIndex 指定的时间范围内绘制 1?
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()
``
默认图是线图。它将连接所有点,因此您不会看到不连续点。相反,您需要一个散点图。
plt.scatter(df.index, df['owl presence'])
您可以查看如何自定义绘图here
如果您的存在价值是:
start observation, end observation, start observation, end observation, etc.
您可能希望每分钟生成一个时间范围,其中 value = 1
如果 owl 存在,value = 0
如果 owl 不存在。
您可以通过创建一个数据框来做到这一点:
presence_df=pd.DataFrame(data={'value':[1,0]*int(len(presence)/2)}, index=presence)
df = pd.DataFrame(index=pd.date_range(presence.min(), presence.max(), freq='min'))
df2 = df.merge(presence_df, left_index=True, right_index=True, how='left').ffill()
df2.plot()
df2
看起来像:
value
2021-12-01 18:08:00 1.0
2021-12-01 18:09:00 1.0
2021-12-01 18:10:00 1.0
2021-12-01 18:11:00 0.0
2021-12-01 18:12:00 0.0
... ...
2021-12-22 17:56:00 1.0
2021-12-22 17:57:00 1.0
2021-12-22 17:58:00 1.0
2021-12-22 17:59:00 1.0
2021-12-22 18:00:00 0.0
df2.plot()
看起来像这样:
当然,这需要一些定制才能让它更漂亮 ;)