官术网_书友最值得收藏!

Displaying geographical maps

Whether dealing with local of global data, geographical maps are a suitable visualization. To plot data on a map, we need coordinates, usually in the form of latitude and longitude values. Several file formats exist with which we can save geographical data. In this recipe, we will use the special shapefile format and the more common tab separated values (TSV) format. The shapefile format was created by the Esri company and uses three mandatory files with the extensions .shp , .shx , and .dbf. The .dbf file contains a database with extra information for each geographical location in the shapefile. The shapefile we will use contains information about country borders, population, and Gross Domestic Product (GDP). We can download the shapefile with the cartopy library. The TSV file holds population data for more than 4000 cities as a timeseries. It comes from https://nordpil.com/resources/world-database-of-large-cities/ (retrieved July 2015).

Getting ready

First, we need to install Proj.4 from source or, if you are lucky, using a binary distribution from available at https://github.com/OSGeo/proj.4 (retrieved July 2015). Then, install cartopy with pip—I wrote the code with cartopy-0.13.0. Alternatively, we can run the following command:

$ conda install -c scitools cartopy

How to do it...

  1. The imports are as follows:
    import cartopy.crs as ccrs
    import matplotlib.pyplot as plt
    import cartopy.io.shapereader as shpreader
    import matplotlib as mpl
    import pandas as pd
    from dautil import options
    from dautil import data
  2. We will use color to visualize country populations and populous cities. Load the data as follows:
    countries = shpreader.natural_earth(resolution='110m',
                                        category='cultural',
                                        name='admin_0_countries')
    
    
    cities = pd.read_csv(data.Nordpil().load_urban_tsv(),
                         sep='\t', encoding='ISO-8859-1')
    mill_cities = cities[cities['pop2005'] > 1000]
  3. Draw a map, a corresponding colorbar, and mark populous cities on the map with the following code:
    %matplotlib inline
    plt.figure(figsize=(16, 12))
    gs = mpl.gridspec.GridSpec(2, 1,
                               height_ratios=[20, 1])
    ax = plt.subplot(gs[0], projection=ccrs.PlateCarree())
    
    norm = mpl.colors.Normalize(vmin=0, vmax=2 * 10 ** 9)
    cmap = plt.cm.Blues
    ax.set_title('Population Estimates by Country')
    
    for country in shpreader.Reader(countries).records():
        ax.add_geometries(country.geometry, ccrs.PlateCarree(),
                          facecolor=cmap(
                              norm(country.attributes['pop_est'])))
        
    plt.plot(mill_cities['Longitude'],
             mill_cities['Latitude'], 'r.',
             label='Populous city',
             transform=ccrs.PlateCarree())
    
    options.set_mpl_options()
    plt.legend(loc='lower left')
    
    cax = plt.subplot(gs[1])
    cb = mpl.colorbar.ColorbarBase(cax,
                                   cmap=cmap,
                                   norm=norm,
                                   orientation='horizontal')
    
    cb.set_label('Population Estimate')
    plt.tight_layout()

Refer to the following plot for the end result:

You can find the code in the plot_map.ipynb file in this book's code bundle.

主站蜘蛛池模板: 西吉县| 定兴县| 南丰县| 襄樊市| 衡阳县| 土默特左旗| 咸阳市| 淮北市| 黄浦区| 唐河县| 双牌县| 丹巴县| 邳州市| 嘉善县| 临沂市| 牟定县| 武冈市| 馆陶县| 红河县| 锦州市| 宝山区| 台北县| 阳高县| 陈巴尔虎旗| 平昌县| 旺苍县| 原阳县| 开鲁县| 镇远县| 玉树县| 青浦区| 卓尼县| 资兴市| 海原县| 淮北市| 漾濞| 朝阳区| 公主岭市| 怀柔区| 黑山县| 泰和县|