Matthijs Kramer Matthijs Kramer - 1 month ago 4
Python Question

Easier way to extract points from data?

I am analysing the output of a weather model, using the python module netCDF4 and makeing images with mpl_toolkits Basemap.

I made some contour plots of air pressure, which was quite easy. Now I want to add barbs for the wind, and ended up with a plot with only wind over the seas. After some fiddling around, I discovered that the basemap function fillcontinents() actually overlays these wind barbs, even though the barbs function is called later. So, removing the fillcontinents() function gave all wind barbs as wanted.

Is there a way to have filled continents, but still have all my barbs in the plot?

My code looks somewhat like this (some graphical parameters I've left out, they are not important for my question):

from netCDF4 import Dataset
from mpl_toolkits.basemap import Basemap
import numpy as np

file = 'W:/projects/wrfout_d01_2013-10-27_120000'
data = Dataset(file)

# Load grid
longrid = data.variables['XLONG'][:].squeeze() # Removes time dimension
latgrid = data.variables['XLAT'][:].squeeze()

# Load surface pressure
p_surf = data.variables['PSFC'][:].squeeze()

# Load wind components
u = data.variables['U10'][:].squeeze()
v = data.variables['V10'][:].squeeze()

m = Basemap(projection='cyl', resolution='l',
llcrnrlat= 42., llcrnrlon= -12.,
urcrnrlat= 57., urcrnrlon= 12.)
m.drawcountries()
m.drawcoastlines()
m.fillcontinents('#FAF0E6') ### Blanking out this line gives barbs over land
m.drawmeridians(np.arange(-180,180,10),labels=[0,0,1,1])
m.drawparallels(np.arange(-90,90,10),labels=[1,0,0,0])

x,y = m(longrid,latgrid)
ct = m.contour(x,y,p_surf, colors='#1C8CFC') ### So far, so good
ct.clabel(fmt='%1i')

m.barbs(x,y,u,v)

plt.savefig('p_surf-barbs-map.png')

Answer

With the help of Paul I found the solution:

when plotting the barbs, add a variable zorder with a high value (I used 100) like this:

m.barbs(x, y, u, v, zorder=100)
Comments