Created
March 7, 2024 15:52
-
-
Save hex2f/4402106a0afcee0008ea0d884d7fcd2d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
from tqdm import tqdm | |
import glob | |
from dateutil.parser import parse | |
# Read messages.csv in all data/messages/* directories | |
message_files = glob.glob('data/messages/*/messages.csv') | |
# Create a list of all message times | |
message_times = [] | |
for file in tqdm(message_files): | |
with open(file, 'r') as f: | |
reader = csv.reader(f) | |
next(reader) | |
for row in reader: | |
message_times.append(parse(row[1])) | |
# sort the message times | |
message_times.sort() | |
print(message_times[0:10]) | |
# Create a list of times of day | |
times = [t.hour + t.minute/60 for t in message_times] | |
# Plot the times | |
plt.scatter(times, message_times, alpha=0.5, s=0.5) | |
plt.xlabel('Time of day') | |
plt.ylabel('Date') | |
# scale the x axis to be 24 hours | |
plt.xlim(0, 24) | |
# scale the y axis to be the first and last date | |
plt.ylim(message_times[0], message_times[-1]) | |
# set the layout to be tight | |
plt.tight_layout() | |
# show months on the y axis | |
plt.yticks(pd.date_range(message_times[0], message_times[-1], freq='ME')) | |
# show each hour on the x axis | |
plt.xticks(range(24)) | |
# invert the y axis so that the first date is at the top | |
plt.gca().invert_yaxis() | |
# set layout to be tall | |
plt.gcf().set_size_inches(10, 20) | |
# increase resolution | |
plt.gcf().set_dpi(300) | |
# save the plot to a file | |
plt.savefig('message_times.png', bbox_inches='tight', dpi=300) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment