F L
F L

Reputation: 183

Plotting an average

I want to plot an average of 24 arrays, but had an error message ''list' object has no attribute 'shape''. Where's the mistake?

empty_array=numpy.zeros[2,30,100]
for x in range (1,25):
    array = array[x,:,:,:]
    empty_array += array
average = map(lambda x:x/24,empty_array)

plt.figure(1)
plt.pcolor(average)
plt.colorbar(orientation='horizontal')                 

Upvotes: 0

Views: 78

Answers (2)

Martin Hallén
Martin Hallén

Reputation: 1552

It looks like you want to compute the average in one of the axis. You can simply use np.mean() for this. Example from docs:

>>> a = np.array([[1, 2], [3, 4]])
>>> np.mean(a)
2.5
>>> np.mean(a, axis=0)
array([ 2.,  3.])
>>> np.mean(a, axis=1)
array([ 1.5,  3.5])

The key here is to correctly define the axis you want to average over.

Other mistakes:

  • np.zeros() take a tuple describing the shape as an input, i.e. np.zeros((2,30,100)).
  • The reason you get "'list' object has no attribute 'shape'" is that average is a python list. Lists in python has no attribute named shape. plt.pcolor() expect a 2-D (numpy) array.

Upvotes: 1

Ian
Ian

Reputation: 1716

empty_array=[2,30,100] does not create an empty array. It is a list containing three numbers. You are looking for numpy.zeros

Upvotes: 1

Related Questions