Chapter 4, example 2

In this example, we show how we can make visual representations of the world cities population data by displaying a map of human density. You need to execute the example 3 of Chapter 3 first, so that the data set is available.


In [1]:
import pandas as pd
filename = '../chapter3/data/worldcitiespop.txt'
data = pd.read_csv(filename)

Let's plot the raw coordinates, with one pixel per position.


In [2]:
plot(data.Longitude, data.Latitude, ',')


Out[2]:
[<matplotlib.lines.Line2D at 0x212cef90>]

We easily recognize the world map!


In [3]:
locations = data[['Longitude','Latitude']].as_matrix()

Now, we load a world map with matplotlib.basemap.


In [4]:
from mpl_toolkits.basemap import Basemap
m = Basemap(projection='mill', llcrnrlat=-65, urcrnrlat=85, llcrnrlon=-180, urcrnrlon=180)

We specify the coordinates of the map's corners.


In [5]:
x0, y0 = m(-180, -65)
x1, y1 = m(180, 85)

Let's create a histogram of the human density. First, we retrieve the population of every city.


In [6]:
population = data.Population

We project the geographical coordinates into cartesian coordinates.


In [7]:
x, y = m(locations[:,0], locations[:,1])

We handle cities which do not have a population value.


In [8]:
weights = population.copy()
weights[isnan(weights)] = 1000

We create the 2D histogram with NumPy.


In [9]:
h, _, _ = histogram2d(x, y, bins=(linspace(x0, x1, 500), linspace(y0, y1, 500)), weights=weights)
h[h==0] = 1

We filter this histogram with a Gaussian kernel.


In [10]:
import scipy.ndimage.filters
z = scipy.ndimage.filters.gaussian_filter(log(h.T), 1)

We draw the coast lines in the world map, as well as the filtered human density.


In [11]:
figure(figsize=(10,6))
m.drawcoastlines()
m.imshow(z, origin='lower', extent=[x0,x1,y0,y1], cmap=get_cmap('Reds'))


Out[11]:
<matplotlib.image.AxesImage at 0x6e417d0>