-
Notifications
You must be signed in to change notification settings - Fork 235
Add Figure.choropleth to plot choropleth maps #2798
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
Open
seisman
wants to merge
22
commits into
main
Choose a base branch
from
choropleth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+119
−14
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c3ebb8e
Add Figure.choropleth to plot choropleth maps
seisman 6617606
Add an example
seisman 63f449d
Update docstrings
seisman 3822832
Add a test
seisman 7c5863c
Add to API doc
seisman 7d381a5
Update test
seisman fac0279
Improve type hints
seisman a1b967e
Update baseline image
seisman 9c76b1e
Skip doctest'
seisman 1084696
Remove one blank line in doctest
seisman b699d99
Use a subset
seisman 0be0365
Revert "Use a subset"
seisman 4063455
Update the doctest
seisman f5a89b8
Fix typos [skip ci]
seisman 40f3ce4
Fix a typo [skip ci]
seisman 905ed8c
Fix a typo [skip ci]
seisman 953613f
Fix styling
seisman bd07f28
Merge branch 'main' into choropleth
seisman 73b0bf3
Fix merging conflicts
seisman 77fb8b8
Merge branch 'main' into choropleth
seisman 4b3dc43
Merge branch 'main' into choropleth
seisman 56eb346
Merge branch 'main' into choropleth
seisman 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
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
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
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
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
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,59 @@ | ||
| """ | ||
| choropleth - Plot a choropleth map. | ||
| """ | ||
|
|
||
| from pygmt._typing import GeoLike, PathLike | ||
|
|
||
| __doctest_skip__ = ["choropleth"] | ||
|
|
||
|
|
||
| def choropleth( | ||
| self, | ||
| data: GeoLike | PathLike, | ||
| column: str, | ||
| cmap: str | bool = True, | ||
| **kwargs, | ||
| ): | ||
| """ | ||
| Plot a choropleth map. | ||
|
|
||
| This method is a thin wrapper around :meth:`pygmt.Figure.plot` that sets the | ||
| appropriate parameters for creating a choropleth map by filling polygons based on | ||
| values in a specified data column. It requires the input data to be a geo-like | ||
| Python object that implements ``__geo_interface__`` (e.g. a | ||
| :class:`geopandas.GeoDataFrame`), or an OGR_GMT file containing the geometry and | ||
| data to plot. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| data | ||
| A geo-like Python object which implements ``__geo_interface__`` (e.g. a | ||
| :class:`geopandas.GeoDataFrame` or :class:`shapely.geometry`), or an OGR_GMT | ||
| file containing the geometry and data to plot. | ||
| column | ||
| The name of the data column to use for the fill. | ||
seisman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| cmap | ||
| The CPT to use for filling the polygons. If set to ``True``, the current CPT | ||
| will be used. | ||
| **kwargs | ||
| Additional keyword arguments passed to :meth:`pygmt.Figure.plot`. | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> import geopandas as gpd | ||
| >>> import pygmt | ||
| >>> world = gpd.read_file( | ||
| ... "https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip" | ||
| ... ) | ||
| >>> world["POP_EST"] *= 1e-6 # Population in millions | ||
|
|
||
| >>> fig = pygmt.Figure() | ||
| >>> fig.basemap(region=[-19.5, 53, -38, 37.5], projection="M15c", frame=True) | ||
| >>> pygmt.makecpt(cmap="bilbao", series=(0, 270, 10), reverse=True) | ||
| >>> fig.choropleth(world, column="POP_EST", pen="0.3p,gray10") | ||
| >>> fig.colorbar(frame=True) | ||
| >>> fig.show() | ||
| """ | ||
| self.plot( | ||
| data=data, close=True, fill="+z", cmap=cmap, aspatial=f"Z={column}", **kwargs | ||
| ) | ||
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,5 @@ | ||
| outs: | ||
| - md5: ed720d3682b43c4474cd21f49e788a98 | ||
| size: 155020 | ||
| hash: md5 | ||
| path: test_choropleth.png |
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,33 @@ | ||
| """ | ||
| Test Figure.choropleth. | ||
| """ | ||
|
|
||
| import pytest | ||
| from pygmt import Figure, makecpt | ||
|
|
||
| gpd = pytest.importorskip("geopandas") | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module", name="world") | ||
| def fixture_world(): | ||
| """ | ||
| Download and cache the Natural Earth countries dataset for testing. | ||
| """ | ||
| return gpd.read_file( | ||
| "https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip" | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.mpl_image_compare | ||
| def test_choropleth(world): | ||
| """ | ||
| Test Figure.choropleth method. | ||
| """ | ||
| world["POP_EST"] *= 1e-6 # Population in millions | ||
|
|
||
| fig = Figure() | ||
| fig.basemap(region=[-19.5, 53, -38, 37.5], projection="M15c", frame=True) | ||
| makecpt(cmap="bilbao", series=(0, 270, 10), reverse=True) | ||
| fig.choropleth(world, column="POP_EST", pen="0.3p,gray10") | ||
| fig.colorbar(frame=True) | ||
| return fig |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
dataparameter is annotated asGeoLike | PathLike, butGeoLikeis currently defined only asgeopandas.GeoDataFramewhile the implementation and surrounding utilities (e.g.,helpers.utils.data_kindandhelpers.tempfile.tempfile_from_geojson) support any geo-like object implementing__geo_interface__(including shapely geometries). This mismatch between the type hint and the documented behavior can confuse users and type checkers; consider broadeningGeoLiketo represent all supported geo-like inputs (e.g., via a protocol/union) or narrowing the docstring to match the actual annotated contract.