Skip to content

Commit 59e7c38

Browse files
yvonnefroehlichseismanmichaelgrund
authored
Figure 6: GeoPandas / spatial data - choropleth map - area with rivers and cities of the states of the USA (#6)
Co-authored-by: Dongdong Tian <seisman.info@gmail.com> Co-authored-by: Michael Grund <23025878+michaelgrund@users.noreply.github.com>
1 parent 2da0ba0 commit 59e7c38

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

Fig6_PyGMT_geopandas.ipynb

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "760146f6-d4c9-4e69-b539-f42e057c7945",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import geopandas as gpd\n",
11+
"import pygmt\n",
12+
"\n",
13+
"provider = \"https://naciscdn.org/naturalearth\"\n",
14+
"states = gpd.read_file(f\"{provider}/50m/cultural/ne_50m_admin_1_states_provinces.zip\")\n",
15+
"rivers = gpd.read_file(f\"{provider}/50m/physical/ne_50m_rivers_lake_centerlines.zip\")\n",
16+
"cities = gpd.read_file(f\"{provider}/110m/cultural/ne_110m_populated_places_simple.zip\")\n",
17+
"\n",
18+
"states = states[states[\"admin\"] == \"United States of America\"]\n",
19+
"rivers = rivers[rivers.intersects(states.union_all())]\n",
20+
"cities = cities[cities[\"adm0name\"] == \"United States of America\"]\n",
21+
"\n",
22+
"states[\"area\"] = states.to_crs(epsg=6933).area / 10 ** 9\n",
23+
"\n",
24+
"fig = pygmt.Figure()\n",
25+
"fig.basemap(projection=\"L-96/35/33/41/12c\", region=[-126, -66, 25, 49], frame=\"+n\")\n",
26+
"\n",
27+
"pygmt.makecpt(cmap=\"hawaii\", series=[0, states[\"area\"].max()], reverse=True)\n",
28+
"fig.plot(data=states, cmap=True, pen=\"0.2p,gray50\", fill=\"+z\", aspatial=\"Z=area\")\n",
29+
"fig.colorbar(frame=\"xaf+lArea (1000 km@+2@+)\", position=\"jRB+o1.9c/0.2c+w3c/0.15c+ml\")\n",
30+
"\n",
31+
"fig.plot(data=rivers, pen=\"0.5p,dodgerblue4\")\n",
32+
"\n",
33+
"fig.plot(data=cities, style=\"s0.17c\", fill=\"darkorange\", pen=\"0.5p\")\n",
34+
"fig.text(\n",
35+
" x=cities.geometry.x,\n",
36+
" y=cities.geometry.y,\n",
37+
" text=cities[\"name\"],\n",
38+
" offset=\"0.35c/0.2c\",\n",
39+
" justify=\"BC\",\n",
40+
" font=\"4.5p,Helvetica-Bold\",\n",
41+
" fill=\"white@30\",\n",
42+
" pen=\"0.2p,darkorange\",\n",
43+
" clearance=\"0.05c+tO\",\n",
44+
")\n",
45+
"\n",
46+
"# Add the states Alaska and Hawaii separately in the lower left corner\n",
47+
"for name, xshift in zip([\"Alaska\", \"Hawaii\"], [\"1.2c\", \"2.8c\"]):\n",
48+
" substate = states[(states[\"name\"] == name)]\n",
49+
" substate = substate.explode()\n",
50+
" substate = substate[substate.to_crs(epsg=6933).area > 1.0e8].dissolve()\n",
51+
" region = pygmt.info(substate, spacing=1)\n",
52+
" with fig.shift_origin(xshift=xshift):\n",
53+
" fig.plot(\n",
54+
" data=substate,\n",
55+
" region=region,\n",
56+
" projection=\"M2c\",\n",
57+
" cmap=True,\n",
58+
" pen=\"0.2p,gray50\",\n",
59+
" fill=\"+z\",\n",
60+
" aspatial=\"Z=area\",\n",
61+
" )\n",
62+
"\n",
63+
"fig.show()\n",
64+
"# fig.savefig(fname=\"Fig6_PyGMT_geopandas.png\")"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": null,
70+
"id": "456f81ee-f666-4d2d-9147-290040583447",
71+
"metadata": {},
72+
"outputs": [],
73+
"source": []
74+
}
75+
],
76+
"metadata": {
77+
"kernelspec": {
78+
"display_name": "Python 3 (ipykernel)",
79+
"language": "python",
80+
"name": "python3"
81+
},
82+
"language_info": {
83+
"codemirror_mode": {
84+
"name": "ipython",
85+
"version": 3
86+
},
87+
"file_extension": ".py",
88+
"mimetype": "text/x-python",
89+
"name": "python",
90+
"nbconvert_exporter": "python",
91+
"pygments_lexer": "ipython3",
92+
"version": "3.12.3"
93+
}
94+
},
95+
"nbformat": 4,
96+
"nbformat_minor": 5
97+
}

0 commit comments

Comments
 (0)