histogramme-heure.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. hist[hour]=0
  19. for message in messages:
  20. if "date_unixtime" in message :
  21. message_hour = datetime.fromtimestamp(int(message["date_unixtime"])).hour
  22. hist[message_hour] += 1
  23. return hist
  24. def main():
  25. # Get the file name from the first command line argument
  26. filename = sys.argv[1]
  27. # Import the array from the JSON file
  28. messages = import_array(filename)
  29. # create a dict containing the number of messages for each hour
  30. hist = create_hist(messages)
  31. # sort and print the resulting wins dict
  32. print(sorted(hist.items(), key=lambda x:x[1], reverse=True))
  33. hours = list(hist.keys())
  34. counts = list(hist.values())
  35. fig = plt.figure(figsize = (10, 5))
  36. plt.xlabel("Messages par heure")
  37. plt.ylabel("Nb de messages")
  38. plt.title("Heure de la journée")
  39. # creating the bar plot
  40. plt.bar(hours, counts,width = 0.4)
  41. #plt.show()
  42. plt.savefig("msg-heures.png")
  43. if __name__ == '__main__':
  44. main()