matplotlib/basemap plot geo data on background map

Hi,
I'm new to matplotlib and I'm looking for an easy way to plot geographical
data on a background map: bkgmap.png
http://old.nabble.com/file/p29679002/bkgmap.png
So far I only found out about warpimage() to do this but only part of
bkgmap.png comes up in the output image. I think this is because this image
has no pixels covering all the world.
http://old.nabble.com/file/p29679002/partialbkg_polygon.png

Any idea on how to insert this image as background?
THANKS

Code: the polygon displays in right position even though background doesn't
show OK (tested with map,lat/lon lines drawn too)

import sys
import Image, ImageDraw # PIL
from matplotlib.patches import Polygon
from mpl_toolkits.basemap import Basemap
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np

lat0=48
lon0=13
lllon=-15
lllat=20
urlon=73
urlat=57
map =
Basemap(projection='stere',lat_0=lat0,lon_0=lon0,llcrnrlon=lllon,llcrnrlat=lllat,urcrnrlon=urlon,urcrnrlat=urlat,
              resolution='c',area_thresh=1000.)
map.warpimage(image='bkgmap.png',scale=None,ax=plt.gca())
# points
lat = [50.,55.,45.,40.,50.]
lon = [-20.,-10.,10.,-10.,-20.]
x0,y0 = map(lon[0],lat[0])
x1,y1 = map(lon[1],lat[1])
x2,y2 = map(lon[2],lat[2])
x3,y3 = map(lon[3],lat[3])
x4,y4 = map(lon[4],lat[4])
plt.gca().add_patch(Polygon([(x0,y0),(x1,y1),(x2,y2),(x3,y3),(x4,y4)],fill=1,facecolor='red',edgecolor='black'))
plt.show()

···

--
View this message in context: http://old.nabble.com/matplotlib-basemap-plot-geo-data-on-background-map-tp29679002p29679002.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

If you’re just looking for points, you can use ax.scatter(). It will plot the points. Also, make sure you set the zorder keyword argument in the scatter.

Example:
x=range(10)
y=range(10)
z=range(10,20)

ax.scatter(x,y,c=z,zorder=10)

Hope this helps,
Aman

···

On Fri, Sep 10, 2010 at 1:06 PM, izzybitsie <isidora@…3269…> wrote:

Hi,

I’m new to matplotlib and I’m looking for an easy way to plot geographical

data on a background map: bkgmap.png

http://old.nabble.com/file/p29679002/bkgmap.png

So far I only found out about warpimage() to do this but only part of

bkgmap.png comes up in the output image. I think this is because this image

has no pixels covering all the world.

http://old.nabble.com/file/p29679002/partialbkg_polygon.png

Any idea on how to insert this image as background?

THANKS

Code: the polygon displays in right position even though background doesn’t

show OK (tested with map,lat/lon lines drawn too)

import sys

import Image, ImageDraw # PIL

from matplotlib.patches import Polygon

from mpl_toolkits.basemap import Basemap

import matplotlib.image as mpimg

import matplotlib.pyplot as plt

import numpy as np

lat0=48

lon0=13

lllon=-15

lllat=20

urlon=73

urlat=57

map =

Basemap(projection=‘stere’,lat_0=lat0,lon_0=lon0,llcrnrlon=lllon,llcrnrlat=lllat,urcrnrlon=urlon,urcrnrlat=urlat,

          resolution='c',area_thresh=1000.)

map.warpimage(image=‘bkgmap.png’,scale=None,ax=plt.gca())

points

lat = [50.,55.,45.,40.,50.]

lon = [-20.,-10.,10.,-10.,-20.]

x0,y0 = map(lon[0],lat[0])

x1,y1 = map(lon[1],lat[1])

x2,y2 = map(lon[2],lat[2])

x3,y3 = map(lon[3],lat[3])

x4,y4 = map(lon[4],lat[4])

plt.gca().add_patch(Polygon([(x0,y0),(x1,y1),(x2,y2),(x3,y3),(x4,y4)],fill=1,facecolor=‘red’,edgecolor=‘black’))

plt.show()

View this message in context: http://old.nabble.com/matplotlib-basemap-plot-geo-data-on-background-map-tp29679002p29679002.html

Sent from the matplotlib - users mailing list archive at Nabble.com.


Start uncovering the many advantages of virtual appliances

and start using them to simplify application deployment and

accelerate your shift to cloud computing

http://p.sf.net/sfu/novell-sfdev2dev


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

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


Aman Thakral
B.Eng & Biosci, M.Eng Design

Hi,
documentation of mpl_toolkits.basemap.warpimage
(http://matplotlib.sourceforge.net/basemap/doc/html/api/basemap_api.html#mpl_toolkits.basemap.Basemap.warpimage)
states that it could only be used with a very specific set of images:
"Specified image must have pixels covering the whole globe in a
regular lat/lon grid, starting and -180W and the South Pole.", so your
image won't work - it is curved, there is no obvious (linear)
transformation from (x,y) to (lat, long). Examples of good images are
given here: NASA Visible Earth - Home

There are easy ways to mimic your picture with native Basemap tools,
like in this examples:
http://matplotlib.sourceforge.net/basemap/doc/html/users/geography.html

But if you would specifically use your own image, the only way I see
is to come up with a transformation function transforming lat and long
coordinates to x,y coordinates of your specific picture. If you would
write such a function yourself than you could easily use ax.plot,
ax.scatter or any other plotting routine to place stuff in correct
place in respect to your picture. Giving (lat, long) of stuff you
would like to draw, than transforming to (x,y) with the function, and
than using those with plotting routines at the same axis you have
place your picture, with for example imshow routine with proper
zorder.

Hope this clears a problem a little bit,
JS

···

On Fri, Sep 10, 2010 at 13:06, izzybitsie <isidora@...3269...> wrote:

Hi,
I'm new to matplotlib and I'm looking for an easy way to plot geographical
data on a background map: bkgmap.png
http://old.nabble.com/file/p29679002/bkgmap.png
So far I only found out about warpimage() to do this but only part of
bkgmap.png comes up in the output image. I think this is because this image
has no pixels covering all the world.
http://old.nabble.com/file/p29679002/partialbkg_polygon.png

Any idea on how to insert this image as background?
THANKS

Code: the polygon displays in right position even though background doesn't
show OK (tested with map,lat/lon lines drawn too)

import sys
import Image, ImageDraw # PIL
from matplotlib.patches import Polygon
from mpl_toolkits.basemap import Basemap
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np

lat0=48
lon0=13
lllon=-15
lllat=20
urlon=73
urlat=57
map =
Basemap(projection='stere',lat_0=lat0,lon_0=lon0,llcrnrlon=lllon,llcrnrlat=lllat,urcrnrlon=urlon,urcrnrlat=urlat,
resolution='c',area_thresh=1000.)
map.warpimage(image='bkgmap.png',scale=None,ax=plt.gca())
# points
lat = [50.,55.,45.,40.,50.]
lon = [-20.,-10.,10.,-10.,-20.]
x0,y0 = map(lon[0],lat[0])
x1,y1 = map(lon[1],lat[1])
x2,y2 = map(lon[2],lat[2])
x3,y3 = map(lon[3],lat[3])
x4,y4 = map(lon[4],lat[4])
plt.gca().add_patch(Polygon([(x0,y0),(x1,y1),(x2,y2),(x3,y3),(x4,y4)],fill=1,facecolor='red',edgecolor='black'))
plt.show()
--
View this message in context: http://old.nabble.com/matplotlib-basemap-plot-geo-data-on-background-map-tp29679002p29679002.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Thanks for your answer. I'm actually looking to plot filled curves whose
points are lat/lon points on top of a given background map. The problem
experienced is misrepresentation of background map when I insert it using
warpimage(), the only function to do this I found so far.

Aman Thakral wrote:

···

If you're just looking for points, you can use ax.scatter(). It will plot
the points. Also, make sure you set the zorder keyword argument in the
scatter.

Example:
x=range(10)
y=range(10)
z=range(10,20)
ax.scatter(x,y,c=z,zorder=10)

Hope this helps,
Aman

On Fri, Sep 10, 2010 at 1:06 PM, izzybitsie <isidora@...3269...> wrote:

Hi,
I'm new to matplotlib and I'm looking for an easy way to plot
geographical
data on a background map: bkgmap.png
http://old.nabble.com/file/p29679002/bkgmap.png
So far I only found out about warpimage() to do this but only part of
bkgmap.png comes up in the output image. I think this is because this
image
has no pixels covering all the world.
http://old.nabble.com/file/p29679002/partialbkg_polygon.png

Any idea on how to insert this image as background?
THANKS

Code: the polygon displays in right position even though background
doesn't
show OK (tested with map,lat/lon lines drawn too)

import sys
import Image, ImageDraw # PIL
from matplotlib.patches import Polygon
from mpl_toolkits.basemap import Basemap
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np

lat0=48
lon0=13
lllon=-15
lllat=20
urlon=73
urlat=57
map =

Basemap(projection='stere',lat_0=lat0,lon_0=lon0,llcrnrlon=lllon,llcrnrlat=lllat,urcrnrlon=urlon,urcrnrlat=urlat,
             resolution='c',area_thresh=1000.)
map.warpimage(image='bkgmap.png',scale=None,ax=plt.gca())
# points
lat = [50.,55.,45.,40.,50.]
lon = [-20.,-10.,10.,-10.,-20.]
x0,y0 = map(lon[0],lat[0])
x1,y1 = map(lon[1],lat[1])
x2,y2 = map(lon[2],lat[2])
x3,y3 = map(lon[3],lat[3])
x4,y4 = map(lon[4],lat[4])

plt.gca().add_patch(Polygon([(x0,y0),(x1,y1),(x2,y2),(x3,y3),(x4,y4)],fill=1,facecolor='red',edgecolor='black'))
plt.show()
--
View this message in context:
http://old.nabble.com/matplotlib-basemap-plot-geo-data-on-background-map-tp29679002p29679002.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

--
Aman Thakral
B.Eng & Biosci, M.Eng Design

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing
http://p.sf.net/sfu/novell-sfdev2dev

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

--
View this message in context: http://old.nabble.com/matplotlib-basemap-plot-geo-data-on-background-map-tp29679002p29679748.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

I tested the same plot with a background image in Mercator with same results
and I also tested with Mercator covering almost the entire world with the
same results. Only a little portion of the background image shows as
background although the polygon looks OK in all cases (with maps drawn and
with either background image).
Unfortunately, I must use the provided background images for my project.
I may need to look for some other tool to accomplish drawing curves on a
provided background image instead of relying on matplotlib.
I did not understand the need for me to write my own transformation
functions. I only used lat/lon->x/y for the points in the curve and the
polygon shows up OK. What should I write my own transformations for?
THANKS

izzybitsie wrote:

···

Thanks for your answer. I'm actually looking to plot filled curves whose
points are lat/lon points on top of a given background map. The problem
experienced is misrepresentation of background map when I insert it using
warpimage(), the only function to do this I found so far.

Aman Thakral wrote:

If you're just looking for points, you can use ax.scatter(). It will
plot
the points. Also, make sure you set the zorder keyword argument in the
scatter.

Example:
x=range(10)
y=range(10)
z=range(10,20)
ax.scatter(x,y,c=z,zorder=10)

Hope this helps,
Aman

On Fri, Sep 10, 2010 at 1:06 PM, izzybitsie <isidora@...3269...> wrote:

Hi,
I'm new to matplotlib and I'm looking for an easy way to plot
geographical
data on a background map: bkgmap.png
http://old.nabble.com/file/p29679002/bkgmap.png
So far I only found out about warpimage() to do this but only part of
bkgmap.png comes up in the output image. I think this is because this
image
has no pixels covering all the world.
http://old.nabble.com/file/p29679002/partialbkg_polygon.png

Any idea on how to insert this image as background?
THANKS

Code: the polygon displays in right position even though background
doesn't
show OK (tested with map,lat/lon lines drawn too)

import sys
import Image, ImageDraw # PIL
from matplotlib.patches import Polygon
from mpl_toolkits.basemap import Basemap
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np

lat0=48
lon0=13
lllon=-15
lllat=20
urlon=73
urlat=57
map =

Basemap(projection='stere',lat_0=lat0,lon_0=lon0,llcrnrlon=lllon,llcrnrlat=lllat,urcrnrlon=urlon,urcrnrlat=urlat,
             resolution='c',area_thresh=1000.)
map.warpimage(image='bkgmap.png',scale=None,ax=plt.gca())
# points
lat = [50.,55.,45.,40.,50.]
lon = [-20.,-10.,10.,-10.,-20.]
x0,y0 = map(lon[0],lat[0])
x1,y1 = map(lon[1],lat[1])
x2,y2 = map(lon[2],lat[2])
x3,y3 = map(lon[3],lat[3])
x4,y4 = map(lon[4],lat[4])

plt.gca().add_patch(Polygon([(x0,y0),(x1,y1),(x2,y2),(x3,y3),(x4,y4)],fill=1,facecolor='red',edgecolor='black'))
plt.show()
--
View this message in context:
http://old.nabble.com/matplotlib-basemap-plot-geo-data-on-background-map-tp29679002p29679002.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

--
Aman Thakral
B.Eng & Biosci, M.Eng Design

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing
http://p.sf.net/sfu/novell-sfdev2dev

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

--
View this message in context: http://old.nabble.com/matplotlib-basemap-plot-geo-data-on-background-map-tp29679002p29679934.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Because matplotlib (and possibly any other tool out there) have
absolutely no idea what transformation have you used to generate your
background picture in the first place. If you want to match grid of
coordinates of you picture with the grid of coordinates of what you
are trying to plot on top of it, you have to input this information
somehow.

And one possible way of doing this is taking a transformation function
which will be exactly the same as one used to generate your
background. I do not think there is some other, magical way of doing
this. This is not a matplotlib specific problem, it is a general
problem. But maybe someone else will correct me if I am wrong.

JS

···

On Fri, Sep 10, 2010 at 14:46, izzybitsie <isidora@...3269...> wrote:

I did not understand the need for me to write my own transformation
functions. I only used lat/lon->x/y for the points in the curve and the
polygon shows up OK. What should I write my own transformations for?

Here is an example of how to make this work if the map projection on the image is exactly the same as the map projection in Basemap:

import sys
import Image, ImageDraw # PIL
from matplotlib.patches import Polygon
from mpl_toolkits.basemap import Basemap
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.image import pil_to_array

lat0=48
lon0=13
lllon=-15
lllat=20
urlon=73
urlat=57
map =\
Basemap(projection='stere',lat_0=lat0,lon_0=lon0,llcrnrlon=lllon,llcrnrlat=lllat,urcrnrlon=urlon,urcrnrlat=urlat,
               resolution='l')
#map.warpimage(image='bkgmap.png',scale=None,ax=plt.gca())
pilImage = Image.open('bkgmap.png')
rgba = pil_to_array(pilImage)
im = map.imshow(rgba)
# points
lat = [50.,55.,45.,40.,50.]
lon = [-20.,-10.,10.,-10.,-20.]
x0,y0 = map(lon[0],lat[0])
x1,y1 = map(lon[1],lat[1])
x2,y2 = map(lon[2],lat[2])
x3,y3 = map(lon[3],lat[3])
x4,y4 = map(lon[4],lat[4])
plt.gca().add_patch(Polygon([(x0,y0),(x1,y1),(x2,y2),(x3,y3),(x4,y4)],fill=1,facecolor='red',edgecolor='black'))
map.drawmapboundary()
map.drawcoastlines()
plt.show()

Note that the coastlines drawn by drawcoastlines don't match the coastlines in the image (attached png). This means the Basemap projection doesn't match the image projection. Unless you can make them match, you'll have to write your own transformations. Of course, you can't write your own transformation unless you know exactly what the map projection of the image is, so the simplest approach will be to make the Basemap projection match the image.

-Jeff

···

On 9/10/10 11:06 AM, izzybitsie wrote:

Hi,
I'm new to matplotlib and I'm looking for an easy way to plot geographical
data on a background map: bkgmap.png
http://old.nabble.com/file/p29679002/bkgmap.png
So far I only found out about warpimage() to do this but only part of
bkgmap.png comes up in the output image. I think this is because this image
has no pixels covering all the world.
http://old.nabble.com/file/p29679002/partialbkg_polygon.png

Any idea on how to insert this image as background?
THANKS

Code: the polygon displays in right position even though background doesn't
show OK (tested with map,lat/lon lines drawn too)

import sys
import Image, ImageDraw # PIL
from matplotlib.patches import Polygon
from mpl_toolkits.basemap import Basemap
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np

lat0=48
lon0=13
lllon=-15
lllat=20
urlon=73
urlat=57
map =
Basemap(projection='stere',lat_0=lat0,lon_0=lon0,llcrnrlon=lllon,llcrnrlat=lllat,urcrnrlon=urlon,urcrnrlat=urlat,
               resolution='c',area_thresh=1000.)
map.warpimage(image='bkgmap.png',scale=None,ax=plt.gca())
# points
lat = [50.,55.,45.,40.,50.]
lon = [-20.,-10.,10.,-10.,-20.]
x0,y0 = map(lon[0],lat[0])
x1,y1 = map(lon[1],lat[1])
x2,y2 = map(lon[2],lat[2])
x3,y3 = map(lon[3],lat[3])
x4,y4 = map(lon[4],lat[4])
plt.gca().add_patch(Polygon([(x0,y0),(x1,y1),(x2,y2),(x3,y3),(x4,y4)],fill=1,facecolor='red',edgecolor='black'))
plt.show()

--
Jeffrey S. Whitaker Phone : (303)497-6313
Meteorologist FAX : (303)497-6449
NOAA/OAR/PSD R/PSD1 Email : Jeffrey.S.Whitaker@...259...
325 Broadway Office : Skaggs Research Cntr 1D-113
Boulder, CO, USA 80303-3328 Web : Jeffrey S. Whitaker: NOAA Physical Sciences Laboratory