Aalok kapoor wrote:
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,
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.
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
# Miller projection world map.
m = Basemap(llcrnrlon=-180,llcrnrlat=-90,urcrnrlon=180,urcrnrlat=90,
# 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.
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]
# cycle through country names, color each one.
for nshape,seg in enumerate(m.countries):
xx,yy = zip(*seg)
color = rgb2hex(colors[countrynames[nshape]])
# draw meridians and parallels.
p.title('Filling Country Polygons')
Jeffrey S. Whitaker Phone : (303)497-6313
NOAA/OAR/CDC R/PSD1 FAX : (303)497-6449
325 Broadway Boulder, CO, USA 80305-3328