histogramme-heure.py 1.2 KB

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