Basemap: adding a polygon covers up scatter plot on map

Hi,

I’m running into a problem with overlaying a scatter plot on a polygon that is on a Basemap. The polygon covers up the scatter plot created by the basemap. To show the issue, the simple example below and broken down into three steps: 1) creating the map, 2) adding the red polygon, and 3) adding the “x” markers via a scatter plot. You will see that the “x” markers are not on top of the polygon.

Why does the polygon cover up the markers, even though I have the markers added after the polygon? Would there be a better way to do this? I could set the polygon alpha to, say, 0.5, but this feature does not show when I save it as an eps image. Therefore, I would like to keep alpha=1.

Thanks,

Tim

from mpl_toolkits.basemap import Basemap

import numpy as np

from matplotlib.pyplot import *

from matplotlib.patches import Polygon

---------------------

Part 1: Draw the map

---------------------

Hawaii:

lat_0 = 20.71-8.

lon_0 = 203.83

figure(1); clf();

m = Basemap(width=2500e3,height=2500e3,

    resolution='l',projection='stere', \

            lat_ts=lat_0,lat_0=lat_0,lon_0=lon_0)

m.drawcoastlines()

m.fillcontinents(color=‘coral’,lake_color=‘aqua’)

draw parallels and meridians:

m.drawmapboundary(fill_color=‘aqua’)

lats = np.arange(np.floor(m.latmin),np.ceil(m.latmax),2.)

lons = np.arange(190.,211.,5.)

m.drawparallels(lats,labels=[True,False,False,False])

m.drawmeridians(lons,labels=[False,False,False,True])

draw(); show()

---------------------

Part 2: Add a polygon

---------------------

lon_poly = np.array([-160., -150., -150., -160.,])

lat_poly = np.array([10., 10., 14., 14.,])

X, Y = m(lon_poly, lat_poly)

xy = np.vstack([X,Y]).T

poly = Polygon(xy, closed=True, \

    facecolor='red', \

    linewidth=1., \

    )

gca().add_patch(poly)

---------------------

Part 3: add some ‘x’ markers

---------------------

lon_markers = np.arange(-168.,-144.,2.)

lat_markers = np.arange(5.,20.,1.)

X, Y = np.meshgrid(lon_markers, lat_markers)

x, y = m(X.ravel(), Y.ravel())

m.scatter(x,y,marker=‘x’)

draw(); show()

Hi,

I'm running into a problem with overlaying a scatter plot on a polygon
that is on a Basemap. The polygon covers up the scatter plot created by
the basemap. To show the issue, the simple example below and broken
down into three steps: 1) creating the map, 2) adding the red polygon,
and 3) adding the "x" markers via a scatter plot. You will see that the
"x" markers are not on top of the polygon.

Why does the polygon cover up the markers, even though I have the
markers added after the polygon? Would there be a better way to do
this? I could set the polygon alpha to, say, 0.5, but this feature does
not show when I save it as an eps image. Therefore, I would like to
keep alpha=1.

Artists have a zorder attribute that controls the drawing order. Try adding the kwarg zorder=4 to your scatter call. I think that will take care of it. (Alternatively you could lower the zorder of the polygon.)

Eric

···

On 2012/12/11 12:16 PM, Timothy Duly wrote:

Thanks,
Tim

from mpl_toolkits.basemap import Basemap
import numpy as np
from matplotlib.pyplot import *
from matplotlib.patches import Polygon

# ---------------------
# Part 1: Draw the map
# ---------------------

# Hawaii:
lat_0 = 20.71-8.
lon_0 = 203.83

figure(1); clf();
m = Basemap(width=2500e3,height=2500e3,
         resolution='l',projection='stere', \
                 lat_ts=lat_0,lat_0=lat_0,lon_0=lon_0)

m.drawcoastlines()
m.fillcontinents(color='coral',lake_color='aqua')

# draw parallels and meridians:
m.drawmapboundary(fill_color='aqua')

lats = np.arange(np.floor(m.latmin),np.ceil(m.latmax),2.)
lons = np.arange(190.,211.,5.)
m.drawparallels(lats,labels=[True,False,False,False])
m.drawmeridians(lons,labels=[False,False,False,True])

draw(); show()

# ---------------------
# Part 2: Add a polygon
# ---------------------

lon_poly = np.array([-160., -150., -150., -160.,])
lat_poly = np.array([10., 10., 14., 14.,])
X, Y = m(lon_poly, lat_poly)
xy = np.vstack([X,Y]).T

poly = Polygon(xy, closed=True, \
         facecolor='red', \
         linewidth=1., \
         )

gca().add_patch(poly)

# ---------------------
# Part 3: add some 'x' markers
# ---------------------

lon_markers = np.arange(-168.,-144.,2.)
lat_markers = np.arange(5.,20.,1.)
X, Y = np.meshgrid(lon_markers, lat_markers)

x, y = m(X.ravel(), Y.ravel())
m.scatter(x,y,marker='x')

draw(); show()

------------------------------------------------------------------------------
LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial
Remotely access PCs and mobile devices and provide instant support
Improve your efficiency, and focus on delivering more value-add services
Discover what IT Professionals Know. Rescue delivers
http://p.sf.net/sfu/logmein_12329d2d

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Thanks Eric, this did the trick. I did not know such a thing existed. Why “4”?

Tim

···

On Tue, Dec 11, 2012 at 4:35 PM, Eric Firing <efiring@…202…> wrote:

On 2012/12/11 12:16 PM, Timothy Duly wrote:

Hi,

I’m running into a problem with overlaying a scatter plot on a polygon

that is on a Basemap. The polygon covers up the scatter plot created by

the basemap. To show the issue, the simple example below and broken

down into three steps: 1) creating the map, 2) adding the red polygon,

and 3) adding the “x” markers via a scatter plot. You will see that the

“x” markers are not on top of the polygon.

Why does the polygon cover up the markers, even though I have the

markers added after the polygon? Would there be a better way to do

this? I could set the polygon alpha to, say, 0.5, but this feature does

not show when I save it as an eps image. Therefore, I would like to

keep alpha=1.

Artists have a zorder attribute that controls the drawing order. Try

adding the kwarg zorder=4 to your scatter call. I think that will take

care of it. (Alternatively you could lower the zorder of the polygon.)

Eric

Thanks,

Tim

from mpl_toolkits.basemap import Basemap

import numpy as np

from matplotlib.pyplot import *

from matplotlib.patches import Polygon

---------------------

Part 1: Draw the map

---------------------

Hawaii:

lat_0 = 20.71-8.

lon_0 = 203.83

figure(1); clf();

m = Basemap(width=2500e3,height=2500e3,

     resolution='l',projection='stere', \
             lat_ts=lat_0,lat_0=lat_0,lon_0=lon_0)

m.drawcoastlines()

m.fillcontinents(color=‘coral’,lake_color=‘aqua’)

draw parallels and meridians:

m.drawmapboundary(fill_color=‘aqua’)

lats = np.arange(np.floor(m.latmin),np.ceil(m.latmax),2.)

lons = np.arange(190.,211.,5.)

m.drawparallels(lats,labels=[True,False,False,False])

m.drawmeridians(lons,labels=[False,False,False,True])

draw(); show()

---------------------

Part 2: Add a polygon

---------------------

lon_poly = np.array([-160., -150., -150., -160.,])

lat_poly = np.array([10., 10., 14., 14.,])

X, Y = m(lon_poly, lat_poly)

xy = np.vstack([X,Y]).T

poly = Polygon(xy, closed=True, \

     facecolor='red', \
     linewidth=1., \
     )

gca().add_patch(poly)

---------------------

Part 3: add some ‘x’ markers

---------------------

lon_markers = np.arange(-168.,-144.,2.)

lat_markers = np.arange(5.,20.,1.)

X, Y = np.meshgrid(lon_markers, lat_markers)

x, y = m(X.ravel(), Y.ravel())

m.scatter(x,y,marker=‘x’)

draw(); show()


LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial

Remotely access PCs and mobile devices and provide instant support

Improve your efficiency, and focus on delivering more value-add services

Discover what IT Professionals Know. Rescue delivers

http://p.sf.net/sfu/logmein_12329d2d


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users


LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial

Remotely access PCs and mobile devices and provide instant support

Improve your efficiency, and focus on delivering more value-add services

Discover what IT Professionals Know. Rescue delivers

http://p.sf.net/sfu/logmein_12329d2d


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Thanks Eric, this did the trick. I did not know such a thing existed.
  Why "4"?

I don't remember exactly what the defaults are for different types of artist, and didn't want to take the time to look, but I think they are all less than 4. So I just picked that as a value that would do the job.

Eric

···

On 2012/12/11 1:24 PM, Timothy Duly wrote:

Tim

On Tue, Dec 11, 2012 at 4:35 PM, Eric Firing <efiring@...202... > <mailto:efiring@…202…>> wrote:

    On 2012/12/11 12:16 PM, Timothy Duly wrote:
     > Hi,
     >
     > I'm running into a problem with overlaying a scatter plot on a
    polygon
     > that is on a Basemap. The polygon covers up the scatter plot
    created by
     > the basemap. To show the issue, the simple example below and broken
     > down into three steps: 1) creating the map, 2) adding the red
    polygon,
     > and 3) adding the "x" markers via a scatter plot. You will see
    that the
     > "x" markers are not on top of the polygon.
     >
     > Why does the polygon cover up the markers, even though I have the
     > markers added after the polygon? Would there be a better way to do
     > this? I could set the polygon alpha to, say, 0.5, but this
    feature does
     > not show when I save it as an eps image. Therefore, I would like to
     > keep alpha=1.

    Artists have a zorder attribute that controls the drawing order. Try
    adding the kwarg zorder=4 to your scatter call. I think that will take
    care of it. (Alternatively you could lower the zorder of the polygon.)

    Eric

     >
     > Thanks,
     > Tim
     >
     > from mpl_toolkits.basemap import Basemap
     > import numpy as np
     > from matplotlib.pyplot import *
     > from matplotlib.patches import Polygon
     >
     > # ---------------------
     > # Part 1: Draw the map
     > # ---------------------
     >
     > # Hawaii:
     > lat_0 = 20.71-8.
     > lon_0 = 203.83
     >
     > figure(1); clf();
     > m = Basemap(width=2500e3,height=2500e3,
     > resolution='l',projection='stere', \
     > lat_ts=lat_0,lat_0=lat_0,lon_0=lon_0)
     >
     > m.drawcoastlines()
     > m.fillcontinents(color='coral',lake_color='aqua')
     >
     > # draw parallels and meridians:
     > m.drawmapboundary(fill_color='aqua')
     >
     > lats = np.arange(np.floor(m.latmin),np.ceil(m.latmax),2.)
     > lons = np.arange(190.,211.,5.)
     > m.drawparallels(lats,labels=[True,False,False,False])
     > m.drawmeridians(lons,labels=[False,False,False,True])
     >
     > draw(); show()
     >
     > # ---------------------
     > # Part 2: Add a polygon
     > # ---------------------
     >
     > lon_poly = np.array([-160., -150., -150., -160.,])
     > lat_poly = np.array([10., 10., 14., 14.,])
     > X, Y = m(lon_poly, lat_poly)
     > xy = np.vstack([X,Y]).T
     >
     > poly = Polygon(xy, closed=True, \
     > facecolor='red', \
     > linewidth=1., \
     > )
     >
     > gca().add_patch(poly)
     >
     > # ---------------------
     > # Part 3: add some 'x' markers
     > # ---------------------
     >
     > lon_markers = np.arange(-168.,-144.,2.)
     > lat_markers = np.arange(5.,20.,1.)
     > X, Y = np.meshgrid(lon_markers, lat_markers)
     >
     > x, y = m(X.ravel(), Y.ravel())
     > m.scatter(x,y,marker='x')
     >
     > draw(); show()
     >
    ------------------------------------------------------------------------------
     > LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial
     > Remotely access PCs and mobile devices and provide instant support
     > Improve your efficiency, and focus on delivering more value-add
    services
     > Discover what IT Professionals Know. Rescue delivers
     > http://p.sf.net/sfu/logmein_12329d2d
     >
     > _______________________________________________
     > Matplotlib-users mailing list
     > Matplotlib-users@lists.sourceforge.net
    <mailto:Matplotlib-users@lists.sourceforge.net>
     > matplotlib-users List Signup and Options
     >

    ------------------------------------------------------------------------------
    LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial
    Remotely access PCs and mobile devices and provide instant support
    Improve your efficiency, and focus on delivering more value-add services
    Discover what IT Professionals Know. Rescue delivers
    http://p.sf.net/sfu/logmein_12329d2d
    _______________________________________________
    Matplotlib-users mailing list
    Matplotlib-users@lists.sourceforge.net
    <mailto:Matplotlib-users@lists.sourceforge.net>
    matplotlib-users List Signup and Options