【发布时间】:2020-07-12 17:11:46
【问题描述】:
我有以下数据和热图,需要一些关于网格线格式的帮助:
import pandas as pd
import numpy as np
data = [
['tom', 1, 1, 1, '0:00', '10:26'],
['tom', 1, 1, 2, '15:30', '18:50'],
['tom', 1, 2, 1, '2:00', '9:15'],
['tom', 1, 2, 2, '13:10', '22:40'],
['tom', 2, 1, 1, '5:00', '22:15'],
['tom', 2, 2, 1, '0:00', '13:40']
]
# Create the pandas DataFrame
df = pd.DataFrame(
columns=['Name',
'Day',
'AllottedPeriod',
'AttemptNo',
'StartTime',
'EndTime'],
data=data,
)
def parse_time_periods(x):
start_minute, start_second = x['StartTime'].split(':')
end_minute, end_second = x['EndTime'].split(':')
# calculate the start and end time in seconds
start = (int(start_minute) * 60) + int(start_second)
end = (int(end_minute) * 60) + int(end_second)
test_range = range(start, end)
for i in range(1, 26, 1):
# create range to check for intercection with testing time
time_range = range((i - 1) * 60, i * 60)
# create variables to indicate if there is overlap between
# test time and minute range. For example, if a test was active
# between minute 10 and minute 15, column `15` will be 1
if len(set(test_range).intersection(time_range)) > 0:
x[f'{i:02}'] = 1
return x
df = df.apply(lambda x: parse_time_periods(x), axis=1).fillna(0)
stacked_df = df.drop(columns=['StartTime', 'EndTime','AttemptNo']).groupby(
by=['Name', 'Day', 'AllottedPeriod']
).agg(sum).unstack().swaplevel(0, 1, 1).sort_index(1)
import seaborn as sns
import matplotlib.pyplot as plt
f, ax = plt.subplots(figsize=(10,3))
cmap = plt.cm.OrRd_r
ax = sns.heatmap(stacked_df,cbar=False,cmap=cmap)
plt.show()
如何插入:
a) 每条线的水平网格线
b) 在2-01点处的一条垂直网格线
只是努力找出方法。任何帮助,将不胜感激!非常感谢!
【问题讨论】: