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.
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.
plot(data.Longitude, data.Latitude, ',')
[<matplotlib.lines.Line2D at 0x212cef90>]
We easily recognize the world map!
locations = data[['Longitude','Latitude']].as_matrix()
Now, we load a world map with matplotlib.basemap
.
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.
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.
population = data.Population
We project the geographical coordinates into cartesian coordinates.
x, y = m(locations[:,0], locations[:,1])
We handle cities which do not have a population value.
weights = population.copy()
weights[isnan(weights)] = 1000
We create the 2D histogram with NumPy.
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.
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.
figure(figsize=(10,6))
m.drawcoastlines()
m.imshow(z, origin='lower', extent=[x0,x1,y0,y1], cmap=get_cmap('Reds'))
<matplotlib.image.AxesImage at 0x6e417d0>