Matplotlib Basemap tutorial 04 : using inside_poly() to select data !

# remember to define the ax instance :

ax = plt.subplot(111)

zone = "Roetgen - Monschau"
x,y = m(6.15,50.41)
x2,y2 = m(6.59,50.67)
x3,y3 = m(6.34,50.855)
x4,y4 = m(5.83,50.65)
data = np.array(([x,x2,x3,x4], [y,y2,y3,y4]))
p = Polygon(data.T,edgecolor='red',linewidth=1.5,facecolor='none')
ax.add_artist(p)

Note that we have an array containing the points already projected using m(x,y) : basemapped_points

x,y = m(data[:,0],data[:,1])
data[:,0]=x
data[:,1]=y
basemapped_points = np.array((X,Y)).T

then, you can query your arrays for items inside the poly :

from matplotlib.mlab import inside_poly
a = inside_poly(basemapped_points,p.xy)
selection = basemapped_points[a]
m.scatter(selection[:,0],selection[:,1],marker='d',facecolor='none',s=300)

Leave a Reply

Your email address will not be published. Required fields are marked *

*