-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TESTING: Add 'roads' and 'chorophleth_map' examples back
- Loading branch information
1 parent
cdca454
commit 4c9eb2e
Showing
2 changed files
with
103 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# ruff: noqa: RUF003 | ||
""" | ||
Roads | ||
===== | ||
The :meth:`pygmt.Figure.plot` method allows us to plot geographical data such | ||
as lines 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. 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. | ||
""" | ||
|
||
# %% | ||
import geopandas as gpd | ||
import pygmt | ||
|
||
# Read shapefile data using geopandas | ||
gdf = gpd.read_file( | ||
"http://www2.census.gov/geo/tiger/TIGER2015/PRISECROADS/tl_2015_15_prisecroads.zip" | ||
) | ||
# The dataset contains different road types listed in the RTTYP column, | ||
# here we select the following ones to plot: | ||
roads_common = gdf[gdf.RTTYP == "M"] # Common name roads | ||
roads_state = gdf[gdf.RTTYP == "S"] # State recognized roads | ||
roads_interstate = gdf[gdf.RTTYP == "I"] # Interstate roads | ||
|
||
fig = pygmt.Figure() | ||
|
||
# Define target region around Oʻahu (Hawaiʻi) | ||
region = [-158.3, -157.6, 21.2, 21.75] # xmin, xmax, ymin, ymax | ||
|
||
title = "Main roads of O`ahu (Hawai`i)" # Approximating the Okina letter ʻ with ` | ||
fig.basemap(region=region, projection="M12c", frame=["af", f"WSne+t{title}"]) | ||
fig.coast(land="gray", water="dodgerblue4", shorelines="1p,black") | ||
|
||
# Plot the individual road types with different pen settings and assign labels | ||
# which are displayed in the legend | ||
fig.plot(data=roads_common, pen="5p,dodgerblue", label="CommonName") | ||
fig.plot(data=roads_state, pen="2p,gold", label="StateRecognized") | ||
fig.plot(data=roads_interstate, pen="2p,red", label="Interstate") | ||
|
||
# Add legend | ||
fig.legend() | ||
|
||
fig.show() |
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,57 @@ | ||
""" | ||
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 | ||
fig.colorbar(frame="x+lPopulation", position="jML+o-0.5c+w3.5c/0.2c") | ||
|
||
fig.show() |