charting.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import pandas as pd
  2. import sqlite3
  3. import os
  4. from matplotlib import pyplot as plt
  5. import seaborn as sns
  6. conn = sqlite3.connect('db.sqlite')
  7. def check_folder():
  8. path = './charts'
  9. if not os.path.exists(path):
  10. os.mkdir(path)
  11. print('create charts directory...')
  12. def time_series_chart(x, y, y_label, title):
  13. sns.set()
  14. plt.figure(figsize=(18,7))
  15. sns.lineplot(x, y)
  16. plt.xlabel('Round')
  17. plt.ylabel(y_label)
  18. plt.title(title)
  19. plt.savefig(os.path.join('charts', title + '.png'))
  20. def main():
  21. check_folder()
  22. # loading records
  23. res = pd.read_sql_query(
  24. '''select timestamp,
  25. normal_user_count,
  26. abnormal_user_count,
  27. nyms_count
  28. from cluster_result''',
  29. con=conn)
  30. # plot time-series key indicator fig
  31. time_series_chart(res.index, res.loc[:, 'nyms_count'],
  32. 'Size of Pseudonym',
  33. 'Attack Anonymity Set')
  34. time_series_chart(res.index, res.loc[:, 'normal_user_count'],
  35. 'Forwarded User Counts',
  36. 'Normal Active User')
  37. time_series_chart(res.index, res.loc[:, 'abnormal_user_count'],
  38. 'Delayed User Counts',
  39. 'Abnormal Active User')
  40. print('save finished.')
  41. if __name__ == '__main__':
  42. main()