SourceForge logo
SourceForge logo
Menu

matplotlib-devel

From: Aalok k. <aal...@ya...> - 2006年11月22日 04:39:46
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
From: Jeff W. <js...@fa...> - 2006年11月22日 13:06:52
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
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.
Thanks for helping keep SourceForge clean.
X





Briefly describe the problem (required):
Upload screenshot of ad (required):
Select a file, or drag & drop file here.
Screenshot instructions:

Click URL instructions:
Right-click on the ad, choose "Copy Link", then paste here →
(This may not be possible with some types of ads)

More information about our ad policies

Ad destination/click URL:

AltStyle によって変換されたページ (->オリジナル) /