{ "cells": [ { "cell_type": "markdown", "id": "52b7e927", "metadata": {}, "source": [ "# How-to: Creating a simple map\n", "\n", "Sometimes it is helpful to be able to look at data geographically. In this how-to, we will see how we can use the package [geopandas](https://geopandas.org/en/stable/) to import data in [geojson](https://geojson.org) format. Then we show how we can relatively easily visualise data on a map. We also show how to use the [geoplot](https://residentmario.github.io/geoplot/) to quickly create some common data-maps.\n", "\n", "We start by loading in our data. You may need to install geopandas first by, for example, using the following command in your terminal: \n", "\n", " conda install -c conda-forge geopandas \n", "\n", "See the Geopandas [getting started](https://geopandas.org/en/stable/getting_started.html) for more details on installation. " ] }, { "cell_type": "code", "execution_count": 48, "id": "eed82646", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import matplotlib as mpt\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import statsmodels.formula.api as smf\n", "import statsmodels.api as sm\n", "\n", "\n", "import geopandas as gpd\n", "import geoplot as gplt" ] }, { "cell_type": "markdown", "id": "ce296fa2", "metadata": {}, "source": [ "geopandas has a built-in data-import function, *read_file()*, that will automatically recognize several types of geographic data. \n", "\n", "Below we will load in a file describing the boundaries of all Norwegian municipalities (kommuner). This is a big file, so it can take a minute to load.\n", "\n", "(I assembled this dataset from the [open API from geonorge](frhttps://ws.geonorge.no/kommuneinfo/v1/#/default/get_kommuner__kommunenummer__omrade))" ] }, { "cell_type": "code", "execution_count": 49, "id": "2dc64695", "metadata": {}, "outputs": [], "source": [ "kommunerShapeDF = gpd.read_file(\"https://jmaurit.github.io/anv_statistikk/data/kommunerShapeDF.geojson\")\n" ] }, { "cell_type": "markdown", "id": "2029e45b", "metadata": {}, "source": [ "We can look at the geopandas dataframe and notice that it looks much like a normal Pandas dataframe, and that is in large part because it is. The differentiating factor is the *geometry* column, which contains the information to define the borders of each municipality (kommune). " ] }, { "cell_type": "code", "execution_count": 50, "id": "5224f393", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | kommunenavn | \n", "area | \n", "kommunenummer | \n", "knummer | \n", "geometry | \n", "
---|---|---|---|---|---|
0 | \n", "TRYSIL | \n", "NO1 | \n", "3421.0 | \n", "3421 | \n", "MULTIPOLYGON (((11.84062 61.39303, 11.84107 61... | \n", "
1 | \n", "OSLO | \n", "NO1 | \n", "301.0 | \n", "0301 | \n", "MULTIPOLYGON (((10.57524 59.98690, 10.57560 59... | \n", "
2 | \n", "BÆRUM | \n", "NO1 | \n", "3024.0 | \n", "3024 | \n", "MULTIPOLYGON (((10.36939 59.93179, 10.36975 59... | \n", "
3 | \n", "NORDRE FOLLO | \n", "NO1 | \n", "3020.0 | \n", "3020 | \n", "MULTIPOLYGON (((10.80297 59.73705, 10.80294 59... | \n", "
4 | \n", "NITTEDAL | \n", "NO1 | \n", "3031.0 | \n", "3031 | \n", "MULTIPOLYGON (((10.76484 60.09539, 10.76520 60... | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
351 | \n", "HASVIK | \n", "NO4 | \n", "5433.0 | \n", "5433 | \n", "MULTIPOLYGON (((21.38443 70.60313, 21.71121 70... | \n", "
352 | \n", "HATTFJELLDAL | \n", "NO4 | \n", "1826.0 | \n", "1826 | \n", "MULTIPOLYGON (((13.82300 65.45053, 13.82294 65... | \n", "
353 | \n", "VEVELSTAD | \n", "NO4 | \n", "1816.0 | \n", "1816 | \n", "MULTIPOLYGON (((12.23998 65.69494, 12.22693 65... | \n", "
354 | \n", "GRANE | \n", "NO4 | \n", "1825.0 | \n", "1825 | \n", "MULTIPOLYGON (((13.17622 65.48674, 13.06642 65... | \n", "
355 | \n", "VEGA | \n", "NO4 | \n", "1815.0 | \n", "1815 | \n", "MULTIPOLYGON (((10.95389 65.97142, 10.79868 65... | \n", "
356 rows × 5 columns
\n", "\n", " | Unnamed: 0 | \n", "region | \n", "year | \n", "contents | \n", "population | \n", "kommunenummer | \n", "med_income_at | \n", "med_income_bt | \n", "
---|---|---|---|---|---|---|---|---|
0 | \n", "19 | \n", "K-3001 Halden | \n", "2005 | \n", "Persons | \n", "27582 | \n", "3001 | \n", "NaN | \n", "NaN | \n", "
1 | \n", "20 | \n", "K-3001 Halden | \n", "2006 | \n", "Persons | \n", "27722 | \n", "3001 | \n", "NaN | \n", "NaN | \n", "
2 | \n", "21 | \n", "K-3001 Halden | \n", "2007 | \n", "Persons | \n", "27835 | \n", "3001 | \n", "NaN | \n", "NaN | \n", "
3 | \n", "22 | \n", "K-3001 Halden | \n", "2008 | \n", "Persons | \n", "28092 | \n", "3001 | \n", "NaN | \n", "NaN | \n", "
4 | \n", "23 | \n", "K-3001 Halden | \n", "2009 | \n", "Persons | \n", "28389 | \n", "3001 | \n", "NaN | \n", "NaN | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
6403 | \n", "13167 | \n", "K-5444 Sør-Varanger | \n", "2018 | \n", "Persons | \n", "10171 | \n", "5444 | \n", "NaN | \n", "NaN | \n", "
6404 | \n", "13168 | \n", "K-5444 Sør-Varanger | \n", "2019 | \n", "Persons | \n", "10156 | \n", "5444 | \n", "NaN | \n", "NaN | \n", "
6405 | \n", "13169 | \n", "K-5444 Sør-Varanger | \n", "2020 | \n", "Persons | \n", "10158 | \n", "5444 | \n", "539000.0 | \n", "668000.0 | \n", "
6406 | \n", "13170 | \n", "K-5444 Sør-Varanger | \n", "2021 | \n", "Persons | \n", "10103 | \n", "5444 | \n", "NaN | \n", "NaN | \n", "
6407 | \n", "13171 | \n", "K-5444 Sør-Varanger | \n", "2022 | \n", "Persons | \n", "9925 | \n", "5444 | \n", "NaN | \n", "NaN | \n", "
6408 rows × 8 columns
\n", "