I am working with some weather data to plot contour lines on basemap using matplotlib. The data I have used (x, y and data) is uploaded here http://www.mediafire.com/download/0epjjdm8auit611/mslp.txt here http://www.mediafire.com/download/1dn6p8nw96h2mmd/xlong.txt and here http://www.mediafire.com/download/31suzsz6j7u2bgz/xlat.txt. The working example code is below:-
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
m = Basemap(projection='merc', llcrnrlat=7, urcrnrlat=40,
llcrnrlon=68, urcrnrlon=110, resolution='l')
x = np.loadtxt('xlong.txt', delimiter=',')
y = np.loadtxt('xlat.txt', delimiter=',')
Z = np.loadtxt('mslp.txt', delimiter=',')
x, y = m(x, y)
CS = plt.contour(x, y, Z, colors='b')
plt.show()
The above code gives me the following plot...
The plot is absolutely OK. But I would like to hide/ remove contour lines over a particular area. So, I have drawn a polygon over a basemap and tried to hide data beneath the polygon. The code I have used for doing is as follows:-
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
def draw_screen_poly(lats, lons, m):
x, y = m(lons, lats)
xy = zip(x, y)
poly = Polygon(xy, facecolor='red')
plt.gca().add_patch(poly)
lats = [30, 35, 35, 30]
lons = [80, 80, 90, 90]
m = Basemap(projection='merc', llcrnrlat=7, urcrnrlat=40,
llcrnrlon=68, urcrnrlon=110, resolution='l')
x = np.loadtxt('xlong.txt', delimiter=',')
y = np.loadtxt('xlat.txt', delimiter=',')
Z = np.loadtxt('mslp.txt', delimiter=',')
x, y = m(x, y)
CS = plt.contour(x, y, Z, colors='b')
draw_screen_poly(lats, lons, m)
plt.show()
The resulting image is lloking like below. As you can see, there is no effect of facecolour
as it does not hide any data beneath it.
What I want to do is either remove contour lines passing through this polygon or hide/ clip the polygon area using image processing technique.
The solution I think of are:-
1. Apply some white colour to polygon region so that it matches the basemap colour and data is hidden (this is already done in the above example and does not work).
2. Go through each contour from contour collection and check if it is passing through the polygon region. Finally remove it from plot.
3. Finally, Chip off the polygon area.
My mind is not proceeding beyond above ideas. Any solution to solve this issue is highly appreciated.