【发布时间】: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