draw_file.py 1017 B

1234567891011121314151617181920212223242526272829303132
  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. matplotlib.rcParams['axes.unicode_minus'] = False
  7. from matplotlib import pyplot as plt
  8. def scatter(title, x_label, y_label, x_values, y_values, color=None, col_map=dict(), size=10,
  9. save_file_path=''):
  10. if save_file_path:
  11. create_file_path(save_file_path, True)
  12. else:
  13. save_file_path = title + '.png'
  14. plt.figure(figsize=(8, 6))
  15. plt.title(title, fontsize=16)
  16. plt.xlabel(x_label, fontsize=14)
  17. plt.ylabel(y_label, fontsize=14)
  18. if color is not None:
  19. plt.scatter(x_values, y_values, s=size, c=color)
  20. if col_map:
  21. patches = [plt.Rectangle((0, 0), 1, 1, fc=c) for c in col_map.values()]
  22. plt.legend(patches, list(col_map.keys()))
  23. else:
  24. plt.scatter(x_values, y_values, s=size)
  25. plt.savefig(save_file_path)
  26. plt.close()