{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nPopulation by macroregion\n=========================\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In this example we want to visualize the most recent data on\nbrazilian population by macroregion.\n\nLet's first retrieve the data with the `seriesbr <seriesbr.readthedocs.io>`_ package.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from seriesbr import ibge\n\npopulation = ibge.get_series(6462, 606, macroregions=True, last_n=1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Then let's convert this ``DataFrame`` into a ``GeoDataFrame`` by assigning\nto a column named \"geometry\" the macroregions' geometric objects.\n\nYou can get a geometric object for a given location with the function\n:py:func:`mapsbr.ibgemaps.geocode`. It can either be a location code or name. If\nit's a name, you'll need to pass which is its geographic level to avoid ambiguity.\n\nFor example, ``ibgemaps.geocode(\"Rio de Janeiro\", geolevel=\"state\")`` if you want Rio\nde Janeiro state map or ``ibgemaps.geocode(\"Rio de Janeiro\", geolevel=\"municipality\")`` if\nthe map for Rio de Janeiro city.\n\nNotice that the column with the geometric objects **has** to be named geometry. Otherwise\nyou'll need to call the ``set_geometry(\"geometries_column\")`` method on the\n``GeoDataFrame``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import geopandas as gpd\nfrom mapsbr import ibgemaps\n\ngdf = gpd.GeoDataFrame(population)\ngdf[\"geometry\"] = ibgemaps.geocode(population[\"Grande Regi\u00e3o\"], geolevel=\"macroregion\")\ngdf.plot(column=\"Valor\", legend=True, edgecolor=\"w\")\n\nimport matplotlib.pyplot as plt\n\nplt.gca().axis(\"off\")\nplt.show()"
      ]
    }
  ],
  "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.7.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}