-
Notifications
You must be signed in to change notification settings - Fork 229
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
Add gallery example showing usage of polygon objects from a geopandas.GeoDataFrame (choropleth map) #2796
Changes from 11 commits
ddda358
cf53b9b
999545a
4bd9f7f
1b0db1e
784df8b
d225921
72a342a
bd158c8
4afbe1c
26c6d73
6f9ccdd
9654a20
2fa4e9f
961e352
3ffc2da
d81aefb
86d90a8
c53869f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
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="M10c", | ||
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. | ||
michaelgrund marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fig.plot( | ||
data=gdf[["population", "geometry"]], | ||
michaelgrund marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pen="0.3p,gray10", | ||
close=True, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel we should explain that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is something I am not 100 % sure about. I orientated myself on the EGU short course example for a choropleth map (https://www.generic-mapping-tools.org/egu22pygmt/ecosystem.html#plotting-geospatial-vector-data-with-geopandas-and-pygmt). And there, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When plotting polygons without fills, When filling polygons, the polygons are always "closed" by default. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then I would suggest to remove it in the example. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm. Based on this, it seems to me, we do not need to explicitly set the I just tried to plot the polygons of this GeoDataFrame without any fill. Also this works for me correctly without using import geopandas as gpd
import pygmt
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="+t ")
fig.plot(data=gdf)
fig.show() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is an example showing how
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @seisman! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am wondering whether we should update the code example for the choropleth map of the EGU22 short course and remove the |
||
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+w6c/0.5c") | ||
michaelgrund marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
fig.show() |
Uh oh!
There was an error while loading. Please reload this page.