【问题标题】:How to visualize absence/presence data如何可视化缺勤/在场数据
【发布时间】:2021-12-22 10:51:59
【问题描述】:

作为 pandas 的新手,我可能有一个非常基本的问题。我在一个黄褐色的猫头鹰巢箱里有一台相机,我记下了猫头鹰在巢箱里度过的时间。我想使用 Python 可视化嵌套框中的时间。我制作了一个熊猫 DatetimeIndex,并用它制作了一个熊猫数据框,如下所示。然而,这表明猫头鹰一直在场。有没有办法仅在 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()
``

【问题讨论】:

    标签: python pandas datetimeindex


    【解决方案1】:

    如果你的存在值是:

    开始观察,结束观察,开始观察,结束 观察等。

    您可能希望每分钟生成一个时间范围,其中 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() 看起来像这样:

    当然,这需要一些定制才能让它漂亮;)

    【讨论】:

      【解决方案2】:

      默认图是线图。它将连接所有点,因此您不会看到不连续性。相反,您需要一个散点图。

      plt.scatter(df.index, df['owl presence'])
      

      您可以查看如何自定义情节here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-22
        • 1970-01-01
        • 1970-01-01
        • 2017-08-15
        • 1970-01-01
        相关资源
        最近更新 更多