Basemap + contour layer?

Hi,

I am trying to plot an image using basemap and overlay another image plotted
as a contour and I am a bit stuck.

e.g.

I have a small image of the world for example (10.5-13.5N, 1.5-3.5E, regular
lat long grid). And I saw what was posted previously
here...http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg01961.html
but I am still stuck.

I plot my original image...

im = m.imshow(image, norm=norm, interpolation='nearest', cmap=colour_map)

then I tried to build the info for the contour plot

delta = 0.03
x = np.arange(1.5, 3.5, delta)
y = np.arange(10.5, 13.5, delta)
X, Y = np.meshgrid(x, y)
m.contour(image2, X, Y, color='black')

plt.show()

but that only plots the first image? The images arrays are identically sized
and of the same area, is there a way to grab the grid from the first plot
perhaps?

No idea what I am doing wrong.

Many thanks,

Martin

···


View this message in context: http://old.nabble.com/Basemap-%2B-contour-layer--tp29235080p29235080.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Hi,

I am trying to plot an image using basemap and overlay another image plotted
as a contour and I am a bit stuck.

e.g.

I have a small image of the world for example (10.5-13.5N, 1.5-3.5E, regular
lat long grid). And I saw what was posted previously
here...Re: [Matplotlib-users] contour and basemap
but I am still stuck.

I plot my original image...

im = m.imshow(image, norm=norm, interpolation='nearest', cmap=colour_map)

then I tried to build the info for the contour plot

delta = 0.03
x = np.arange(1.5, 3.5, delta)
y = np.arange(10.5, 13.5, delta)
X, Y = np.meshgrid(x, y)
m.contour(image2, X, Y, color='black')

plt.show()

but that only plots the first image? The images arrays are identically sized
and of the same area, is there a way to grab the grid from the first plot
perhaps?

No idea what I am doing wrong.
   
Nor do I - it would help to have a self-contained example to run that demonstrates your problem.

-Jeff

···

On 7/22/10 3:53 AM, mdekauwe wrote:

Many thanks,

Martin

Ok... how about this?

import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

# generate some random data - i.e. what would be an image of the world, say
different
# land covers
data = np.random.randint(10,27,100*67).reshape(100, 67)

# I want to draw a boundary round each land cover type, so
# classify a new array
data2 = np.where(data == 10, 1, data)
data2 = np.where(data == 11, 1, data)
data2 = np.where(data == 13, 2, data)
data2 = np.where(data == 14, 2, data)
data2 = np.where(data == 15, 2, data)
data2 = np.where(data == 16, 2, data)
data2 = np.where(data == 18, 3, data)
data2 = np.where(data == 19, 3, data)
data2 = np.where(data == 20, 3, data)
data2 = np.where(data == 26, 4, data)
data2 = np.where(data == 27, 4, data)

plt.contour(data2)
plt.show()
sys.exit()

this pops up a contour plot...

Now what if as I do I want to overlay this on a basemap plot...

import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

# generate some random data - i.e. what would be an image of the world, say
different
# land covers
data = np.random.randint(10,27,100*67).reshape(100, 67)

# I want to draw a boundary round each land cover type, so
# classify a new array
data2 = np.where(data == 10, 1, data)
data2 = np.where(data == 11, 1, data)
data2 = np.where(data == 13, 2, data)
data2 = np.where(data == 14, 2, data)
data2 = np.where(data == 15, 2, data)
data2 = np.where(data == 16, 2, data)
data2 = np.where(data == 18, 3, data)
data2 = np.where(data == 19, 3, data)
data2 = np.where(data == 20, 3, data)
data2 = np.where(data == 26, 4, data)
data2 = np.where(data == 27, 4, data)

fig = plt.figure(figsize=(8, 6))
m = Basemap(llcrnrlon=1.5, llcrnrlat=10.5, urcrnrlon=3.5, urcrnrlat=13.5,
                    resolution='c', projection='cyl')
ax = fig.add_axes([0.1, 0.1, 0.6, 0.7])
m.ax = ax

delta = 0.03
x = np.arange(1.5, 3.5, delta)
y = np.arange(10.5, 13.5, delta)
X, Y = np.meshgrid(x, y)

im = m.imshow(data, interpolation='nearest', cmap=plt.cm.jet)
m.contour(data2, X, Y, color='black')
plt.show()

This only plots the basemap without the contours?

Many thanks,

Martin

···


View this message in context: http://old.nabble.com/Basemap-%2B-contour-layer--tp29235080p29237074.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

# generate some random data - i.e. what would be an image of the world, say
different
# land covers
data = np.random.randint(10,27,100*67).reshape(100, 67)

# I want to draw a boundary round each land cover type, so
# classify a new array
data2 = np.where(data == 10, 1, data)
data2 = np.where(data == 11, 1, data)
data2 = np.where(data == 13, 2, data)
data2 = np.where(data == 14, 2, data)
data2 = np.where(data == 15, 2, data)
data2 = np.where(data == 16, 2, data)
data2 = np.where(data == 18, 3, data)
data2 = np.where(data == 19, 3, data)
data2 = np.where(data == 20, 3, data)
data2 = np.where(data == 26, 4, data)
data2 = np.where(data == 27, 4, data)

plt.contour(data2)
plt.show()
sys.exit()

this pops up a contour plot...

Now what if as I do I want to overlay this on a basemap plot...

import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

# generate some random data - i.e. what would be an image of the world, say
different
# land covers
data = np.random.randint(10,27,100*67).reshape(100, 67)

# I want to draw a boundary round each land cover type, so
# classify a new array
data2 = np.where(data == 10, 1, data)
data2 = np.where(data == 11, 1, data)
data2 = np.where(data == 13, 2, data)
data2 = np.where(data == 14, 2, data)
data2 = np.where(data == 15, 2, data)
data2 = np.where(data == 16, 2, data)
data2 = np.where(data == 18, 3, data)
data2 = np.where(data == 19, 3, data)
data2 = np.where(data == 20, 3, data)
data2 = np.where(data == 26, 4, data)
data2 = np.where(data == 27, 4, data)

fig = plt.figure(figsize=(8, 6))
m = Basemap(llcrnrlon=1.5, llcrnrlat=10.5, urcrnrlon=3.5, urcrnrlat=13.5,
                     resolution='c', projection='cyl')
ax = fig.add_axes([0.1, 0.1, 0.6, 0.7])
m.ax = ax

delta = 0.03
x = np.arange(1.5, 3.5, delta)
y = np.arange(10.5, 13.5, delta)
X, Y = np.meshgrid(x, y)

im = m.imshow(data, interpolation='nearest', cmap=plt.cm.jet)
m.contour(data2, X, Y, color='black')
   
Martin: This needs to be

m.contour(X, Y, data2, colors='black')

(the data goes after the coordinate arrays, and the keyword is 'colors', not 'color')

-Jeff

···

On 7/22/10 7:48 AM, mdekauwe wrote:

plt.show()

This only plots the basemap without the contours?

Many thanks,

Martin

Hi Jeff,

Yes you are right! Apologies. Whilst it is easy enough to define the grid in
this case as it is a regular lat,long grid. If it was for example a
geostationary grid, I think this might be more difficult. Is there a way to
return the grid for example from the basemap call, which can then be used in
the contour call?

I hope that makes sense.

Thanks again.

Jeff Whitaker wrote:

···

On 7/22/10 7:48 AM, mdekauwe wrote:

import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

# generate some random data - i.e. what would be an image of the world,
say
different
# land covers
data = np.random.randint(10,27,100*67).reshape(100, 67)

# I want to draw a boundary round each land cover type, so
# classify a new array
data2 = np.where(data == 10, 1, data)
data2 = np.where(data == 11, 1, data)
data2 = np.where(data == 13, 2, data)
data2 = np.where(data == 14, 2, data)
data2 = np.where(data == 15, 2, data)
data2 = np.where(data == 16, 2, data)
data2 = np.where(data == 18, 3, data)
data2 = np.where(data == 19, 3, data)
data2 = np.where(data == 20, 3, data)
data2 = np.where(data == 26, 4, data)
data2 = np.where(data == 27, 4, data)

plt.contour(data2)
plt.show()
sys.exit()

this pops up a contour plot...

Now what if as I do I want to overlay this on a basemap plot...

import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

# generate some random data - i.e. what would be an image of the world,
say
different
# land covers
data = np.random.randint(10,27,100*67).reshape(100, 67)

# I want to draw a boundary round each land cover type, so
# classify a new array
data2 = np.where(data == 10, 1, data)
data2 = np.where(data == 11, 1, data)
data2 = np.where(data == 13, 2, data)
data2 = np.where(data == 14, 2, data)
data2 = np.where(data == 15, 2, data)
data2 = np.where(data == 16, 2, data)
data2 = np.where(data == 18, 3, data)
data2 = np.where(data == 19, 3, data)
data2 = np.where(data == 20, 3, data)
data2 = np.where(data == 26, 4, data)
data2 = np.where(data == 27, 4, data)

fig = plt.figure(figsize=(8, 6))
m = Basemap(llcrnrlon=1.5, llcrnrlat=10.5, urcrnrlon=3.5, urcrnrlat=13.5,
                     resolution='c', projection='cyl')
ax = fig.add_axes([0.1, 0.1, 0.6, 0.7])
m.ax = ax

delta = 0.03
x = np.arange(1.5, 3.5, delta)
y = np.arange(10.5, 13.5, delta)
X, Y = np.meshgrid(x, y)

im = m.imshow(data, interpolation='nearest', cmap=plt.cm.jet)
m.contour(data2, X, Y, color='black')
   
Martin: This needs to be

m.contour(X, Y, data2, colors='black')

(the data goes after the coordinate arrays, and the keyword is 'colors',
not 'color')

-Jeff

plt.show()

This only plots the basemap without the contours?

Many thanks,

Martin

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

--
View this message in context: http://old.nabble.com/Basemap-%2B-contour-layer--tp29235080p29237306.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

If you have the lats and lons of the data grid, you can convert to map projection coordinates by calling the Basemap instance, i.e.

map = Basemap(....)
x,y = map(lons,lats)

you can then use the x,y values in contour

map.contourf(x,y,data)

Does that help?

-Jeff

···

On 7/22/10 8:08 AM, mdekauwe wrote:

Hi Jeff,

Yes you are right! Apologies. Whilst it is easy enough to define the grid in
this case as it is a regular lat,long grid. If it was for example a
geostationary grid, I think this might be more difficult. Is there a way to
return the grid for example from the basemap call, which can then be used in
the contour call?

I hope that makes sense.

Thanks again.

Hi Jeff,

That sounds like the perfect solution as I do have the lat, lons - I'll give
that a go!

Thanks again,

Martin

Jeff Whitaker wrote:

···

On 7/22/10 8:08 AM, mdekauwe wrote:

Hi Jeff,

Yes you are right! Apologies. Whilst it is easy enough to define the grid
in
this case as it is a regular lat,long grid. If it was for example a
geostationary grid, I think this might be more difficult. Is there a way
to
return the grid for example from the basemap call, which can then be used
in
the contour call?

I hope that makes sense.

Thanks again.

If you have the lats and lons of the data grid, you can convert to map
projection coordinates by calling the Basemap instance, i.e.

map = Basemap(....)
x,y = map(lons,lats)

you can then use the x,y values in contour

map.contourf(x,y,data)

Does that help?

-Jeff

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

--
View this message in context: http://old.nabble.com/Basemap-%2B-contour-layer--tp29235080p29237609.html
Sent from the matplotlib - users mailing list archive at Nabble.com.