2011-09-28 14 views

答えて

16

GitHubのBasemapレポにうまく整形された例があります:fillstates.py。形状ファイル(dbf | shp | shx)もexamples folderに含まれています。我々はシェープファイルをロードするときの形状および属性がに格納されていること

Resulting plot with Texas filled red

注:赤充填テキサスとのプロットを得られ

import matplotlib.pyplot as plt 
from mpl_toolkits.basemap import Basemap 
from matplotlib.patches import Polygon 

# create the map 
map = Basemap(llcrnrlon=-119,llcrnrlat=22,urcrnrlon=-64,urcrnrlat=49, 
     projection='lcc',lat_1=33,lat_2=45,lon_0=-95) 

# load the shapefile, use the name 'states' 
map.readshapefile('st99_d00', name='states', drawbounds=True) 

# collect the state names from the shapefile attributes so we can 
# look up the shape obect for a state by it's name 
state_names = [] 
for shape_dict in map.states_info: 
    state_names.append(shape_dict['NAME']) 

ax = plt.gca() # get current axes instance 

# get Texas and draw the filled polygon 
seg = map.states[state_names.index('Texas')] 
poly = Polygon(seg, facecolor='red',edgecolor='red') 
ax.add_patch(poly) 

plt.show() 

:ここ

は、実施例の簡略版でありますmap.statesおよびmap.states_infoを、readshapefileコールで使用されるnameパラメータに基づくリストとしてそれぞれ示します。したがって、特定の状態のシェイプを検索するには、属性から状態名の対応するリストを作成する必要がありました。

+0

これは本当に素晴らしいです。私が特定の緯度/経度に星を入れたいのであれば、それをどのように追加すればよいでしょうか? – vy32

+0

@ vy32ここでは、[地図上のポイントデータをプロットする例](https://www.getdatajoy.com/examples/python-plots/plot-data-points-on-a-map)、マーカースタイルは[matplotlib documentation](http://matplotlib.org/api/markers_api.html)にあります。特に、 '*'は開始マーカーです。 – Weston

関連する問題