Skip to content

Commit 02a918f

Browse files
Add gallery example for geopandas point geometry (cities in Europe from Natural Earth) (#4231)
1 parent 73e895f commit 02a918f

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
GeoPandas: Plotting points with Point or MultiPoint geometry
3+
============================================================
4+
5+
The :meth:`pygmt.Figure.plot` method allows us to plot geographical data such as points
6+
with Point or MultiPoint geometry types stored in a :class:`geopandas.GeoDataFrame`
7+
object. Use :func:`geopandas.read_file` to load data from any supported OGR format such
8+
as a shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. Then, pass the
9+
:class:`geopandas.GeoDataFrame` object as an argument to the ``data`` parameter of
10+
:meth:`pygmt.Figure.plot`, and style the points using the ``fill`` and ``pen``
11+
parameters. Additionally, pass suitable columns of the :class:`geopandas.GeoDataFrame`
12+
to the ``x``, ``y``, and ``text`` parameters of the :meth:`pygmt.Figure.text` method
13+
to label specific features.
14+
"""
15+
16+
# %%
17+
import geopandas as gpd
18+
import pygmt
19+
20+
# Read a sample dataset provided by Natural Earth. The dataset contains cities stored
21+
# as Point geometry type. In this example we focus on Europe.
22+
provider = "https://naciscdn.org/naturalearth"
23+
cities = gpd.read_file(f"{provider}/50m/cultural/ne_50m_populated_places_simple.zip")
24+
cities = cities[cities["name"] != "Vatican City"].copy() # No overlap with label Rome
25+
# Create two subsets for small and large cities
26+
cities_small = cities[cities["worldcity"] != 1].copy()
27+
cities_large = cities[cities["worldcity"] == 1].copy()
28+
29+
fig = pygmt.Figure()
30+
fig.basemap(region=[-10, 32.7, 37, 57], projection="M12c", frame=True)
31+
fig.coast(land="gray95", shorelines="1/0.3p,gray50")
32+
33+
# Plot the two subsets using squares with different sizes and fills.
34+
fig.plot(data=cities_small, style="s0.1c", fill="lightgray", pen="0.5p")
35+
fig.plot(data=cities_large, style="s0.15c", fill="darkorange", pen="0.5p")
36+
37+
# Label the larger cities with their names.
38+
fig.text(
39+
x=cities_large.geometry.x,
40+
y=cities_large.geometry.y,
41+
text=cities_large["name"],
42+
offset="0.12c",
43+
justify="BL",
44+
font="6p,Helvetica-Bold",
45+
fill="white@30",
46+
pen="0.5p,darkorange",
47+
clearance="0.05c+tO",
48+
)
49+
50+
fig.show()

0 commit comments

Comments
 (0)