{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import cartopy.crs as ccrs\n",
    "import matplotlib.pyplot as plt\n",
    "import cartopy.io.shapereader as shpreader\n",
    "import matplotlib as mpl\n",
    "import pandas as pd\n",
    "from dautil import options\n",
    "from dautil import data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "countries = shpreader.natural_earth(resolution='110m',\n",
    "                                    category='cultural',\n",
    "                                    name='admin_0_countries')\n",
    "\n",
    "\n",
    "cities = pd.read_csv(data.Nordpil().load_urban_tsv(),\n",
    "                     sep='\\t', encoding='ISO-8859-1')\n",
    "mill_cities = cities[cities['pop2005'] > 1000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "plt.figure(figsize=(16, 12))\n",
    "gs = mpl.gridspec.GridSpec(2, 1,\n",
    "                           height_ratios=[20, 1])\n",
    "ax = plt.subplot(gs[0], projection=ccrs.PlateCarree())\n",
    "\n",
    "norm = mpl.colors.Normalize(vmin=0, vmax=2 * 10 ** 9)\n",
    "cmap = plt.cm.Blues\n",
    "ax.set_title('Population Estimates by Country')\n",
    "\n",
    "for country in shpreader.Reader(countries).records():\n",
    "    ax.add_geometries(country.geometry, ccrs.PlateCarree(),\n",
    "                      facecolor=cmap(\n",
    "                          norm(country.attributes['pop_est'])))\n",
    "    \n",
    "plt.plot(mill_cities['Longitude'],\n",
    "         mill_cities['Latitude'], 'r.',\n",
    "         label='Populous city',\n",
    "         transform=ccrs.PlateCarree())\n",
    "\n",
    "options.set_mpl_options()\n",
    "plt.legend(loc='lower left')\n",
    "\n",
    "cax = plt.subplot(gs[1])\n",
    "cb = mpl.colorbar.ColorbarBase(cax,\n",
    "                               cmap=cmap,\n",
    "                               norm=norm,\n",
    "                               orientation='horizontal')\n",
    "\n",
    "cb.set_label('Population Estimate')\n",
    "plt.tight_layout()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
