python对比作图两个风速风向表

背景介绍

在前面python得到风速风向图一文中,我们得到风速风向表后,

有时候我们需要快速比较一个扇区或者多个扇区的不同机位点风速图或者风向图

方便进行对比分析,为此需要提取对应列信息,并进行重排以及绘图工作

技术分解

  1. 遍历
  2. 提取信息
  3. 绘图 matplotlib.pyplot模块用于作图
  4. 根据数据表提取信息,并根据表间转换提取数组中的index,设置count和newcount【妙用】参照表(according_table)是实际需要的X轴信息, x_axix是当前风速风向表的信息, 该值需要根据参照表进行调整

技术实现

原先的x轴

x_axix=["T12", "T11", "T10", "T1", "T3", "T2", "T4", "T5", "T9", "T8", "T7", "T6", "T13", "T14", "T15", "T16", "T17", "T23", "T22", "T21", "T20", "T19", "T18", "table_387"];

新的x轴

accordingTable=[ "T1","T2", "T3", "T4", "T5", "T6","T7","T8", "T9", "T10","T11","T12",  "T13", "T14", "T15", "T16", "T17","T18","T19","T20","T21","T22",  "T23", "table_387"];

x轴显示信息

x_axisChange=[ "1","2", "3", "4", "5", "6","7","8", "9", "10","11","12",  "13", "14", "15", "16", "17","18","19","20","21","22",  "23", "Clim"];

需要提取的扇区和物理量

plotSector='22'
plotPhysics='Velocity'

并通过数组的index函数得到对应你的sectorID或者类似的TurbineID等。

通过numpy创建两个空数组

windsimYaxis=np.zeros(x_axix.__len__())
aiwindYaxis=np.zeros(x_axix.__len__())

最终实现

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

columnMe = ['0', '22', '45', '67', '90', '112', '135', '157', '180', '202', '225', '247', '270', '292', '315', '337'];
indexMe = ["T12_Velocity", "T12_Angle", "T11_Velocity", "T11_Angle", "T10_Velocity", "T10_Angle", "T1_Velocity",
           "T1_Angle",
           "T3_Velocity", "T3_Angle", "T2_Velocity", "T2_Angle", "T4_Velocity", "T4_Angle", "T5_Velocity", "T5_Angle",
           "T9_Velocity", "T9_Angle", "T8_Velocity", "T8_Angle", "T7_Velocity", "T7_Angle", "T6_Velocity", "T6_Angle",
           "T13_Velocity", "T13_Angle", "T14_Velocity", "T14_Angle", "T15_Velocity", "T15_Angle", "T16_Velocity",
           "T16_Angle",
           "T17_Velocity", "T17_Angle", "T23_Velocity", "T23_Angle", "T22_Velocity", "T22_Angle", "T21_Velocity",
           "T21_Angle",
           "T20_Velocity", "T20_Angle", "T19_Velocity", "T19_Angle", "T18_Velocity", "T18_Angle",
           "HL387_24035001_Velocity",
           "HL387_24035001_Angle"]


asd = pd.read_csv(r'C:\Users\yezhaoliang\PythonAIWind\AIWINDspeedTable.csv', skiprows=1)
wsd = pd.read_csv(r'C:\Users\yezhaoliang\PythonAIWind\Results-WindSimSpeedTableZhuFengxiagn.csv', skiprows=0)

x_axix=["T12", "T11", "T10", "T1", "T3", "T2", "T4", "T5", "T9", "T8", "T7", "T6", "T13", "T14", "T15", "T16", "T17", "T23", "T22", "T21", "T20", "T19", "T18", "table_387"];
windsimYaxis=np.zeros(x_axix.__len__())
aiwindYaxis=np.zeros(x_axix.__len__())
accordingTable=[ "T1","T2", "T3", "T4", "T5", "T6","T7","T8", "T9", "T10","T11","T12",  "T13", "T14", "T15", "T16", "T17","T18","T19","T20","T21","T22",  "T23", "table_387"];

x_axisChange=[ "1","2", "3", "4", "5", "6","7","8", "9", "10","11","12",  "13", "14", "15", "16", "17","18","19","20","21","22",  "23", "Clim"];


plotSector='22'
plotPhysics='Velocity'
#plotPhysics='Angle'
sectorId=columnMe.index(plotSector)+1
count=0
plotId=0
newCount=0
# for turbine in x_axix:
for turbine in accordingTable:
    if(plotPhysics=='Velocity'):
        plotId=0
    else:
        plotId=1
    # windsimYaxis[count]=wsd.iloc[2*count+plotId,sectorId]
    # aiwindYaxis[count]=asd.iloc[2*count+plotId,sectorId]
    # count = count+1
    newCount=x_axix.index(turbine)
    windsimYaxis[count]=wsd.iloc[2*newCount+plotId,sectorId]
    aiwindYaxis[count]=asd.iloc[2*newCount+plotId,sectorId]
    count = count+1
tempArray=np.array([windsimYaxis,aiwindYaxis])
ylimMax=tempArray.max()
ylimMin=tempArray.min()
# 开始画图
plt.figure(figsize = (15,8))
plt.title('Sector-'+plotSector+' Analysis',fontsize=32 )
#plt.plot(x_axix, aiwindYaxis, marker='o', color='black', label='AIWIND')
plt.plot(x_axisChange, aiwindYaxis, marker='o',markersize='9', color='red',markerfacecolor='grey',markeredgecolor='black',linestyle='solid', label='AIWIND',linewidth=3)
#plt.plot(sub_axix, test_acys,marker='*', color='red', label='testing accuracy')
#plt.plot(x_axix, windsimYaxis,marker='*', color='blue', label='WindSiM')
plt.plot(x_axisChange, windsimYaxis,marker='*',markersize='9', color='black',markerfacecolor='grey',markeredgecolor='black',label='WindSiM',linewidth=3)
#plt.plot(x_axix, thresholds, color='blue', label='threshold')
plt.legend()  # 显示图例
plt.margins(0)
plt.grid()
plt.ylim(ylimMin-1,ylimMax+1)
plt.xlabel('Turbine Names',fontsize=24 )
plt.ylabel(plotPhysics,fontsize=24 )
plt.tick_params(axis='both',which='major',labelsize=24)
plt.savefig(plotSector+"_"+plotPhysics+".jpg",dpi = 900)
plt.show()

# python 一个折线图绘制多个曲线
# color:曲线颜色,blue,green,red等等
# label:图例,这个参数内容就自定义啦,注意如果写这个参数一定要加上plt.legend(),之后再plt.show()才有有用!
# linestyle:曲线风格,’–’,’-.’,’:’等等
# linewidth:曲线宽度,自定义就可以
# marker:标记点样式,’o’,’x’,也就是说这些符号会标示出曲线上具体的“点”,这样一来就易于观察曲线上那些地方是支撑点
# markersize:标记点的大小,自定义就可以

改为批处理版本

增加一下for循环,通过ColumnMe,用于提取section

for plotSector in columnMe:
    #plotPhysics='Angle'
    sectorId=columnMe.index(plotSector)+1
    count=0
    plotId=0
    newCount=0
    # for turbine in x_axix:
    for turbine in accordingTable:
        #if(plotPhysics=='Velocity'):
        if (plotPhysics == 'Velocity'):
                plotId=0
        else:
            plotId=1
        # windsimYaxis[count]=wsd.iloc[2*count+plotId,sectorId]
        # aiwindYaxis[count]=asd.iloc[2*count+plotId,sectorId]
        # count = count+1
        newCount=x_axix.index(turbine)
        windsimYaxis[count]=wsd.iloc[2*newCount+plotId,sectorId]
        aiwindYaxis[count]=asd.iloc[2*newCount+plotId,sectorId]
        count = count+1
    tempArray=np.array([windsimYaxis,aiwindYaxis])
    ylimMax=tempArray.max()
....

最终结果

曲线数据导出

数据导出方便进行比较。 使用了pandas的concat的功能,注意axis的用法,默认是行记录导入,axis=1相当于列记录导入.

    sdf=pd.concat([pd.Series(x_axisChange),pd.Series(windsimYaxis),pd.Series(aiwindYaxis)],axis=1)
    exportFilename=plotSector+"-"+plotPhysics+".xlsx"
    sdf.to_excel(exportFilename)

Related
叶昭良
叶昭良
Engineer of offshore wind turbine technique research

My research interests include distributed energy, wind turbine power generation technique , Computational fluid dynamic and programmable matter.