-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrequency.py
31 lines (21 loc) · 879 Bytes
/
frequency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from utils import load_messages
import datetime
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
messages = load_messages()
timestamps = [int(message['date_unixtime']) for message in messages]
datetimes = pd.to_datetime(timestamps, unit='s')
df = pd.DataFrame(datetimes, columns=['datetime'])
df['day_of_week'] = df['datetime'].dt.dayofweek
df['hour'] = df['datetime'].dt.hour
frequency = df.groupby(['day_of_week', 'hour']).size().unstack(fill_value=0)
plt.figure(figsize=(8, 3))
sns.heatmap(frequency, cmap='YlGnBu', linewidths=.5, annot=False)
plt.ylabel('Day of Week')
plt.xlabel('Hour of Day')
plt.title('Posting Frequency Heatmap')
plt.xticks(np.arange(0.5, len(frequency.columns), 1), frequency.columns)
plt.yticks(np.arange(0.5, len(frequency.index), 1), ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'])
plt.show()