I try to hatch only the regions where I have statistically significant results. How can I do this using Basemap and pcolormesh?
plt.figure(figsize=(12,12))
lons = iris_cube.coord('longitude').points
lats = iris_cube.coord('latitude').points
m = Basemap(llcrnrlon=lons[0], llcrnrlat=lats[0], urcrnrlon=lons[-1], urcrnrlat=lats[-1], resolution='l')
lon, lat = np.meshgrid(lons, lats)
plt.subplot(111)
cs = m.pcolormesh(lon, lat, significant_data, cmap=cmap, norm=norm, hatch='/')

I have a simple solution for this problem, using only
pcolormeshand notpcolor: Plot the color mesh, then hatch the entire plot, and then plot the original mesh again, this time by masking statistically significant cells, so that the only hatching visible is those on significant cells. Alternatively, you can put a marker on every cell (looks good too), instead of hatching the entire figure.(I use
cartopyinstead ofbasemap, but this shouldn't matter.)Step 1: Plot your field (
z) normally, usingpcolormesh.where x/y can be lons/lats.
Step 2: Hatch the entire plot. For this, use
fill_between:Check details of
fill_betweento setxmin,xmax,y1andy2. You simply define two horizontal lines beyond the bounds of your plot, and hatch the area in between. Use more, or less/s to set hatch density.To adjust hatch thickness, use below lines:
As an alternative to hatching everything, you can plot all your x-y points (or, lon-lat couples) as markers. A simple solution is putting a dot (x also looks good).
One of the above will be the basis of your 'hatch'. This is how it should look after Step 2:
Step 3: On top of these two, plot your color mesh once again with
pcolormesh, this time masking cells containing statistically significant values. This way, the markers on your 'insignificant' cells become invisible again, while significant markers stay visible.Assuming you have an identically sized array containing the t statistic for each cell (
t_z), you can mask significant values usingnumpy'smamodule.Then, plot the color mesh, using the masked array.
Use
zorderto make sure the layers are in correct order. This is how it should look after Step 3: