draw_file.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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()
  27. if __name__ == '__main__':
  28. import pandas as pd
  29. import numpy as np
  30. from matplotlib import pyplot as plt
  31. df = pd.read_csv(r"/home/wzl/test_data/2024_10_17_14_54_46_200k_Root.csv")
  32. df.reset_index(inplace=True, drop=True)
  33. df.columns = ['data']
  34. # Calculate the moving average with a window of 3 (1 before, 1 after)
  35. window_size = 20
  36. moving_avg = df['data'].rolling(window=window_size).mean()
  37. df['moving_avg'] = moving_avg
  38. # Calculate the percentage difference
  39. percentage_diff = abs((df['data'] - moving_avg) / moving_avg) * 100
  40. df['percentage_diff'] = percentage_diff
  41. # Flag values that differ by more than threshold
  42. threshold = 3
  43. df['is_anomaly'] = percentage_diff < threshold
  44. avg = df['data'].mean()
  45. df['avg']=df['data'] > avg
  46. difference_ratio = df.iloc[window_size:]
  47. difference_ratio.reset_index(inplace=True)
  48. # 创建图形和轴对象
  49. plt.figure(figsize=(10, 6))
  50. colors = np.where((difference_ratio['is_anomaly'] == True) & (difference_ratio['avg'] == True), 'r', np.where((difference_ratio['is_anomaly'] == False) & (difference_ratio['avg'] == False), 'g', 'b'))
  51. datas = difference_ratio['data'].values
  52. # for i in range(len(datas)):
  53. # plt.plot(i, datas[i], marker='o', color=colors[i])
  54. plt.figure(figsize=(10, 6))
  55. plt.scatter([i for i in range(len(datas))], datas, c=colors)
  56. # 添加标题和标签
  57. plt.title('Difference Ratio of Each Data Point to Its Previous 10 Data Points Mean')
  58. plt.xlabel('Index')
  59. plt.ylabel('Difference Ratio')
  60. # 显示网格
  61. plt.grid(True)
  62. # 显示图形
  63. plt.show()