风玫瑰图的绘制
这两天研究了下python中风玫瑰图绘制,有几种方法,比较方便的是引用windrose模块。
数据的获取,从气象A文件中获取,为计算年数据将每年A文件放入文件夹,首先放入效果图。
代码如下:
#Author:Wu Dongqiao#.6.6import osimport calendarimport datetimeimport numpy as npfrom matplotlib import pyplot as pltimport matplotlib as mplfrom windrose import WindroseAxesmpl.rcParams['font.sans-serif'] = ['SimHei'] #解决中文显示的问题#定义读取A文件中风数据def read_Fdata(file_path):yyyy = int(file_path[-10:][0:4])mm = int(file_path[-6:][0:2])dataline = []data = []line = []with open(file_path) as fp:# 读到文件尾再退出dates = calendar.monthrange(int(yyyy),int(mm))[1] #计算某月的天数while True:line = fp.readline()if not line:breakif line[:2] == 'FN':for i in range(4*dates):dataline = fp.readline()[:-1].split(' ')data = np.append(data,np.array(dataline), axis=0)breakreturn data#读多个A文件后拼接数据Fdata,ws,wd=[],[],[]for root,dirs,files in os.walk(r'D:\data\afile\'): #数据A文件放入文件夹的地址for tempFile in files:flag = tempFile[-17]if flag=='J':continueyyyy=int(tempFile[-10:-6])path = os.path.join(root, tempFile)print(path)tempdata=read_Fdata(path)Fdata=np.append(Fdata,tempdata,axis=0)#含缺测和静风的风向风速wd=[i[:3] for i in Fdata]ws=[i[3:6] for i in Fdata]#算静风率C=np.round(wd.count('PPC')/len(wd),3)C=str(np.round(C*100,1))+ '%'#删除数组中的缺测和静风,删除np字符串数组中非数值型数组的方法list1=list(Fdata)def filter_fun(x):try:return isinstance(int(x[:6]),(int))except:return FalseFdata=np.array(list(filter(filter_fun,list1)))#去掉静风和缺测后的风向风速wd=[int(i[:3]) for i in Fdata]ws=[int(i[3:6])/10 for i in Fdata]#画图,前面的部分都是为了提取出风向和风速的序列ax = WindroseAxes.from_ax()ax.bar(wd, ws, normed=True, opening=0.8, edgecolor='white')ax.set_legend()title=str(yyyy)+'年风玫瑰图'ax.set_title(title,fontsize=15,loc='center')#ax.text(0.7,0.7,'静风频率:'+ C) #根据需要填加显示静风率plt.savefig(str(yyyy)+'年风玫瑰图.tiff')plt.show()