From 380753f51a0cad114ad6c68ac4802454abd6a8d2 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 8 May 2026 14:18:29 +0800 Subject: [PATCH] Figure.set_panel: Fix the bug when panel is not set --- pygmt/src/subplot.py | 14 ++++++-------- pygmt/tests/test_subplot.py | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/pygmt/src/subplot.py b/pygmt/src/subplot.py index c5a618b0e07..539c39727d3 100644 --- a/pygmt/src/subplot.py +++ b/pygmt/src/subplot.py @@ -419,13 +419,11 @@ def set_panel( aliasdict = AliasSystem(A=Alias(tag, name="tag")).add_common(V=verbose) aliasdict.merge(kwargs) + args = ["set"] + if panel is not None: + args.append(Alias(panel, name="panel", sep=",", size=2)._value) # type: ignore[arg-type] + args.extend(build_arg_list(aliasdict)) + with Session() as lib: - lib.call_module( - module="subplot", - args=[ - "set", - Alias(panel, name="panel", sep=",", size=2)._value, - *build_arg_list(aliasdict), - ], - ) + lib.call_module(module="subplot", args=args) yield diff --git a/pygmt/tests/test_subplot.py b/pygmt/tests/test_subplot.py index 513f006ffa2..5fc3013eadd 100644 --- a/pygmt/tests/test_subplot.py +++ b/pygmt/tests/test_subplot.py @@ -132,6 +132,25 @@ def test_subplot_outside_plotting_positioning(): return fig +@pytest.mark.mpl_image_compare(filename="test_subplot_basic_frame.png") +def test_subplot_set_panel_without_panel(): + """ + Ensure Figure.set_panel works when panel is omitted. + + Omitting ``panel`` should let GMT advance to the next panel according to the subplot + order instead of passing an invalid ``None`` argument to the C API. + + Use the baseline image from test_subplot_basic_frame. + """ + fig = Figure() + with fig.subplot(nrows=1, ncols=2, figsize=("6c", "3c"), frame=Frame(axes="WSne")): + with fig.set_panel(): + fig.basemap(region=[0, 3, 0, 3], frame=Frame(title="plot0")) + with fig.set_panel(): + fig.basemap(region=[0, 3, 0, 3], frame=Frame(title="plot1")) + return fig + + def test_subplot_deprecated_autolabel(): """ Test that using the deprecated autolabel parameter raises a warning when conflicted