import numpy as np from matplotlib import pyplot as plt from matplotlib import cm from matplotlib.collections import LineCollection from matplotlib.patches import Polygon from matplotlib.patches import Polygon from mpl_toolkits.basemap import Basemap %matplotlib inline import shapefile def draw_portugal(islands = True): """ This functions draws and returns a map of Portugal, either just of the mainland or including the Azores and Madeira islands. """ fig = plt.figure(figsize=(15.7,12.3)) projection='merc' llcrnrlat=-80 urcrnrlat=90 llcrnrlon=-180 urcrnrlon=180 resolution='i' if islands: x1 = -32. x2 = 5. y1 = 30. y2 = 45. else: x1 = -12. x2 = -5. y1 = 36. y2 = 44. m = Basemap(projection=projection, llcrnrlat=y1, urcrnrlat=y2, llcrnrlon=x1, urcrnrlon=x2, resolution=resolution) m.drawcoastlines() m.drawmapboundary() m.drawcountries() m.fillcontinents(color = '#C0C0C0') return m draw_portugal() plt.show() r = shapefile.Reader(r"maps\prt_adm1") shapes = r.shapes() records = r.records() print r.fields print "\n----------------------------------------------------------------------------------\n" for record in records: print record populacao = {"Aveiro": 735790, "Azores": 246746, "Beja": 150287, "Braga": 848185, "Bragança": 139344, "Castelo Branco": 225916, "Coimbra": 429714, "Évora": 168034, "Faro": 434023, "Guarda": 168898, "Leiria": 470895, "Lisboa": 2244799, "Madeira": 267785, "Portalegre": 118952, "Porto": 2027191, "Santarém": 465701, "Setúbal": 866794, "Viana do Castelo": 250390, "Vila Real": 213775, "Viseu": 391215 } max_pop = np.max(populacao.values())+0.0 for record in records: record.append(populacao[record[4].decode('Latin1').encode('UTF-8')]/max_pop) #print a record to make sure that the information was added print records[0] m = draw_portugal(islands=False) ax = plt.subplot(111) for record, shape in zip(records,shapes): #read shape lons,lats = zip(*shape.points) data = np.array(m(lons, lats)).T #each shape may have different segments if len(shape.parts) == 1: segs = [data,] else: segs = [] for i in range(1,len(shape.parts)): index = shape.parts[i-1] index2 = shape.parts[i] segs.append(data[index:index2]) segs.append(data[index2:]) #draws the segments, and sets its properties. A colormap is used to get the gradient effect. lines = LineCollection(segs,antialiaseds=(1,)) lines.set_facecolors(cm.YlGn(record[-1])) lines.set_edgecolors('k') lines.set_linewidth(0.1) ax.add_collection(lines) plt.show() fig = plt.figure(figsize=(15.7,12.3)) ax = plt.subplot(111) map = Basemap(projection='cyl',llcrnrlat=35,urcrnrlat=72,\ llcrnrlon=-17,urcrnrlon=42,resolution='c') map.drawmapboundary(fill_color='aqua') map.readshapefile('maps\world_by_country', 'countries_sf', drawbounds=True) #the readshapefile method allow you to call the shapefile's shapes and info. #Both are lists, the first containing a list of tuples (coordinates), and the second containig a dictionary with associated metadata print len(map.countries_sf) #number of shapes print map.countries_sf_info[0].keys() #metadata #plots the shapes as Polygons with a random facecolor (if European), or gray (if not) for shape, country in zip(map.countries_sf, map.countries_sf_info): if country['REGION'] == 'Europe': poly = Polygon(shape, facecolor=cm.YlGn(np.random.rand())) plt.gca().add_patch(poly) else: poly = Polygon(shape, facecolor="gray") plt.gca().add_patch(poly) #adds the colormap legend cmleg = np.zeros((1,20)) for i in range(20): cmleg[0,i] = (i*5)/100.0 plt.imshow(cmleg, cmap=plt.get_cmap("YlGn"), aspect=1) plt.colorbar() plt.show()