Skip to content

Commit 1b65324

Browse files
committed
Update
1 parent c6a927b commit 1b65324

1 file changed

Lines changed: 67 additions & 11 deletions

File tree

benchmarks/bench_cartopy_coastlines.py

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
OUTPUT_DIR = Path("plots/coastlines")
1717
REPEATS = 10
1818
CARTOPY_RESOLUTIONS = ("110m", "50m", "10m")
19-
PYGMT_RESOLUTIONS = ("crude", "low", "intermediate")
19+
PYGMT_RESOLUTIONS = ("crude", "low", "intermediate", "high", "full")
2020
LAND_COLOR = "#cccccc"
2121
WATER_COLOR = "#b9d9ea"
2222

2323

24-
def plot_cartopy(resolution: str):
24+
def plot_cartopy_global(resolution: str):
2525
"""Create a simple global coastline plot with cartopy."""
2626
fig = plt.figure(figsize=(6, 4), dpi=300)
2727
ax = fig.add_subplot(1, 1, 1, projection=ccrs.Robinson())
@@ -33,17 +33,29 @@ def plot_cartopy(resolution: str):
3333
return fig
3434

3535

36+
def plot_cartopy_regional(resolution: str):
37+
"""Create a regional coastline plot with cartopy."""
38+
fig = plt.figure(figsize=(6, 4), dpi=300)
39+
ax = fig.add_subplot(1, 1, 1, projection=ccrs.Mercator())
40+
ax.set_extent([110, 160, -45, -10], crs=ccrs.PlateCarree())
41+
ax.add_feature(cfeature.LAND, facecolor=LAND_COLOR)
42+
ax.add_feature(cfeature.OCEAN, facecolor=WATER_COLOR)
43+
ax.coastlines(resolution=resolution, linewidth=0.5)
44+
ax.gridlines(color="0.8", linewidth=0.4)
45+
return fig
46+
47+
3648
def save_cartopy(fig, output: Path) -> None:
3749
"""Save a cartopy/matplotlib figure and release it."""
3850
fig.savefig(output)
3951
plt.close(fig)
4052

4153

42-
def plot_pygmt(resolution: str) -> pygmt.Figure:
54+
def plot_pygmt_global(resolution: str) -> pygmt.Figure:
4355
"""Create a simple global coastline plot with PyGMT."""
4456
fig = pygmt.Figure()
4557
fig.coast(
46-
region="g",
58+
region="d",
4759
projection="R6i",
4860
resolution=resolution,
4961
land=LAND_COLOR,
@@ -53,6 +65,20 @@ def plot_pygmt(resolution: str) -> pygmt.Figure:
5365
)
5466
return fig
5567

68+
def plot_pygmt_regional(resolution: str) -> pygmt.Figure:
69+
"""Create a regional coastline plot with PyGMT."""
70+
fig = pygmt.Figure()
71+
fig.coast(
72+
region=[110, 160, -45, -10],
73+
projection="M6i",
74+
resolution=resolution,
75+
land=LAND_COLOR,
76+
water=WATER_COLOR,
77+
shorelines="1/0.5p,black",
78+
frame="afg",
79+
)
80+
return fig
81+
5682

5783
def save_pygmt(fig: pygmt.Figure, output: Path) -> None:
5884
"""Save a PyGMT figure."""
@@ -109,12 +135,26 @@ def main() -> None:
109135
print(f"Running {REPEATS} timed run(s) per backend")
110136
print(f"Writing PNG files to {OUTPUT_DIR}")
111137

112-
print("Benchmarking cartopy...", flush=True)
138+
print("Benchmarking cartopy global...", flush=True)
113139
for resolution in CARTOPY_RESOLUTIONS:
114140
print(f"Resolution: {resolution}")
115141
plot_timings, save_timings = benchmark(
116-
name=f"cartopy_{resolution}",
117-
plot_func=plot_cartopy,
142+
name=f"cartopy_global_{resolution}",
143+
plot_func=plot_cartopy_global,
144+
save_func=save_cartopy,
145+
output_dir=OUTPUT_DIR,
146+
repeats=REPEATS,
147+
resolution=resolution,
148+
)
149+
print(format_summary("cartopy plot", plot_timings))
150+
print(format_summary("cartopy savefig", save_timings))
151+
152+
print("Benchmarking cartopy regional...", flush=True)
153+
for resolution in CARTOPY_RESOLUTIONS[2:]:
154+
print(f"Resolution: {resolution}")
155+
plot_timings, save_timings = benchmark(
156+
name=f"cartopy_regional_{resolution}",
157+
plot_func=plot_cartopy_regional,
118158
save_func=save_cartopy,
119159
output_dir=OUTPUT_DIR,
120160
repeats=REPEATS,
@@ -123,12 +163,27 @@ def main() -> None:
123163
print(format_summary("cartopy plot", plot_timings))
124164
print(format_summary("cartopy savefig", save_timings))
125165

126-
print("Benchmarking pygmt...", flush=True)
127-
for resolution in PYGMT_RESOLUTIONS:
166+
167+
print("Benchmarking pygmt global...", flush=True)
168+
for resolution in PYGMT_RESOLUTIONS[0:3]:
169+
print(f"Resolution: {resolution}")
170+
plot_timings, save_timings = benchmark(
171+
name=f"pygmt_global_{resolution}",
172+
plot_func=plot_pygmt_global,
173+
save_func=save_pygmt,
174+
output_dir=OUTPUT_DIR,
175+
repeats=REPEATS,
176+
resolution=resolution,
177+
)
178+
print(format_summary("pygmt plot", plot_timings))
179+
print(format_summary("pygmt savefig", save_timings))
180+
181+
print("Benchmarking pygmt regional...", flush=True)
182+
for resolution in PYGMT_RESOLUTIONS[2:]:
128183
print(f"Resolution: {resolution}")
129184
plot_timings, save_timings = benchmark(
130-
name=f"pygmt_{resolution}",
131-
plot_func=plot_pygmt,
185+
name=f"pygmt_regional_{resolution}",
186+
plot_func=plot_pygmt_regional,
132187
save_func=save_pygmt,
133188
output_dir=OUTPUT_DIR,
134189
repeats=REPEATS,
@@ -138,5 +193,6 @@ def main() -> None:
138193
print(format_summary("pygmt savefig", save_timings))
139194

140195

196+
141197
if __name__ == "__main__":
142198
main()

0 commit comments

Comments
 (0)