-
Notifications
You must be signed in to change notification settings - Fork 228
Add gallery example showing usage of polygon objects from a geopandas.GeoDataFrame (choropleth map) #2796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add gallery example showing usage of polygon objects from a geopandas.GeoDataFrame (choropleth map) #2796
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ddda358
Create polygons.py
michaelgrund cf53b9b
[format-command] fixes
actions-bot 999545a
Apply suggestions from code review
michaelgrund 4bd9f7f
remove np import
michaelgrund 1b0db1e
Apply suggestions from code review
michaelgrund 784df8b
Update and rename polygons.py to choropleth_map.py
michaelgrund d225921
Apply suggestions from code review
michaelgrund 72a342a
Rename choropleth_map.py to choropleth_map.py
michaelgrund bd158c8
Update choropleth_map.py
michaelgrund 4afbe1c
Update choropleth_map.py
michaelgrund 26c6d73
Merge branch 'main' into add-gallery-polygons
michaelgrund 6f9ccdd
Apply suggestions from code review
michaelgrund 9654a20
Update choropleth_map.py
michaelgrund 2fa4e9f
Merge branch 'main' into add-gallery-polygons
michaelgrund 961e352
Update choropleth_map.py
michaelgrund 3ffc2da
Merge branch 'main' into add-gallery-polygons
michaelgrund d81aefb
remove close parameter
michaelgrund 86d90a8
Merge branch 'main' into add-gallery-polygons
michaelgrund c53869f
Apply suggestions from code review
michaelgrund File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,58 @@ | ||
""" | ||
Choropleth Map | ||
============== | ||
|
||
The :meth:`pygmt.Figure.plot` method allows us to plot geographical data such | ||
as polygons which are stored in a :class:`geopandas.GeoDataFrame` object. Use | ||
:func:`geopandas.read_file` to load data from any supported OGR format such as | ||
a shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. You can also | ||
use a full URL pointing to your desired data source. Then, pass the | ||
:class:`geopandas.GeoDataFrame` as an argument to the ``data`` parameter of | ||
:meth:`pygmt.Figure.plot`, and style the geometry using the ``pen`` parameter. | ||
To fill the polygons based on a corresponding column you need to set | ||
``fill="+z"`` as well as select the appropriate column using the ``aspatial`` | ||
parameter as shown in the example below. | ||
""" | ||
|
||
import geopandas as gpd | ||
import pygmt | ||
|
||
# Read polygon data using geopandas | ||
gdf = gpd.read_file("https://geodacenter.github.io/data-and-lab/data/airbnb.zip") | ||
|
||
fig = pygmt.Figure() | ||
|
||
fig.basemap( | ||
region=gdf.total_bounds[[0, 2, 1, 3]], | ||
projection="M6c", | ||
frame="+tPopulation of Chicago", | ||
) | ||
|
||
# The dataset contains different attributes, here we select | ||
# the "population" column to plot. | ||
|
||
# First, we define the colormap to fill the polygons based on | ||
# the "population" column. | ||
pygmt.makecpt( | ||
cmap="acton", | ||
series=[gdf["population"].min(), gdf["population"].max(), 10], | ||
continuous=True, | ||
reverse=True, | ||
) | ||
|
||
# Next, we plot the polygons and fill them using the defined | ||
# colormap. The target column is defined by the aspatial | ||
# parameter. | ||
fig.plot( | ||
data=gdf, | ||
pen="0.3p,gray10", | ||
fill="+z", | ||
cmap=True, | ||
aspatial="Z=population", | ||
) | ||
|
||
|
||
# Add colorbar legend | ||
michaelgrund marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fig.colorbar(frame="x+lPopulation", position="jML+o-0.5c+w3.5c/0.2c") | ||
|
||
fig.show() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.