windRoseOfTurbine.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import os
  2. import pandas as pd
  3. import numpy as np
  4. import plotly.graph_objects as go
  5. from plotly.subplots import make_subplots
  6. import seaborn as sns
  7. import matplotlib.pyplot as plt
  8. from matplotlib.ticker import MultipleLocator
  9. from windrose import WindroseAxes
  10. from .analyst import Analyst
  11. from .utils.directoryUtil import DirectoryUtil as dir
  12. from confBusiness import *
  13. from matplotlib.cm import get_cmap
  14. from matplotlib.colors import ListedColormap
  15. class WinRoseOfTurbineAnalyst(Analyst):
  16. """
  17. 风电机组变桨-功率分析
  18. """
  19. def typeAnalyst(self):
  20. return "wind_rose_turbine"
  21. def turbinesAnalysis(self, dataFrameMerge, outputAnalysisDir, confData: ConfBusiness):
  22. self.windRoseAnalysis(dataFrameMerge, outputAnalysisDir, confData)
  23. def windRoseAnalysis(self, dataFrameMerge, outputAnalysisDir, confData: ConfBusiness):
  24. # 风速区间
  25. bins = [0, 3, 6, 9, np.inf]
  26. speed_labels = ['[0,3)', '[3,6)', '[6,9)', '>=9']
  27. # 准备颜色映射
  28. colors = plt.cm.Blues(np.linspace(0, 1, len(speed_labels)))
  29. cmap = ListedColormap(colors)
  30. # 将风向按照22.5度一个间隔进行分组
  31. wind_directions = np.arange(0, 360, 22.5)
  32. # 按设备名分组数据
  33. grouped = dataFrameMerge.groupby(Field_NameOfTurbine)
  34. print("self.ratedPower {}".format(confData.rated_power))
  35. # 遍历每个设备并绘制散点图
  36. for name, group in grouped:
  37. # 对风速进行分箱处理,但不添加到DataFrame中
  38. speed_bins = pd.cut(group[confData.field_wind_speed], bins=bins, labels=speed_labels)
  39. # 将风向按照22.5度一个间隔进行分组
  40. wind_directions = np.arange(0, 360, 22.5)
  41. group['风向分组'] = pd.cut(group[confData.field_wind_dir], bins=wind_directions, labels=wind_directions[:-1])
  42. # 绘制风玫瑰图
  43. fig, ax = plt.subplots(figsize=(8, 8), subplot_kw={'polar': True})
  44. # 为每个风速区间绘制风向的条形图,并添加图例
  45. for i, (label, color) in enumerate(zip(speed_labels, colors)):
  46. # 筛选出当前风速区间的数据
  47. subset = group[speed_bins == label]
  48. # 计算每个风向分组的频数
  49. counts = subset['风向分组'].value_counts().reindex(wind_directions[:-1], fill_value=0)
  50. # 绘制条形图,并添加标签用于图例
  51. bar = ax.bar(counts.index * np.pi / 180, counts.values, color=cmap(i), alpha=0.75, width=(22.5 * np.pi / 180), label=label)
  52. # 设置标题和标签
  53. ax.set_title("Wind Rose", va='top')
  54. ax.set_theta_zero_location('N') # 设置0度位置为北
  55. ax.set_theta_direction(-1) # 设置角度方向为顺时针
  56. ax.set_yticklabels([]) # 不显示y轴刻度标签
  57. ax.set_xticks(wind_directions * np.pi / 180) # 设置x轴刻度
  58. ax.set_xticklabels(['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW']) # 设置x轴刻度标签为方向
  59. # 添加图例,并设置位置以避免与图形重叠
  60. ax.legend(title='Wind Speed', bbox_to_anchor=(1.1, 1), loc='center left', borderaxespad=0.)
  61. # 保存图像并关闭绘图窗口
  62. output_file = os.path.join(outputAnalysisDir, f"{name}.png")
  63. plt.savefig(output_file, bbox_inches='tight', dpi=120)
  64. plt.close()