Skip to content

Commit

Permalink
Merge pull request #4 from health-data-science-OR/2024
Browse files Browse the repository at this point in the history
2024 update ( v4.0.0)
  • Loading branch information
TomMonks committed Dec 11, 2023
2 parents e88f336 + 71516c8 commit 5f3dfff
Show file tree
Hide file tree
Showing 5 changed files with 2,629 additions and 278 deletions.
27 changes: 13 additions & 14 deletions binder/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ name: hds_logistics
channels:
- conda-forge
dependencies:
- contextily=1.2.0
- folium=0.13.0
- geopandas=0.12.1
- contextily=1.4.0
- folium=0.15.1
- geopandas=0.14.1
- geoplot=0.5.1
- jupyterlab=3.4.6
- jupyterlab-spellchecker=0.7.2
- mapclassify=2.4.3
- matplotlib=3.5.3
- numpy=1.23.2
- pandas=1.4.4
- pip=22.2.2
- pysal=2.7.0
- python=3.8.12
- scipy=1.9.1
- seaborn=0.11.1
- jupyterlab=4.0.9
- jupyterlab-spellchecker=0.8.4
- matplotlib=3.8.2
- numpy=1.26.2
- pandas=2.1.4
- pip=23.3.1
- pysal=23.7
- python=3.10.13
- scipy=1.11.4
- seaborn=0.13.0
7 changes: 7 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changes

# v4.0.0
* Updated conda environment.yml to Python 3.10 and latest versions of libraries Dec 2023.
* Updated Lab1
* removed stamin base maps as these are no longer supported
* Update inset map code to use new version of `matplotlib` API


# v3.0.0
* Tested for Academic Year 2022/23
* Updated conda environment.yml to latest versions of libraries
123 changes: 67 additions & 56 deletions mapping/01_geospatial_analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from mpl_toolkits.axes_grid1.inset_locator import (inset_axes, \n",
" InsetPosition,\n",
" mark_inset)\n",
"\n",
"import geopandas as gpd\n",
"import contextily as ctx\n",
"import folium"
Expand Down Expand Up @@ -497,11 +495,12 @@
"```python\n",
"\n",
"# convert your data to a flat projection\n",
"gdf_exeter_admit_flat = gdf_exeter_admit.to_crs(epsg=3857)\n",
"gdf_exeter_admit = gdf_exeter_admit.to_crs(epsg=3857)\n",
"sw_acute = sw_acute.to_crs(epsg=3857)\n",
"\n",
"# create map of IMD deciles\n",
"ax = gdf_exeter_admit_flat.plot(column='IMD_Decile', cmap='autumn',\n",
" legend=True, figsize=(15,9))\n",
"ax = gdf_exeter_admit.plot(column='IMD_Decile', cmap='autumn',\n",
" legend=True, figsize=(15,9))\n",
"\n",
"#add base map using contextily\n",
"ctx.add_basemap(ax, \n",
Expand All @@ -516,8 +515,12 @@
"* \"OpenStreetMap.Mapnik\"\n",
"* \"CartoDB.Positron\"\n",
"* \"CartoDB.Voyager\"\n",
"* \"Stamen.Toner\"\n",
"* \"Stamen.TonerLines\"\n",
"\n",
"you can see all provider by running\n",
"\n",
"```python\n",
"ctx.providers\n",
"```\n",
"\n",
"# Exercise 8\n",
"\n",
Expand Down Expand Up @@ -572,10 +575,13 @@
" facecolor='r', \n",
" markersize=200, \n",
" marker='*')\n",
"\n",
"#### CODE UPDATED 2024. all stamen tiles deprecated.\n",
"# base map using contextily\n",
"ctx.add_basemap(ax,\n",
" source=ctx.providers.Stamen.Watercolor,\n",
" source=ctx.providers.OpenStreetMap.Mapnik,\n",
" zoom=10)\n",
"#####\n",
"\n",
"ax.set_axis_off()\n",
"ax.set_title('IMD Decile by LSOA')\n",
Expand Down Expand Up @@ -862,14 +868,12 @@
"source": [
"## Step 3: Creating an inset map\n",
"\n",
"Matplotlib makes this relatively straight forward using the `axes_grid1.inset_locator` module. You have already imported what is need at the start of this notebook using the code:\n",
"Matplotlib makes this relatively straight forward. The inset map API has changed recently and I recommend using the new approach below as the old method will be deprecated in future releases. \n",
"\n",
"```python\n",
"from mpl_toolkits.axes_grid1.inset_locator import (inset_axes, \n",
" InsetPosition,\n",
" mark_inset)\n",
"```\n",
"The basic logic requires you to create a map and then use `Axes.inset_axes` to create a the inset axis within the main axis. Full documentation and links to other examples can be found in the [main documentation](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.inset_axes.html#matplotlib.axes.Axes.inset_axes)\n",
"\n",
"It is alos possible to add \"zoom lines\" from the main map to the inset. To do this you need to use the `Axis.indicate_inset_zoom` method.\n",
" \n",
"# Optional Exercise 1\n",
"The code below generates an inset map of Exeter and includes explanation comments.\n",
"\n",
Expand Down Expand Up @@ -910,13 +914,13 @@
" ax=ax1)\n",
"\n",
"\n",
"#create the inset\n",
"ax2 = plt.axes([0,0,1,1])\n",
"#### UPDATED code for 2024. New simpler inset API\n",
"# create the inset\n",
"# set the lower left x, y location and relative size of the bounding box\n",
"# note these are in fractional units.\n",
"ax2 = ax1.inset_axes([0.025,0.5,0.45,0.45])\n",
"\n",
"#set the lower left x, y location and relative size of the bounding box\n",
"#note these are in fractional units.\n",
"ip = InsetPosition(ax1, [0.025,0.5,0.45,0.45])\n",
"ax2.set_axes_locator(ip)\n",
"#####\n",
"\n",
"#plot the exeter LSOA inset\n",
"ax2 = exeter_city_rate.plot(column='admits_per_1k',\n",
Expand All @@ -927,44 +931,45 @@
" ax=ax2)\n",
"\n",
"\n",
"#manual text within inset\n",
"# manual text within inset\n",
"ax2.text(-3.575, 50.755,'Exeter detail', fontsize='large')\n",
"\n",
"\n",
"#plot the RDE and only\n",
"# plot the RDE and only\n",
"ax2 = sw_acute.loc[sw_acute['acute'] == 'RDE'].plot(ax=ax2, edgecolor='k', \n",
" facecolor='r', \n",
" markersize=200, \n",
" marker='*')\n",
"\n",
"#formatting tweaks...\n",
"# formatting tweaks...\n",
"\n",
"#alight legend left\n",
"# align legend left\n",
"leg = ax1.get_legend()\n",
"leg._legend_box.align = \"left\"\n",
"\n",
"#set axis 1 limits to provide enough space for exeter\n",
"#this might take a bit of trial and error\n",
"# set axis 1 limits to provide enough space for exeter\n",
"# this might take a bit of trial and error\n",
"ax1.set_ylim(50.5, 51.6)\n",
"ax1.set_xlim(-4.5, -2.75)\n",
"\n",
"#hide axis tick marks\n",
"# hide axis tick marks\n",
"ax2.tick_params(axis=u'both', which=u'both',length=0)\n",
"\n",
"#hide axis lat long labels, but keep bounding box\n",
"# hide axis lat long labels, but keep bounding box\n",
"ax2.set_xticklabels([])\n",
"ax2.set_yticklabels([])\n",
"\n",
"#this draw a box aropund the area where extra detail is needed and\n",
"#'zoom out' lines to the detail.\n",
"#this is optional and the chart might look better without it.\n",
"#try changing loc1 and loc2 between 1 and 4 to see what happens\n",
"#ls = line style '-' for solid, '--' for dashed\n",
"#ec = edge colour\n",
"#fc = face colour \n",
"mark_inset(ax1, ax2, loc1=1, loc2=3, ls='--', ec='k', fc='none')\n",
"\n",
"#hide the box and axis labels\n",
"# #### UPDATED CODE 2024 - new interface\n",
"# this draw a box around the area where extra detail is needed and\n",
"# 'zoom out' lines to the detail.\n",
"# this is optional and the chart might look better without it.\n",
"# ls = line style '-' for solid, '--' for dashed\n",
"# ec = edge colour\n",
"# fc = face colour \n",
"ax1.indicate_inset_zoom(ax2, edgecolor=\"black\", ls='--', ec='black', fc=None)\n",
"# ####\n",
"\n",
"# hide the box and axis labels\n",
"ax1.set_axis_off()\n",
"\n",
"fig.savefig('images/insert_image.png', bbox_inches='tight')"
Expand Down Expand Up @@ -999,7 +1004,6 @@
"* “OpenStreetMap”\n",
"* “Mapbox Bright” (Limited levels of zoom for free tiles)\n",
"* “Mapbox Control Room” (Limited levels of zoom for free tiles)\n",
"* “Stamen” (Terrain, Toner, and Watercolor)\n",
"* “Cloudmade” (Must pass API key)\n",
"* “Mapbox” (Must pass API key)\n",
"* “CartoDB” (positron and dark_matter)\n",
Expand All @@ -1008,8 +1012,7 @@
"\n",
"```python\n",
"#add some different background layers\n",
"tiles = ['stamenwatercolor', 'cartodbpositron', 'cartodbdark_matter', \n",
" 'openstreetmap', 'stamenterrain', 'stamentoner']\n",
"tiles = ['cartodbpositron', 'cartodbdark_matter', 'openstreetmap']\n",
"for tile in tiles:\n",
" folium.TileLayer(tile).add_to(stroke_map)\n",
"\n",
Expand Down Expand Up @@ -1073,38 +1076,39 @@
"stroke_map = folium.Map(location=[50.71671, -3.50668], zoom_start=9, \n",
" tiles='cartodbpositron')\n",
"\n",
"#add some different background layers\n",
"tiles = ['stamenwatercolor', 'cartodbpositron', 'cartodbdark_matter', \n",
" 'openstreetmap', 'stamenterrain', 'stamentoner']\n",
"#### Updated code 2024\n",
"# add some different background layers\n",
"tiles = ['cartodbpositron', 'cartodbdark_matter', 'openstreetmap']\n",
"####\n",
"\n",
"for tile in tiles:\n",
" folium.TileLayer(tile).add_to(stroke_map)\n",
"\n",
"#filter data to display\n",
"# filter data to display\n",
"fields = ['LSOA11NM', 'admissions', 'admits_per_1k', 'IMD_Decile']\n",
"lsoa_tia = gdf_exeter_admit[fields]\n",
"\n",
"#field to color; 0 = LSOA11NM; 1 = 'admissions' etc.\n",
"# field to color; 0 = LSOA11NM; 1 = 'admissions' etc.\n",
"field_index = 3\n",
"\n",
"#create and add choropleth map\n",
"# create and add choropleth map\n",
"choropleth = folium.Choropleth(\n",
" #pass the GeoDataFrame\n",
" # pass the GeoDataFrame\n",
" geo_data=gdf_exeter_admit,\n",
" #Data for overlaying\n",
" # Data for overlaying\n",
" data=lsoa_tia,\n",
" #columns = [key, field to plot]\n",
" #c olumns = [key, field to plot]\n",
" columns=['LSOA11NM', fields[field_index]],\n",
" key_on='feature.properties.LSOA11NM',\n",
" #choose the fill colour\n",
" # choose the fill colour\n",
" fill_color='OrRd',\n",
" #set the legent name\n",
" # set the legent name\n",
" legend_name=fields[field_index],\n",
" #highlight the LSOA shape when mouse pointer enters it\n",
" # highlight the LSOA shape when mouse pointer enters it\n",
" highlight=True,\n",
" smooth_factor=0).add_to(stroke_map) \n",
" \n",
"#add layer control to map - make sure you do this after adding choropleth map\n",
"# add layer control to map - make sure you do this after adding choropleth map\n",
"folium.LayerControl().add_to(stroke_map)\n",
"\n",
"stroke_map"
Expand Down Expand Up @@ -1148,6 +1152,13 @@
"source": [
"# your code here ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -1166,7 +1177,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 5f3dfff

Please sign in to comment.