{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "import folium\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "# the geojson data file\n", "SA_geo = '../data/SA_regions.json' \n", "\n", "# initialize the map at (23, 45) which is Saudi Arabia\n", "SA_map = folium.Map(location=[23,45], zoom_start=5)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Region200320022001
0Riyadh366769.0422882.0392459.0
1Makkah285647.0246865.0253335.0
2Madinah267826.0239032.0205698.0
3Qassim165556.0169599.0118732.0
4Eastern Region83441.084432.053778.0
\n", "
" ], "text/plain": [ " Region 2003 2002 2001\n", "0 Riyadh 366769.0 422882.0 392459.0\n", "1 Makkah 285647.0 246865.0 253335.0\n", "2 Madinah 267826.0 239032.0 205698.0\n", "3 Qassim 165556.0 169599.0 118732.0\n", "4 Eastern Region 83441.0 84432.0 53778.0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# read the data we want to visualize (obtained from data.gov.sa)\n", "data = pd.read_csv('./goats.csv')\n", "data.head()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bins = list(data['2001'].quantile([0, 0.25, 0.5, 0.75, 1]))\n", "\n", "choropleth = folium.Choropleth(\n", " geo_data=SA_geo,\n", " name='choropleth',\n", " data=data,\n", " columns=['Region', '2001'],\n", " key_on='feature.properties.name',\n", " fill_color='YlGn',\n", " fill_opacity=0.7,\n", " line_opacity=0.3, \n", " legend_name='Estimated Number of Goats in 2001',\n", " bins=bins,\n", " reset=True\n", ").add_to(SA_map)\n", "\n", "style_function = \"font-size: 15px; font-weight: bold\"\n", "choropleth.geojson.add_child(\n", " folium.features.GeoJsonTooltip(['name'], style=style_function, labels=False))\n", "\n", "# create a layer control\n", "folium.LayerControl().add_to(SA_map)\n", "\n", "SA_map" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "SA_map.save('SA_regions_map.html')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7" }, "widgets": { "state": {}, "version": "1.1.2" } }, "nbformat": 4, "nbformat_minor": 2 }