Skip to content

Commit 4c9eb2e

Browse files
TESTING: Add 'roads' and 'chorophleth_map' examples back
1 parent cdca454 commit 4c9eb2e

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

examples/gallery/lines/roads.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# ruff: noqa: RUF003
2+
"""
3+
Roads
4+
=====
5+
6+
The :meth:`pygmt.Figure.plot` method allows us to plot geographical data such
7+
as lines which are stored in a :class:`geopandas.GeoDataFrame` object. Use
8+
:func:`geopandas.read_file` to load data from any supported OGR format such as
9+
a shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. Then, pass the
10+
:class:`geopandas.GeoDataFrame` as an argument to the ``data`` parameter of
11+
:meth:`pygmt.Figure.plot`, and style the geometry using the ``pen`` parameter.
12+
"""
13+
14+
# %%
15+
import geopandas as gpd
16+
import pygmt
17+
18+
# Read shapefile data using geopandas
19+
gdf = gpd.read_file(
20+
"http://www2.census.gov/geo/tiger/TIGER2015/PRISECROADS/tl_2015_15_prisecroads.zip"
21+
)
22+
# The dataset contains different road types listed in the RTTYP column,
23+
# here we select the following ones to plot:
24+
roads_common = gdf[gdf.RTTYP == "M"] # Common name roads
25+
roads_state = gdf[gdf.RTTYP == "S"] # State recognized roads
26+
roads_interstate = gdf[gdf.RTTYP == "I"] # Interstate roads
27+
28+
fig = pygmt.Figure()
29+
30+
# Define target region around Oʻahu (Hawaiʻi)
31+
region = [-158.3, -157.6, 21.2, 21.75] # xmin, xmax, ymin, ymax
32+
33+
title = "Main roads of O`ahu (Hawai`i)" # Approximating the Okina letter ʻ with `
34+
fig.basemap(region=region, projection="M12c", frame=["af", f"WSne+t{title}"])
35+
fig.coast(land="gray", water="dodgerblue4", shorelines="1p,black")
36+
37+
# Plot the individual road types with different pen settings and assign labels
38+
# which are displayed in the legend
39+
fig.plot(data=roads_common, pen="5p,dodgerblue", label="CommonName")
40+
fig.plot(data=roads_state, pen="2p,gold", label="StateRecognized")
41+
fig.plot(data=roads_interstate, pen="2p,red", label="Interstate")
42+
43+
# Add legend
44+
fig.legend()
45+
46+
fig.show()
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
Choropleth map
3+
==============
4+
5+
The :meth:`pygmt.Figure.plot` method allows us to plot geographical data such
6+
as polygons which are stored in a :class:`geopandas.GeoDataFrame` object. Use
7+
:func:`geopandas.read_file` to load data from any supported OGR format such as
8+
a shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. You can also
9+
use a full URL pointing to your desired data source. Then, pass the
10+
:class:`geopandas.GeoDataFrame` as an argument to the ``data`` parameter of
11+
:meth:`pygmt.Figure.plot`, and style the geometry using the ``pen`` parameter.
12+
To fill the polygons based on a corresponding column you need to set
13+
``fill="+z"`` as well as select the appropriate column using the ``aspatial``
14+
parameter as shown in the example below.
15+
"""
16+
17+
# %%
18+
import geopandas as gpd
19+
import pygmt
20+
21+
# Read polygon data using geopandas
22+
gdf = gpd.read_file("https://geodacenter.github.io/data-and-lab/data/airbnb.zip")
23+
24+
fig = pygmt.Figure()
25+
26+
fig.basemap(
27+
region=gdf.total_bounds[[0, 2, 1, 3]],
28+
projection="M6c",
29+
frame="+tPopulation of Chicago",
30+
)
31+
32+
# The dataset contains different attributes, here we select
33+
# the "population" column to plot.
34+
35+
# First, we define the colormap to fill the polygons based on
36+
# the "population" column.
37+
pygmt.makecpt(
38+
cmap="acton",
39+
series=[gdf["population"].min(), gdf["population"].max(), 10],
40+
continuous=True,
41+
reverse=True,
42+
)
43+
44+
# Next, we plot the polygons and fill them using the defined colormap.
45+
# The target column is defined by the aspatial parameter.
46+
fig.plot(
47+
data=gdf,
48+
pen="0.3p,gray10",
49+
fill="+z",
50+
cmap=True,
51+
aspatial="Z=population",
52+
)
53+
54+
# Add colorbar legend
55+
fig.colorbar(frame="x+lPopulation", position="jML+o-0.5c+w3.5c/0.2c")
56+
57+
fig.show()

0 commit comments

Comments
 (0)