We use shapefiles from the Natural Earth website, that have been previously downloaded
Set up imports; because we will be handling shapefiles, we need polygon helper functions
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
# these are needed for shapefile mgt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.patches import PathPatch
#import seaborn as sb
Different way of setting figure size (do it once for whole notebook)
#from pylab import rcParams
import pylab
%matplotlib inline
pylab.rcParams['figure.figsize'] = (16,8)
#sb.set_context('poster')
world
for subsequent referencefig = plt.figure()
ax = fig.add_subplot(111)
m = Basemap(projection='gall',llcrnrlat=-70,urcrnrlat=90,\
llcrnrlon=-180,urcrnrlon=180,resolution='c')
#m.fillcontinents(color='tan')
m.drawmapboundary()
m.drawparallels(np.arange(-80.,81.,20.))
m.drawmeridians(np.arange(-180.,181.,20.))
#m.drawcoastlines()
#m.drawrivers(color='blue')
m.readshapefile('shapefiles/ne_10m_admin_0_countries',
name='world',
drawbounds=True,
color='gray')
import math
import numbers
patches = []
population = []
for info,shape in zip(m.world_info, m.world):
patches.append(Polygon(np.array(shape), True))
if( isinstance(info['POP_EST'], numbers.Number) ):
try:
z = math.log10(info['POP_EST']+1.0)
except:
z = 0
#end try
else:
z = 0.0
#end if
population.append(z)
#end for
p = PatchCollection(patches, alpha=0.5, zorder=3, cmap='rainbow')
p.set_array(np.array(population))
ax.add_collection(p)
cb = fig.colorbar(p, ax=ax, shrink=0.6, ticks = range(0,10))
tick_labels = ["{:,}".format(10**i) for i in range(0,10)]
cb.ax.set_yticklabels(tick_labels)
cb.ax.set_xlabel('Country Population', size = 8)
plt.title('World Choropleth')
plt.show()