0

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!?

ErikaAWT
  • 67
  • 2
  • 14
  • It looks like `qplt.plot()` gets unexpected type of data: you're providing it lists but it expects something else (an object with a `'ndim'` attribute: a numpy `ndarray`?). – zezollo Oct 03 '17 at 11:19
  • Thank you for your response @zezollo, I am new to all this, could you explain what I should do to fix it then? If it wants an ndarray, do I change my AverageX variable to equal n.ndarray() and if so what do I put in the brackets to get x coordinates that are months? – ErikaAWT Oct 03 '17 at 11:27
  • I don't know this exactly, I don't know iris at all. The doc of `plot()` (http://scitools.org.uk/iris/docs/latest/iris/iris/plot.html#iris.plot.plot) tells its arguments should be cubes or coordinates (what matches your errors). Cubes: http://scitools.org.uk/iris/docs/latest/iris/iris/coords.html#iris.coords.Coord Coordinates: http://scitools.org.uk/iris/docs/latest/iris/iris/cube.html#iris.cube.Cube. So, your data should certainly be used to build such objects before being given to `qplt.plot()`. – zezollo Oct 03 '17 at 12:27
  • The `quickplot` functions are all wrappers of `matplotlib.pyplot` functions. So if you have lists or numpy arrays instead of Iris cubes, you should just be able to use the pyplot function directly. In this case: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot – RuthC Oct 04 '17 at 12:25
  • Thanks Ruth. I have tried changing it to `AverageY = (CCCmaYR_mean.data + CLMcomYR_mean.data + DMIYR_mean.data + KNMIYR_mean.data + MPIYR_mean.data + SMHIYR_mean.data)/6.` `AverageX = np.arange(0,12,1)` and `plot(AverageX, AverageY, label='Average', lw=1.5, color='black')` I have also added `import matplotlib.pyplot.plot as plot` but I guess this isn't right as I get the following error: `No module named plot` do you know what I should be importing instead? Will it still plot on the same graph if not using qplot? – ErikaAWT Oct 04 '17 at 16:23
  • From your code, it looks like you already have `import matplotlib.pyplot as plt` somewhere. If so, you just need `plt.plot(....)`. – RuthC Oct 05 '17 at 12:02
  • Thank you @RuthC That worked well! :) – ErikaAWT Oct 10 '17 at 10:35

1 Answers1

0

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')

ErikaAWT
  • 67
  • 2
  • 14