Python netcdf plot data onto grid
<p>I am using Lat, Lon data and would like to average all the sample_data within a grid cell (say 1km x 1km) uniformly across the whole area, and then plot it similar to this post, but with a basemap, I'm a bit stuck where to start: 
<a href="http://stackoverflow.com/questions/25071968/heatmap-with-text-in-each-cell-with-matplotlibs-pyplot/25074150#25074150">Heatmap with text in each cell with matplotlib&#39;s pyplot</a></p>

<p>The code below plots values through each time point, and I'd like to average the data within defined grid squares across the whole data area at each time point, and plot the average value onto the basemap with a grid at set time intervals (ie. make a set of images for a timelapse/movie). </p>

<pre><code>import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from netCDF4 import Dataset
import matplotlib.cm as cm

data = Dataset(netcdf_data,'r')

lat = data.variables['lat'][:]
lon = data.variables['lon'][:]
time = data.variables['time'][:]


fig = plt.figure(figsize=(1800,1200))
m = Basemap(projection='ortho',lon_0=5,lat_0=35,resolution='l')
m.drawcoastlines()


for value in range(0,len(sample_data)):
    m.plot(lat[:,value], lon[:,value], alpha=1, latlon=True)

plt.show()
</code></pre>

<p>This is a crude way of doing it:</p>

<pre><code>lat_1 = 58
lat_2 = 60
lon_1 = 2
lon_2 = 5
size = 0 
age2 = 0 

for parts in range(10):
    for length in range(len(lon)):
        if lon[length,parts] &lt; lon_2:
            if lon[length,parts] &gt; lon_1:
                if lat[length,parts] &gt; lat_1:
                    if lat[length,parts] &lt; lat_2:
                        age = (age[length, parts])
                        size = size + 1
                        age2 = age + age2
                        mean = age2/size
</code></pre>

python matplotlib grid netcdf