Creating a human density map

I had originally planned on producing a worldwide human density map, but the graphics available don't allow for setting the color of each country. So, I built a density map for the United States.

The algorithm is:

  1. Obtain graphic shapes for each of the states.
  2. Obtain the density for each state.
  3. Decide on a color range and apply the lowest density to one end of the range and the highest to the other end.
  4. For each state:
    • Determine it's density
    • Lookup that density value in the range and select a color
    • Draw the state

This is coded with the following (comments embedded as the code proceeds):

%matplotlib inline
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
import pandas as pd
import numpy as np
import matplotlib
# 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'
# download from https://github.com/matplotlib/basemap/tree/master/examples/st99_d00.dbf,shx,shp
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
# load density data drawn from
# https://en.wikipedia.org/wiki/List_of_U.S._states_by_population_density
df = pd.read_csv('states.csv')
print(df.head())
State rank density/mi2 density/km2 pop_rank 2015_pop
New Jersey 1 1,218 470 11 8,958,013
Rhode Island 2 1,021 394 43 1,056,298
Massachusetts 3 871 336 15 6,794,422
Connecticut 4 741 286 29 3,590,886
Maryland 5 618 238 19 6,006,401
land_rank area_mi2 area_km2
0 46 7,354 19,046.80
1 50 1,034 2,678.00
2 45 7,800 20,201.90
3 48 4,842 12,540.70
4 42 9,707 25,141.00
# determine the range of density values
max_density = -1.0
min_density = -1.0
for index, row in df.iterrows():
d = row['density/mi2']
density = float(d.replace(',' , ''))
if (max_density==-1.0) or (max_density<density):
max_density = density
if (min_density==-1.0) or (min_density>density):
min_density = density
print('max',max_density)
print('min',min_density)
range_density = max_density - min_density
print(range_density)
('max', 1218.0)
('min', 1.0)
1217.0
# we pick a color for the state density out of color map
cmap = matplotlib.cm.get_cmap('Spectral')
# for each state get the color for it's density
for index, row in df.iterrows():
state_name = row['State']
d = row['density/mi2']
density = float(d.replace(',' , ''))
color = cmap((density - min_density)/range_density)
seg = map.states[state_names.index(state_name)]
poly = Polygon(seg, facecolor=color, edgecolor=color)
ax.add_patch(poly)
plt.show()

We see a color-coded density map in the following figure. I am not sure why Minnesota and Wisconsin did not match up with the data (they show no color for the density in the map). The data file looks correct and does appear to map to the image points.

The packages used in this example would need to be installed, as they are not part of the standard set issued:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.227.69