2011-09-28 20 views

risposta

16

V'è un esempio ben formattato nel repository su GitHub mappa di base: fillstates.py. Lo shapefile (dbf | shp | shx) è anche incluso nel examples folder.

Ecco una versione abbreviata dell'esempio:

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() 

trama risultante con il Texas riempita rosso:

Resulting plot with Texas filled red

Si noti che quando si carica un file shape le forme e gli attributi sono memorizzati in map.states e map.states_info rispettivamente come elenchi basati sul parametro name utilizzato nella chiamata readshapefile. Quindi, per cercare la forma per uno stato specifico, abbiamo dovuto creare un elenco corrispondente dei nomi di stato dagli attributi.

+0

È davvero fantastico. Se voglio mettere una stella su un particolare lat/lon, come dovrei aggiungerlo? – vy32

+0

@ vy32 ecco un buon [esempio di dati del punto di tracciamento su una mappa] (https://www.getdatajoy.com/examples/python-plots/plot-data-points-on-a-map), tutti i gli stili dei marcatori possono essere trovati nella [documentazione di matplotlib] (http://matplotlib.org/api/markers_api.html), in particolare '*' è il marker di inizio. – Weston

Problemi correlati