[英]Python Iris plot average NetCDF data
我試圖繪制一些NetCDF文件的輸出以及所有文件的平均值。 我已經成功繪制了NetCDF文件本身,如下所示(其中CCCmaYR_mean,CLMcomYR_mean,DMIYR_mean,KNMIYR_mean,MPIYR_mean,SMHIYR_mean,CRUYR_mean和UDelYR_mean都是預定義的NetCDF文件)
#PART 4: PLOT LINE GRAPH
#set x-axis ticks
plt.xticks(range(12), calendar.month_abbr[0:12])
#assign the line colours and set x axis to 'month' rather than 'time'
qplt.plot(CCCmaYR_mean.coord('month_number'), CCCmaYR_mean, label='CanRCM4_ERAINT', lw=1.5, color='blue')
qplt.plot(CLMcomYR_mean.coord('month_number'), CLMcomYR_mean, label='CCLM4-8-17_ERAINT', lw=1.5, color='green')
qplt.plot(DMIYR_mean.coord('month_number'), DMIYR_mean, label='HIRHAM5_ERAINT', lw=1.5, color='red')
qplt.plot(KNMIYR_mean.coord('month_number'), KNMIYR_mean, label='RACMO22T_ERAINT', lw=1.5, color='cyan')
qplt.plot(MPIYR_mean.coord('month_number'), MPIYR_mean, label='REMO2009_ERAINT', lw=1.5, color='magenta')
qplt.plot(SMHIYR_mean.coord('month_number'), SMHIYR_mean, label='RCA4_ERAINT', lw=1.5, color='yellow')
qplt.plot(CRUYR_mean.coord('month_number'), CRUYR_mean, label='Observed_CRU', lw=2, color='grey')
qplt.plot(UDelYR_mean.coord('month_number'), UDelYR_mean, label='Observed UDel', lw=2, color='grey', linestyle = '--')
#set a title for the y axis
plt.ylabel('Near-Surface Temperature (degrees Celsius)')
#create a legend and set its location to under the graph
plt.legend(loc="upper center", bbox_to_anchor=(0.5,-0.05), fancybox=True, shadow=True, ncol=2)
#create a title
plt.title('Mean Near Surface Temperature for Malawi by Month 1990-2008', fontsize=11)
#add grid lines
plt.grid()
#save the image of the graph and include full legend
#plt.savefig('ERAINT_Temperature_LineGraph_Monthly', bbox_inches='tight')
#show the graph in the console
iplt.show()
為了創建平均值,我添加了以下代碼:
##Create an average CORDEX and some x coordinates
AverageY = (CCCmaYR_mean.data + CLMcomYR_mean.data + DMIYR_mean.data + KNMIYR_mean.data + MPIYR_mean.data + SMHIYR_mean.data)/6.
AverageX = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
然后將以下內容與上面顯示的其他qplot.plot代碼一起包含。
qplt.plot(AverageX, AverageY, label='AverageY', lw=1.5, color='black')
這給了我以下錯誤:
AttributeError: 'list' object has no attribute 'ndim'
我也嘗試過如下定義AverageX:
AverageX = np.arange(0,12,1)
這給出了這個錯誤:
TypeError: Plot arguments must be cubes or coordinates.
我確定我做的事情確實很傻,但是有人可以告訴我這是什么!
@RuthC在上面的評論中回答。
通過添加來修復它
將numpy導入為np
並將我的Y數據定義為:AverageY =(CCCmaYR_mean.data + CLMcomYR_mean.data + DMIYR_mean.data + KNMIYR_mean.data + MPIYR_mean.data + SMHIYR_mean.data)/ 6。
我的X數據為:AverageX = np.arange(1,13,1)
然后將其繪制為:plt.plot(AverageX,AverageY,label ='Average',lw = 1.5,color ='black')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.