histogramme-heure-minute.py 1.4 KB

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