draw_file.py 967 B

12345678910111213141516171819202122232425262728293031
  1. import matplotlib
  2. from utils.file.trans_methods import create_file_path
  3. matplotlib.use('Agg')
  4. matplotlib.rcParams['font.family'] = 'SimHei'
  5. matplotlib.rcParams['font.sans-serif'] = ['SimHei']
  6. from matplotlib import pyplot as plt
  7. def scatter(title, x_label, y_label, x_values, y_values, color=None, col_map=dict(), size=10,
  8. save_file_path=''):
  9. if save_file_path:
  10. create_file_path(save_file_path, True)
  11. else:
  12. save_file_path = title + '.png'
  13. plt.figure(figsize=(8, 6))
  14. plt.title(title, fontsize=16)
  15. plt.xlabel(x_label, fontsize=14)
  16. plt.ylabel(y_label, fontsize=14)
  17. if color is not None:
  18. plt.scatter(x_values, y_values, s=size, c=color)
  19. if col_map:
  20. patches = [plt.Rectangle((0, 0), 1, 1, fc=c) for c in col_map.values()]
  21. plt.legend(patches, list(col_map.keys()))
  22. else:
  23. plt.scatter(x_values, y_values, s=size)
  24. plt.savefig(save_file_path)
  25. plt.close()