2012-11-15 66 views
15

こんにちは私はいくつかの国で特定の色で塗られたpythonsベースマップを使用してマップをプロットしようとしています。Pythonベースマップで国を塗りつぶし

そこにすばやく簡単な解決策がありますか?

+2

多分便利ます:http://www.geophysique。/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がすでに言っているように、Thomasの投稿hereは、まさにあなたが後にしているものです。

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

このサイトの回答の重要な部分をこのサイトに投稿するか、あなたの投稿のリスクが削除されるようにすることを忘れないように注意してください[「リンクをはるかに超える」回答を記載したFAQを参照してください。](http:// stackoverflow .com/faq#deletion)もしあなたが望むのであれば、あなたは依然としてリンクを含めることができますが、 '参照'としてだけです。答えは、リンクを必要とせずに単独で立つべきです。 – Taryn

+1

ありがとう@bluefeet - なぜそうなるのか分かります。私はいくつかの新しい情報を与えるために答えを更新しました。(私は著作権を所有していなかった元のリンクを複製することなく)。乾杯、 – pelson

+0

shpreader.natural_earthを呼び出すと、HTTP 404が見つかりませんでした。明らかにそれをダウンロードしようとしていますか? – Leo

9

pelsonからの回答に触発され、私はポストを:

あなたは少しhttp://scitools.org.uk/cartopy/docs/latest/tutorials/using_the_shapereader.htmlから適合させることができる(v0.7)でCartopy、対応するコードでこれを行うにしたいなら私の解決策。私はあなたに最高の作品を残すので、私は現時点では何の答えも受け入れません。

#! /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) 

他のすべてのシェイプを実行した後に行うことが重要です。おそらく、あなたはこのコードの一部を取り除くことができますが、私が言ったように、それは私のためには十分でした。名前や大陸によって私のアプリケーションI色のcontriesのために

、したがって、これらの行:私はこのウェブサイトから得た使用

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

データ:http://www.naturalearthdata.com/

+2

あなたのアルバニアは沈んでいます。多くの人が気付くであろう:D – theta

+0

はい、実際はアルメニアにも同じことが起こります。私は、後でこれらの2つの国を明示的に埋めることによって、仕事をする必要がありました。naturalearthdataからの人々とのinquieryは決定的なものではなく、私が私のためにそれを固定した後、私はこれをフォローしませんでした –

+0

@red_tiger私はアルゼンチンとアンゴラと同じ問題を抱えています。あなたは "アルバニアの問題"にあなたの解決策を投稿できますか? NaturalEarthの人々は何を言ったのですか?ありがとう。 –

関連する問題