draw_file.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # import matplotlib
  2. #
  3. # from utils.file.trans_methods import create_file_path
  4. #
  5. # matplotlib.use('Agg')
  6. # matplotlib.rcParams['font.family'] = 'SimHei'
  7. # matplotlib.rcParams['font.sans-serif'] = ['SimHei']
  8. # matplotlib.rcParams['axes.unicode_minus'] = False
  9. # from matplotlib import pyplot as plt
  10. #
  11. #
  12. # def scatter(title, x_label, y_label, x_values, y_values, color=None, col_map=dict(), size=10,
  13. # save_file_path=''):
  14. # if save_file_path:
  15. # create_file_path(save_file_path, True)
  16. # else:
  17. # save_file_path = title + '.png'
  18. #
  19. # plt.figure(figsize=(8, 6))
  20. # plt.title(title, fontsize=16)
  21. # plt.xlabel(x_label, fontsize=14)
  22. # plt.ylabel(y_label, fontsize=14)
  23. # if color is not None:
  24. # plt.scatter(x_values, y_values, s=size, c=color)
  25. # if col_map:
  26. # patches = [plt.Rectangle((0, 0), 1, 1, fc=c) for c in col_map.values()]
  27. # plt.legend(patches, list(col_map.keys()))
  28. # else:
  29. # plt.scatter(x_values, y_values, s=size)
  30. #
  31. # plt.savefig(save_file_path)
  32. # plt.close()
  33. #
  34. #
  35. # if __name__ == '__main__':
  36. # import pandas as pd
  37. # import numpy as np
  38. # from matplotlib import pyplot as plt
  39. #
  40. # df = pd.read_csv(r"/home/wzl/test_data/2024_10_17_14_54_46_200k_Root.csv")
  41. # df.reset_index(inplace=True, drop=True)
  42. # df.columns = ['data']
  43. #
  44. # # Calculate the moving average with a window of 3 (1 before, 1 after)
  45. # window_size = 20
  46. # moving_avg = df['data'].rolling(window=window_size).mean()
  47. # df['moving_avg'] = moving_avg
  48. # # Calculate the percentage difference
  49. # percentage_diff = abs((df['data'] - moving_avg) / moving_avg) * 100
  50. # df['percentage_diff'] = percentage_diff
  51. # # Flag values that differ by more than threshold
  52. # threshold = 3
  53. # df['is_anomaly'] = percentage_diff < threshold
  54. #
  55. # avg = df['data'].mean()
  56. # df['avg']=df['data'] > avg
  57. #
  58. #
  59. # difference_ratio = df.iloc[window_size:]
  60. # difference_ratio.reset_index(inplace=True)
  61. # # 创建图形和轴对象
  62. # plt.figure(figsize=(10, 6))
  63. # 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'))
  64. #
  65. # datas = difference_ratio['data'].values
  66. # # for i in range(len(datas)):
  67. # # plt.plot(i, datas[i], marker='o', color=colors[i])
  68. #
  69. # plt.figure(figsize=(10, 6))
  70. # plt.scatter([i for i in range(len(datas))], datas, c=colors)
  71. #
  72. # # 添加标题和标签
  73. # plt.title('Difference Ratio of Each Data Point to Its Previous 10 Data Points Mean')
  74. # plt.xlabel('Index')
  75. # plt.ylabel('Difference Ratio')
  76. #
  77. # # 显示网格
  78. # plt.grid(True)
  79. #
  80. # # 显示图形
  81. # plt.show()