-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plotting with contextily and geoviews
- Loading branch information
Showing
2 changed files
with
188 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |