mirror of
https://github.com/wassname/geopandas.git
synced 2026-10-03 12:31:14 +08:00
197 lines
5.2 KiB
Plaintext
197 lines
5.2 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"# Plotting with Geoplot and GeoPandas\n",
|
|
"\n",
|
|
"[Geoplot](https://residentmario.github.io/geoplot/index.html) is a Python\n",
|
|
"library providing a selection of easy-to-use geospatial visualizations. It is\n",
|
|
"built on top of the lower-level [CartoPy](http://scitools.org.uk/cartopy/),\n",
|
|
"covered in a separate section of this tutorial, and is designed to work with\n",
|
|
"GeoPandas input.\n",
|
|
"\n",
|
|
"This example is a brief tour of the `geoplot` API. For more details on the\n",
|
|
"library refer to [its documentation](https://residentmario.github.io/geoplot/index.html).\n",
|
|
"\n",
|
|
"First we'll load in the data using GeoPandas.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import geopandas\n",
|
|
"import geoplot\n",
|
|
"\n",
|
|
"world = geopandas.read_file(\n",
|
|
" geopandas.datasets.get_path('naturalearth_lowres')\n",
|
|
")\n",
|
|
"boroughs = geopandas.read_file(\n",
|
|
" geoplot.datasets.get_path('nyc_boroughs')\n",
|
|
")\n",
|
|
"collisions = geopandas.read_file(\n",
|
|
" geoplot.datasets.get_path('nyc_injurious_collisions')\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Plotting with Geoplot\n",
|
|
"=====================\n",
|
|
"\n",
|
|
"We start out by replicating the basic GeoPandas world plot using Geoplot.\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"geoplot.polyplot(world, figsize=(8, 4))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Geoplot can re-project data into any of the map projections provided by\n",
|
|
"CartoPy (see the list\n",
|
|
"[here](http://scitools.org.uk/cartopy/docs/latest/crs/projections.html)).\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# use the Orthographic map projection (e.g. a world globe)\n",
|
|
"ax = geoplot.polyplot(\n",
|
|
" world, projection=geoplot.crs.Orthographic(), figsize=(8, 4)\n",
|
|
")\n",
|
|
"ax.outline_patch.set_visible(True)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"``polyplot`` is trivial and can only plot the geometries you pass to it. If\n",
|
|
"you want to use color as a visual variable, specify a ``choropleth``. Here\n",
|
|
"we sort GDP per person by country into five buckets by color, using\n",
|
|
"\"quantiles\" binning from the [Mapclassify](https://pysal.org/mapclassify/)\n",
|
|
"library.\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import mapclassify\n",
|
|
"gpd_per_person = world['gdp_md_est'] / world['pop_est']\n",
|
|
"scheme = mapclassify.Quantiles(gpd_per_person, k=5)\n",
|
|
"\n",
|
|
"# Note: this code sample requires geoplot>=0.4.0.\n",
|
|
"geoplot.choropleth(\n",
|
|
" world, hue=gpd_per_person, scheme=scheme,\n",
|
|
" cmap='Greens', figsize=(8, 4)\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"If you want to use size as a visual variable, use a ``cartogram``. Here are\n",
|
|
"population estimates for countries in Africa.\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"africa = world.query('continent == \"Africa\"')\n",
|
|
"ax = geoplot.cartogram(\n",
|
|
" africa, scale='pop_est', limits=(0.2, 1),\n",
|
|
" edgecolor='None', figsize=(7, 8)\n",
|
|
")\n",
|
|
"geoplot.polyplot(africa, edgecolor='gray', ax=ax)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"If we have data in the shape of points in space, we may generate a\n",
|
|
"three-dimensional heatmap on it using ``kdeplot``.\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"tags": [
|
|
"nbsphinx-thumbnail"
|
|
]
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"ax = geoplot.kdeplot(\n",
|
|
" collisions.head(1000), clip=boroughs.geometry,\n",
|
|
" shade=True, cmap='Reds',\n",
|
|
" projection=geoplot.crs.AlbersEqualArea())\n",
|
|
"geoplot.polyplot(boroughs, ax=ax, zorder=1)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"These are just some of the plots you can make with Geoplot. There are\n",
|
|
"many other possibilities not covered in this brief introduction. For more\n",
|
|
"examples, refer to the\n",
|
|
"[Gallery](https://residentmario.github.io/geoplot/gallery/index.html) in\n",
|
|
"the Geoplot documentation.\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"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.6"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
} |