histogramme-heure-minute.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/python3
  2. import json
  3. import sys
  4. from datetime import datetime
  5. import matplotlib.pyplot as plt
  6. def import_array(filename):
  7. # Load the JSON file
  8. with open(filename, 'r') as f:
  9. data = json.load(f)
  10. # Access the "messages" array
  11. array = data['messages']
  12. return array
  13. def create_hist(messages):
  14. # Initialize the dict in which we'll store the
  15. # number of messages for each hour of the day
  16. hist = {}
  17. for hour in range(0, 24):
  18. for minute in range(0, 60):
  19. hist[str(hour).zfill(2) + str(minute).zfill(2)]=0
  20. for message in messages:
  21. if "date_unixtime" in message :
  22. message_hour = str(datetime.fromtimestamp(int(message["date_unixtime"])).hour).zfill(2)
  23. message_minute = str(datetime.fromtimestamp(int(message["date_unixtime"])).minute).zfill(2)
  24. hist[message_hour + message_minute] += 1
  25. return hist
  26. def main():
  27. # Get the file name from the first command line argument
  28. filename = sys.argv[1]
  29. # Import the array from the JSON file
  30. messages = import_array(filename)
  31. # create a dict containing the number of messages for each hour
  32. hist = create_hist(messages)
  33. # sort and print the resulting wins dict
  34. # print(sorted(hist.items(), key=lambda x:x[1], reverse=True))
  35. hours = list(hist.keys())
  36. counts = list(hist.values())
  37. fig = plt.figure(figsize = (10, 5))
  38. plt.xlabel("Messages par heure")
  39. plt.ylabel("Nb de messages")
  40. plt.title("Heure de la journée")
  41. # creating the bar plot
  42. plt.bar(hours, counts)
  43. #plt.show()
  44. plt.savefig("msg-heureminutes.png")
  45. if __name__ == '__main__':
  46. main()