{ "cells": [ { "cell_type": "markdown", "id": "0bacc36e-61b7-4e64-8772-0a7de2934df0", "metadata": {}, "source": [ "## Population Pyramid" ] }, { "cell_type": "code", "execution_count": 1, "id": "8801b61c-5eea-468d-953c-90bc61df7a26", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This function renders only package's internal data, it might not be the most up-to-date.\n", "Have a look at api.insee.fr !\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Getting data: 0%| | 0/1 [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Subscribe to api.insee.fr and get your credentials!\n", "# Save your credentials with init_conn function :\n", "# from pynsee.utils import init_conn\n", "# init_conn(insee_key=\"my_insee_key\", insee_secret=\"my_insee_secret\")\n", "\n", "# Beware : any change to the keys should be tested after having cleared the cache\n", "# Please do : from pynsee.utils import clear_all_cache; clear_all_cache()\"\n", "\n", "from pynsee.localdata import get_local_metadata, get_local_data\n", "\n", "import pandas as pd\n", "pd.options.mode.chained_assignment = None \n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "\n", "import logging\n", "import sys\n", "logging.basicConfig(stream=sys.stdout,\n", " level=logging.INFO, \n", " format=\"%(message)s\")\n", "\n", "metadata = get_local_metadata()\n", "\n", "data = get_local_data(dataset_version='GEO2020RP2017',\n", " variables = 'SEXE-AGED100',\n", " nivgeo = 'FE',\n", " geocodes=['1'])\n", "\n", "dataM = data[(data.SEXE == '1') & (data.AGED100 != 'ENS')]\n", "dataF = data[(data.SEXE == '2') & (data.AGED100 != 'ENS')]\n", "\n", "dataF['OBS_VALUE'] = dataF['OBS_VALUE'].apply(lambda x: x * -1)\n", "\n", "# define plot\n", "y = range(0, len(dataM))\n", "x_male = dataM['OBS_VALUE']\n", "x_female = dataM['OBS_VALUE']\n", "\n", "#define plot parameters\n", "fig, axes = plt.subplots(ncols=2, sharey=True, figsize=(12, 5))\n", "fig.suptitle(\"Population Pyramid in France in 2017\", fontweight='bold')\n", "fig.tight_layout(pad = 2)\n", "#define male and female bars\n", "axes[0].barh(y, x_male, align='center', color='blue')\n", "axes[0].set(title='Males')\n", "axes[1].barh(y, x_female, align='center', color='red')\n", "axes[1].set(title='Females')\n", "axes[0].invert_xaxis()\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }