2012-11-15 54 views
15

嗨我想绘制一个地图使用pythons底图与一些国家填补了一定的颜色。在Python底图填充国家

那里有一个快速简单的解决方案吗?

+2

或许有用:HTTP://www.geophysique。 be/2011/01/27/matplotlib-basemap-tutorial-07-shapefiles-unleached/ – unutbu

+0

我相信这有助于:http://matplotlib.1069221.n5.nabble.com/How-to-draw-a-specific- country-by-basemap-td15744.html –

+2

感谢您的评论,他们最有帮助。我还发现了一个包含免费国家/地区数据的网站,这正是我正在寻找的内容:[http://www.naturalearthdata.com/](http://www.naturalearthdata.com/) –

回答

12

正如@unutbu所说,托马斯的职位here正是你所追求的。

如果您想Cartopy,相应的代码(在V0.7)要做到这一点可以从http://scitools.org.uk/cartopy/docs/latest/tutorials/using_the_shapereader.html略有调整:

import cartopy.crs as ccrs 
import matplotlib.pyplot as plt 
import cartopy.io.shapereader as shpreader 
import itertools 
import numpy as np 

shapename = 'admin_0_countries' 
countries_shp = shpreader.natural_earth(resolution='110m', 
             category='cultural', name=shapename) 

# some nice "earthy" colors 
earth_colors = np.array([(199, 233, 192), 
           (161, 217, 155), 
           (116, 196, 118), 
           (65, 171, 93), 
           (35, 139, 69), 
           ])/255. 
earth_colors = itertools.cycle(earth_colors) 



ax = plt.axes(projection=ccrs.PlateCarree()) 
for country in shpreader.Reader(countries_shp).records(): 
    print country.attributes['name_long'], earth_colors.next() 
    ax.add_geometries(country.geometry, ccrs.PlateCarree(), 
         facecolor=earth_colors.next(), 
         label=country.attributes['name_long']) 

plt.show() 

output

+3

请注意,你应该在这里发布答案的重要部分,在这个网站上,或者你的帖子风险被删除[查看常见问题,其中提到的答案几乎不超过链接。](http:// stackoverflow .com/faq#删除)如果您愿意,您仍然可以包含链接,但仅作为“参考”。答案应该独立,不需要链接。 – Taryn

+1

谢谢@bluefeet - 我明白为什么会是这种情况。我已经更新了答案,以提供一些新的信息(没有重复原始链接,我没有拥有版权)。干杯, – pelson

+0

调用shpreader.natural_earth给我一个HTTP 404未找到错误,它显然尝试下载它? – Leo

9

由pelson回答启发,我发布我有的解决方案。我会留给你最好的,所以我现在不会接受任何答案。

#! /usr/bin/env python 

import sys 
import os 
from pylab import * 
from mpl_toolkits.basemap import Basemap 
import matplotlib as mp 

from shapelib import ShapeFile 
import dbflib 
from matplotlib.collections import LineCollection 
from matplotlib import cm 

def get_shapeData(shp,dbf): 
    for npoly in range(shp.info()[0]): 
    shpsegs = [] 
    shpinfo = [] 

    shp_object = shp.read_object(npoly) 
    verts = shp_object.vertices() 
    rings = len(verts) 
    for ring in range(rings): 
     if ring == 0: 
      shapedict = dbf.read_record(npoly) 
     name = shapedict["name_long"] 
     continent = shapedict["continent"] 
     lons, lats = zip(*verts[ring]) 
     if max(lons) > 721. or min(lons) < -721. or max(lats) > 91. or min(lats) < -91: 
      raise ValueError,msg 
     x, y = m(lons, lats) 
     shpsegs.append(zip(x,y)) 
     shapedict['RINGNUM'] = ring+1 
     shapedict['SHAPENUM'] = npoly+1 
     shpinfo.append(shapedict) 

    lines = LineCollection(shpsegs,antialiaseds=(1,)) 
    lines.set_facecolors(cm.jet(np.random.rand(1))) 
    lines.set_edgecolors('k') 
    lines.set_linewidth(0.3) 
    ax.add_collection(lines) 


if __name__=='__main__': 

    f=figure(figsize=(10,10)) 
    ax = plt.subplot(111) 
    m = Basemap(projection='merc',llcrnrlat=30,urcrnrlat=72,\ 
      llcrnrlon=-40,urcrnrlon=50,resolution='c') 
    m.drawcountries(linewidth=0.1,color='w') 

    sfile = 'ne_10m_admin_0_countries' 

    shp = ShapeFile(sfile) 
    dbf = dbflib.open(sfile) 
    get_shapeData(shp,dbf) 

    show() 
    sys.exit(0) 

这是结果

example for filling in countries in different colours

这里是我的榜样如何填写阿尔巴尼亚在正确的颜色(不是很优雅,我知道;))。

#HACK for Albania 
    shpsegs = [] 
    shpinfo = [] 

    shp_object = shp.read_object(9) 
    verts = shp_object.vertices() 
    rings = len(verts) 
    for ring in range(rings): 
     if ring == 0: 
      shapedict = dbf.read_record(9) 
     name = shapedict["name_long"] 
     continent = shapedict["continent"] 
     lons, lats = zip(*verts[ring]) 
     if max(lons) > 721. or min(lons) < -721. or max(lats) > 91. or min(lats) < -91: 
      raise ValueError,msg 
     x, y = m(lons, lats) 
     shpsegs.append(zip(x,y)) 
     shapedict['RINGNUM'] = ring+1 
     shapedict['SHAPENUM'] = npoly+1 
     shpinfo.append(shapedict) 
    lines = LineCollection(shpsegs,antialiaseds=(1,)) 
    if name == 'Albania': 
    lines.set_facecolors('w') 
    lines.set_edgecolors('k') 
    lines.set_linewidth(0.3) 
    ax.add_collection(lines) 

重要的是,在完成所有其他形状后,请执行此操作。也许你可以摆脱这段代码的某些部分,但正如我所说,这对我来说已经足够了。

对于我的应用程序的彩色contries通过名称或大陆,因此,这些行:

name = shapedict["name_long"] 
    continent = shapedict["continent"] 

使用我从这个网站获得的数据:http://www.naturalearthdata.com/

+2

你的阿尔巴尼亚人沉没。没有多少人会注意到:D – theta

+0

是的,亚美尼亚的情况实际上也是如此。之后我必须明确填补这两个国家的工作空间。与naturalearthdata的人询问并不确定,一旦我为我解决这个问题,我没有跟进这个问题 –

+0

@red_tiger我和阿根廷和安哥拉有同样的问题。你能把你的解决方案发布到“阿尔巴尼亚问题”吗? NaturalEarth的人们说什么?谢谢。 –

相关问题