3

I am working some meteorological data to plot contour lines on a basemap. The full working example code I have done earlier is here How to remove/omit smaller contour lines using matplotlib. All works fine and I don’t complain with the contour plot. However there is a special case that I have to hide all contour lines over a specific region (irregular lat & lon) on a Basemap.

The only possible solution I can think of is to draw a ploygon lines over a desired region and fill with the color of same as Basemap. After lot of search I found this link How to draw rectangles on a Basemap (code below)

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', alpha=0.4 )
    plt.gca().add_patch(poly)

lats = [ -30, 30, 30, -30 ]
lons = [ -50, -50, 50, 50 ]

m = Basemap(projection='sinu',lon_0=0)
m.drawcoastlines()
m.drawmapboundary()
draw_screen_poly( lats, lons, m )

plt.show()

It seems to work partially. However, I want to draw a region which is irregular.

Any solution is appreciated.

Edit: 1

I have understood where the problem is. It seems that any colour (facecolor) filled within the polygon region does not make it hide anything below. Always it is transparent only, irrespective of alpha value used or not. To illustrate the problem, I have cropped the image which has all three regions ie. contour, basemap region and polygon region. Polygon region is filled with red colour but as you can see, the contour lines are always visible. The particular line I have used in the above code is :-

poly = Polygon(xy, facecolor='red', edgecolor='b')

Therefore the problem is not with the code above. It seem the problem with the polygon fill. But still no solution for this issue. The resulting image (cropped image) is below (See my 2nd edit below the attached image):-

enter image description here

Edit 2: Taking clue from this http://matplotlib.1069221.n5.nabble.com/Clipping-a-plot-inside-a-polygon-td41950.html which has the similar requirement of mine, I am able to remove some the data. However, the removed data is only from outside of polygon region instead of within. Here is the code I have taken clue from:-

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon

data = np.arange(100).reshape(10, 10)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.contourf(data)
poly = RegularPolygon([ 0.5,  0.5], 6, 0.4, fc='none', 
                      ec='k', transform=ax.transAxes)
for artist in ax.get_children():
    artist.set_clip_path(poly)

Now my question is that what command is used for removing the data within the polygon region?

Community
  • 1
  • 1
sundar_ima
  • 3,604
  • 8
  • 33
  • 52
  • handing in lats/lons an the polygon vertexes does not work? – tacaswell Apr 08 '16 at 19:26
  • Sorry for the delay. It works after providing all the lat & lon. However, the basemap colour I use is white/ no colour. When I give give polygon colour as `white` using this command `poly = Polygon(xy, facecolor='white')` then it does not hide the data/ contour lines behind it. Basically `white` colour act as a transparent. The code works but the issue is with the colour now. – sundar_ima Apr 10 '16 at 02:26
  • I have updated my question with screen shot. – sundar_ima Apr 10 '16 at 15:22

1 Answers1

3

Didn't noticed there was a claim on this so I might just give the solution already proposed here. You can tinker with the zorder to hide stuff behind your polygon:

import matplotlib
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)


# Create a simple contour plot with labels using default colors.  The
# inline argument to clabel will control whether the labels are draw
# over the line segments of the contour, removing the lines beneath
# the label
fig = plt.figure()
ax = fig.add_subplot(111)
CS = plt.contour(X, Y, Z,zorder=3)
plt.clabel(CS, inline=1, fontsize=10)
plt.title('Simplest default with labels')

rect1 = matplotlib.patches.Rectangle((0,0), 2, 1, color='white',zorder=5)

ax.add_patch(rect1)

plt.show()

, the result is:

Hiding contour line with polygon

armatita
  • 12,825
  • 8
  • 48
  • 49
  • I had started a new thread as there was no response on this thread. Anyway, I am accepting this answer as well. Enjoy the bounty ;-)). – sundar_ima Apr 13 '16 at 01:21