Search code examples
pythonpython-2.7python-iris

Python Iris plot average NetCDF data


I am trying to plot the outputs of some NetCDF files as well as the average of all of them. I have successfully plotted the the NetCDF files themselves, as follows (where CCCmaYR_mean, CLMcomYR_mean, DMIYR_mean, KNMIYR_mean, MPIYR_mean, SMHIYR_mean, CRUYR_mean and UDelYR_mean are all pre-defined NetCDF files)

#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() 

Which gives me the following graph: enter image description here

To create the average I have added this code:

 ##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']

and then included the following with the other qplot.plot code shown above.

qplt.plot(AverageX, AverageY, label='AverageY', lw=1.5, color='black')

This gives me the following error:

AttributeError: 'list' object has no attribute 'ndim'

I have also tried defining AverageX as follows:

AverageX = np.arange(0,12,1)

Which gives this error:

TypeError: Plot arguments must be cubes or coordinates.

I'm sure I am doing something really silly, but can someone tell me what it is!?


Solution

  • Answer from @RuthC in comments above.

    Fixed it by adding in

    import numpy as np

    and the defining my Y data as: AverageY = (CCCmaYR_mean.data + CLMcomYR_mean.data + DMIYR_mean.data + KNMIYR_mean.data + MPIYR_mean.data + SMHIYR_mean.data)/6.

    and my X data as: AverageX = np.arange(1,13,1)

    Then plotting it as: plt.plot(AverageX, AverageY, label='Average', lw=1.5, color='black')