Some queries regarding basemap.

Hi all,

I have tried to use basemap to plot world map and successfully plotted points on it by giving lists of latitudes and longitudes. One problem i have is i want to give different colors for different countries, i.e. regarding population. For ex. for range 2-20 – green. 20-100 – red… etc…
How can i give colors to different countries according to data I pass to it? please help me in knowing whether it is possible or not.

Thanks in advance,
-Aalok

···

Find out what India is talking about on - Yahoo! Answers India

Send FREE SMS to your friend’s mobile from Yahoo! Messenger Version 8. Get it NOW

Aalok kapoor wrote:

Hi all,

I have tried to use basemap to plot world map and successfully plotted points on it by giving lists of latitudes and longitudes. One problem i have is i want to give different colors for different countries, i.e. regarding population. For ex. for range 2-20 -- green. 20-100 -- red.. etc..
How can i give colors to different countries according to data I pass to it? please help me in knowing whether it is possible or not.

Thanks in advance,
-Aalok

Aaolok: If you have access to the individual country polygons by ccountry name, it's easy. Unfortunately, the built-in country database is not accessible that way. Googling 'country shapefiles' came up with this.

http://www.cipotato.org/DIVA/data/MoreData.htm

Using that shapefile is was pretty easy to adapt the 'fillstates.py' example to fill the country polygons. Here each country is filled with a random color, so you can just replace the random number with a value from your dataset.

import pylab as p
from matplotlib.toolkits.basemap import Basemap as Basemap
from matplotlib.colors import rgb2hex
import random

# Miller projection world map.
m = Basemap(llcrnrlon=-180,llcrnrlat=-90,urcrnrlon=180,urcrnrlat=90,
            projection='mill')
# draw country boundaries.
# data from http://www.cipotato.org/DIVA/data/MoreData.htm
shp_info = m.readshapefile('world_adm0','countries',drawbounds=True)
# choose a color for each state based on population density.
colors={}
countrynames=
cmap = p.cm.jet # use 'jet' colormap
vmin = 0; vmax = 1000000 # set range of population
for shapedict in m.countries_info:
    countryname = shapedict['NAME']
    # set population to a random number
    # (replace with a real value)
    pop = float(random.randint(0,1000000))
    # calling colormap with value between 0 and 1 returns
    # rgba value. Invert color range (hot colors are high
    # population), take sqrt root to spread out colors more.
    colors[countryname] = cmap(1.-p.sqrt((pop-vmin)/(vmax-vmin)))[:3]
    countrynames.append(countryname)
# cycle through country names, color each one.
for nshape,seg in enumerate(m.countries):
    xx,yy = zip(*seg)
    color = rgb2hex(colors[countrynames[nshape]])
    p.fill(xx,yy,color,edgecolor=color)
# draw meridians and parallels.
m.drawparallels(p.arange(-90,91,30),labels=[1,0,0,0])
m.drawmeridians(p.arange(-180,181,60),labels=[0,0,0,1])
p.title('Filling Country Polygons')
p.show()

HTH,

-Jeff

···

--
Jeffrey S. Whitaker Phone : (303)497-6313
NOAA/OAR/CDC R/PSD1 FAX : (303)497-6449
325 Broadway Boulder, CO, USA 80305-3328