Skip to content

Commit

Permalink
plotting with contextily and geoviews
Browse files Browse the repository at this point in the history
  • Loading branch information
ajfriend committed Nov 12, 2023
1 parent 1fea86c commit d52679a
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 8 deletions.
16 changes: 8 additions & 8 deletions poly/malaria.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,25 @@
{
"cell_type": "code",
"execution_count": null,
"id": "497350a3-fce8-420f-8a73-304c088dcd9d",
"id": "7db24528-5622-4c85-8c8c-2bba1cb28459",
"metadata": {},
"outputs": [],
"source": [
"def show_gj_str(gj_str):\n",
" import json\n",
" from IPython.display import GeoJSON\n",
" \n",
" return GeoJSON(json.loads(gj_str))"
"colombia.crs"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e7fed94f-6ab0-43b8-97eb-f7a0a7483c41",
"id": "497350a3-fce8-420f-8a73-304c088dcd9d",
"metadata": {},
"outputs": [],
"source": [
"colombia["
"def show_gj_str(gj_str):\n",
" import json\n",
" from IPython.display import GeoJSON\n",
" \n",
" return GeoJSON(json.loads(gj_str))"
]
},
{
Expand Down
180 changes: 180 additions & 0 deletions poly/plotting.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "595a3eea-392d-44e1-a657-1a2771b08947",
"metadata": {},
"outputs": [],
"source": [
"import h3\n",
"import geopandas\n",
"import geodatasets\n",
"import contextily as cx\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e971ca2-24a4-43ba-8a90-83f6091b462b",
"metadata": {},
"outputs": [],
"source": [
"2263 # new york\n",
"4326 # wgs84\n",
"3857 # web mercator"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "92bb0020-9c94-474f-90c1-96faa8744759",
"metadata": {},
"outputs": [],
"source": [
"df = geopandas.read_file(geodatasets.get_path('nybb'))\n",
"df.crs"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9df12bd5-b36e-413c-a18a-67ae857f9daf",
"metadata": {},
"outputs": [],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1b12a4bc-196c-4acb-931e-8cbe76a8d7c1",
"metadata": {},
"outputs": [],
"source": [
"def foo(df, epsg):\n",
" df = df.to_crs(epsg=epsg)\n",
" \n",
" ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k', column='BoroName', categorical=True, legend=True, legend_kwds={'loc': 'upper left'})\n",
" \n",
" ax.get_xaxis().set_visible(False)\n",
" ax.get_yaxis().set_visible(False)\n",
" \n",
" cx.add_basemap(ax, crs=df.crs, source=cx.providers.CartoDB.Positron)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d76ec57d-83f0-4c69-8c67-6536ca881f65",
"metadata": {},
"outputs": [],
"source": [
"foo(df, 2263)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cfd6380d-8b2c-4176-8c50-48bea9bd04c3",
"metadata": {},
"outputs": [],
"source": [
"foo(df, 4326)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8e948d0d-f6a0-4306-a23e-4feb99de0e4e",
"metadata": {},
"outputs": [],
"source": [
"foo(df, 3857)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee42f565-4b74-469a-9adb-0a4b448ce685",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "2ad7aa3c-e249-4ef7-b760-d25794e405df",
"metadata": {},
"outputs": [],
"source": [
"import geodatasets as gds\n",
"import geopandas as gpd\n",
"import geoviews as gv\n",
"import cartopy.crs as ccrs\n",
"\n",
"gv.extension('bokeh');"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c9964ae-c52f-464c-ab7c-965794fe918a",
"metadata": {},
"outputs": [],
"source": [
"def bar(df, epsg):\n",
" crs = ccrs.epsg(epsg)\n",
" df = df.to_crs(crs)\n",
" \n",
" tiles = gv.tile_sources.CartoLight\n",
" polys = gv.Polygons(df, vdims=['BoroName'], crs=df.crs)\n",
"\n",
" out = tiles * polys.opts(color='BoroName', cmap='Pastel1', tools=['hover'], width=1200, height=800, alpha=0.5, active_tools=['wheel_zoom'], xaxis=None, yaxis=None)\n",
"\n",
" return out"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8a3d3d09-e57c-4985-a246-e2e27ff85157",
"metadata": {},
"outputs": [],
"source": [
"bar(df, 3857)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "831de887-a746-430b-a586-5e7069581d95",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit d52679a

Please sign in to comment.