Skip to content

================ by Jawad Haider

Chpt 4 - Visualization with Matplotlib

Example: California Cities



Example: California Cities

Recall that in “Customizing Plot Legends”, we demonstrated the use of size and color in a scatter plot to convey information about the location, size, and population of California cities. Here, we’ll create this plot again, but using Basemap to put the data in context.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
cities=pd.read_csv('../data/california_cities.csv')
cities.head()
Unnamed: 0 city latd longd elevation_m elevation_ft population_total area_total_sq_mi area_land_sq_mi area_water_sq_mi area_total_km2 area_land_km2 area_water_km2 area_water_percent
0 0 Adelanto 34.576111 -117.432778 875.0 2871.0 31765 56.027 56.009 0.018 145.107 145.062 0.046 0.03
1 1 AgouraHills 34.153333 -118.761667 281.0 922.0 20330 7.822 7.793 0.029 20.260 20.184 0.076 0.37
2 2 Alameda 37.756111 -122.274444 NaN 33.0 75467 22.960 10.611 12.349 59.465 27.482 31.983 53.79
3 3 Albany 37.886944 -122.297778 NaN 43.0 18969 5.465 1.788 3.677 14.155 4.632 9.524 67.28
4 4 Alhambra 34.081944 -118.135000 150.0 492.0 83089 7.632 7.631 0.001 19.766 19.763 0.003 0.01
# extract the data we're interested in 
lat=cities['latd'].values
lon=cities['longd'].values
population=cities['population_total'].values
area=cities['area_total_km2'].values
# 1. Draw the map background
fig=plt.figure(figsize=(8,8))
m=Basemap(projection='lcc', resolution='h',
         lat_0=37.5, lon_0=-119,
         width=1E6, height=1.2E6)

m.shadedrelief()
m.drawcoastlines(color='gray')
m.drawcountries(color='gray')
m.drawstates(color='gray')

# 2. scatter city data, with color reflecting population and size reflecting area
m.scatter(lon, lat, latlon=True,
         c=np.log10(population),s=area,
         cmap='Reds',alpha=0.5)

# 3. Create colobar and legned
plt.colorbar(label=r'$log_{10}({\rm population})$')
plt.clim(3,7)


# 4. make legend with dummy points
for a in [100,300,500]:
    plt.scatter([],[],c='k',alpha=0.5,s=a,label=str(a)+'km$^2$')

plt.legend(scatterpoints=1, frameon=False,
          labelspacing=1, loc='lower left')
<matplotlib.legend.Legend at 0x7f0308093b50>