【问题标题】:heatmap of values grouped by time - seaborn按时间分组的值的热图 - seaborn
【发布时间】:2021-07-19 09:32:54
【问题描述】:

我将按时间分组的变量的计数绘制为热图。但是,当同时包含小时和分钟时,计数非常低,因此生成的热图并不能真正提供任何真正的洞察力。是否可以在更大的时间内对计数进行分组?我希望测试一些不同的时间段(5、10 分钟)。

我也希望在 x 轴上绘制时间。类似于附加的输出。

import seaborn as sns
import pandas as pd
from datetime import datetime
from datetime import timedelta

start = datetime(1900,1,1,10,0,0)
end = datetime(1900,1,1,13,0,0)

seconds = (end - start).total_seconds()

step = timedelta(minutes = 1)

array = []
for i in range(0, int(seconds), int(step.total_seconds())):
    array.append(start + timedelta(seconds=i))

array = [i.strftime('%Y-%m-%d %H:%M%:%S') for i in array]

df2 = pd.DataFrame(array).rename(columns = {0:'Time'})
df2['Count'] = np.random.uniform(0.0, 0.5, size = len(df2))
df2['Count'] = df2['Count'].round(1)

df2['Time'] = pd.to_datetime(df2['Time'])
df2['Hour'] = df2['Time'].dt.hour
df2['Min'] = df2['Time'].dt.minute

g = df2.groupby(['Hour','Min','Count'])

count_df = g['Count'].nunique().unstack()

count_df.fillna(0, inplace = True)

sns.heatmap(count_df)

【问题讨论】:

    标签: python pandas seaborn heatmap


    【解决方案1】:

    为了处理这种情况,我认为使用数据下采样会很容易。更改阈值也很容易。输出图中的轴标签需要修改,但我们推荐这种方法。

    import seaborn as sns
    import pandas as pd
    import numpy as np
    from datetime import datetime
    from datetime import timedelta
    
    start = datetime(1900,1,1,10,0,0)
    end = datetime(1900,1,1,13,0,0)
    
    seconds = (end - start).total_seconds()
    
    step = timedelta(minutes = 1)
    
    array = []
    for i in range(0, int(seconds), int(step.total_seconds())):
        array.append(start + timedelta(seconds=i))
    
    array = [i.strftime('%Y-%m-%d %H:%M:%S') for i in array]
    
    df2 = pd.DataFrame(array).rename(columns = {0:'Time'})
    df2['Count'] = np.random.uniform(0.0, 0.5, size = len(df2))
    df2['Count'] = df2['Count'].round(1)
    
    df2['Time'] = pd.to_datetime(df2['Time'])
    df2['Hour'] = df2['Time'].dt.hour
    df2['Min'] = df2['Time'].dt.minute
    
    df2.set_index('Time', inplace=True)
    count_df = df2.resample('10min')['Count'].value_counts().unstack()
    count_df.fillna(0, inplace = True)
    
    sns.heatmap(count_df.T)
    

    【讨论】:

    • 更好的解决方案。不知道你可以用 resample 做到这一点:)
    • 这很好。是否可以交换轴。 Time 在 x 轴上?
    • 您可以使用sns.heatmap(count_df.T) 交换轴。
    • 干杯。这是否会使 y 轴倒置?
    • 倒置是指xticks的标签起点吗?
    【解决方案2】:

    实现此目的的方法是创建一列,其中的数字具有重复的分钟数元素。 例如:

    minutes = 3
    x = [0,1,2]
    np.repeat(x, repeats=minutes, axis=0)
    >>>> [0,0,0,1,1,1,2,2,2]
    

    然后使用此列对数据进行分组。

    所以你的代码看起来像:

    ...
    minutes = 5
    x = [i for i in range(int(df2.shape[0]/5))]
    df2['group'] = np.repeat(x, repeats=minutes, axis=0)
    
    g = df2.groupby(['Min', 'Count'])
    
    count_df = g['Count'].nunique().unstack()
    
    count_df.fillna(0, inplace = True)
    

    【讨论】:

      猜你喜欢
      • 2015-09-12
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 2021-12-05
      • 2018-08-07
      • 2021-11-01
      • 1970-01-01
      • 2021-03-01
      相关资源
      最近更新 更多