From d67810d0bc780cae8ec7a9ca6861c975c2e596dc Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 15 Mar 2023 21:55:44 +0800 Subject: [PATCH 1/7] Use separate sessions for 'subplot begin' and 'subplot end' --- pygmt/src/subplot.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pygmt/src/subplot.py b/pygmt/src/subplot.py index 9ef8f7ff9a4..e409d2b3670 100644 --- a/pygmt/src/subplot.py +++ b/pygmt/src/subplot.py @@ -154,12 +154,16 @@ def subplot(self, nrows=1, ncols=1, **kwargs): "Please provide either one of 'figsize' or 'subsize' only." ) - with Session() as lib: - try: + # Need to use separate sessions for "subplot begin" and "subplot end". + # Otherwise, "subplot end" will use the last session, which may causes + # strange behavior like https://github.com/GenericMappingTools/pygmt/issues/2426 + try: + with Session() as lib: arg_str = " ".join(["begin", f"{nrows}x{ncols}", build_arg_string(kwargs)]) lib.call_module(module="subplot", args=arg_str) yield - finally: + finally: + with Session() as lib: v_arg = build_arg_string({"V": kwargs.get("V")}) lib.call_module(module="subplot", args=f"end {v_arg}") From f262b0cf09a28ef31666c3590eca2bd7d5870c7f Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 15 Mar 2023 22:09:56 +0800 Subject: [PATCH 2/7] Improve the comments --- pygmt/src/subplot.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pygmt/src/subplot.py b/pygmt/src/subplot.py index e409d2b3670..55f5ec6fb34 100644 --- a/pygmt/src/subplot.py +++ b/pygmt/src/subplot.py @@ -155,8 +155,9 @@ def subplot(self, nrows=1, ncols=1, **kwargs): ) # Need to use separate sessions for "subplot begin" and "subplot end". - # Otherwise, "subplot end" will use the last session, which may causes - # strange behavior like https://github.com/GenericMappingTools/pygmt/issues/2426 + # Otherwise, "subplot end" will use the last session, which may cause + # strange positioning issues for later plotting calls. + # See https://github.com/GenericMappingTools/pygmt/issues/2426. try: with Session() as lib: arg_str = " ".join(["begin", f"{nrows}x{ncols}", build_arg_string(kwargs)]) From a9eb8fb975c0fdfa9333930cde2ee2e68502f22f Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 15 Mar 2023 22:12:13 +0800 Subject: [PATCH 3/7] Update the grdgradient_shading example to use 'JBC' to position colorbars --- examples/gallery/images/grdgradient_shading.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gallery/images/grdgradient_shading.py b/examples/gallery/images/grdgradient_shading.py index 9fff89d194c..909a4fa076f 100644 --- a/examples/gallery/images/grdgradient_shading.py +++ b/examples/gallery/images/grdgradient_shading.py @@ -57,6 +57,6 @@ panel=True, ) -fig.colorbar(position="x9.0c/-1.1c+w10c/0.25c+h", frame="a2000f500+lElevation (m)") +fig.colorbar(position="JBC+w10c/0.25c+h", frame="a2000f500+lElevation (m)") fig.show() From 7e7bf6b26a9830a8a0acaf135145723c4a53f142 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 15 Mar 2023 22:50:48 +0800 Subject: [PATCH 4/7] Add a unit test --- ...t_subplot_outside_plotting_positioning.png.dvc | 4 ++++ pygmt/tests/test_subplot.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 pygmt/tests/baseline/test_subplot_outside_plotting_positioning.png.dvc diff --git a/pygmt/tests/baseline/test_subplot_outside_plotting_positioning.png.dvc b/pygmt/tests/baseline/test_subplot_outside_plotting_positioning.png.dvc new file mode 100644 index 00000000000..ff82ff19eae --- /dev/null +++ b/pygmt/tests/baseline/test_subplot_outside_plotting_positioning.png.dvc @@ -0,0 +1,4 @@ +outs: +- md5: 14da5db0ab63addd8a6d52d6746d9da2 + size: 12124 + path: test_subplot_outside_plotting_positioning.png diff --git a/pygmt/tests/test_subplot.py b/pygmt/tests/test_subplot.py index 2afd071391d..8ce47dab148 100644 --- a/pygmt/tests/test_subplot.py +++ b/pygmt/tests/test_subplot.py @@ -100,3 +100,18 @@ def test_subplot_nrows_ncols_less_than_one_error(): with pytest.raises(GMTInvalidInput): with fig.subplot(nrows=0, ncols=-1, figsize=("2c", "1c")): pass + + +@pytest.mark.mpl_image_compare +def test_subplot_outside_plotting_positioning(): + """ + Plotting calls are correctly positioned after exiting subplot. + + See https://github.com/GenericMappingTools/pygmt/issues/2426. + """ + fig = Figure() + with fig.subplot(nrows=1, ncols=2, figsize=(10, 5)): + fig.basemap(region=[0, 10, 0, 10], projection="X?", frame=True, panel=True) + fig.basemap(region=[0, 10, 0, 10], projection="X?", frame=True, panel=True) + fig.colorbar(position="JBC+w5c+h", cmap="turbo", frame=True) + return fig From 0a3936983760d1d828d8fe8a29328994fb46c2a2 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 15 Mar 2023 23:15:42 +0800 Subject: [PATCH 5/7] Minor changes due to different gs versions --- .../test_subplot_outside_plotting_positioning.png.dvc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/tests/baseline/test_subplot_outside_plotting_positioning.png.dvc b/pygmt/tests/baseline/test_subplot_outside_plotting_positioning.png.dvc index ff82ff19eae..6ec14d5770a 100644 --- a/pygmt/tests/baseline/test_subplot_outside_plotting_positioning.png.dvc +++ b/pygmt/tests/baseline/test_subplot_outside_plotting_positioning.png.dvc @@ -1,4 +1,4 @@ outs: -- md5: 14da5db0ab63addd8a6d52d6746d9da2 - size: 12124 +- md5: 60b51be2c54ccd6edf627a6fdae1d6ee + size: 12117 path: test_subplot_outside_plotting_positioning.png From aa40f9c7eea664f937dc460cd8f60b06629451a1 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 16 Mar 2023 10:59:34 +0800 Subject: [PATCH 6/7] Update pygmt/tests/test_subplot.py Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/tests/test_subplot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pygmt/tests/test_subplot.py b/pygmt/tests/test_subplot.py index 8ce47dab148..b7d5aa082f0 100644 --- a/pygmt/tests/test_subplot.py +++ b/pygmt/tests/test_subplot.py @@ -107,7 +107,8 @@ def test_subplot_outside_plotting_positioning(): """ Plotting calls are correctly positioned after exiting subplot. - See https://github.com/GenericMappingTools/pygmt/issues/2426. + This is a regression test for + https://github.com/GenericMappingTools/pygmt/issues/2426. """ fig = Figure() with fig.subplot(nrows=1, ncols=2, figsize=(10, 5)): From dc0fec474dc60ab945841610f77c815fc49552d9 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 16 Mar 2023 11:17:47 +0800 Subject: [PATCH 7/7] Update baseline image --- .../test_subplot_outside_plotting_positioning.png.dvc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/tests/baseline/test_subplot_outside_plotting_positioning.png.dvc b/pygmt/tests/baseline/test_subplot_outside_plotting_positioning.png.dvc index 6ec14d5770a..93b0a774f3b 100644 --- a/pygmt/tests/baseline/test_subplot_outside_plotting_positioning.png.dvc +++ b/pygmt/tests/baseline/test_subplot_outside_plotting_positioning.png.dvc @@ -1,4 +1,4 @@ outs: -- md5: 60b51be2c54ccd6edf627a6fdae1d6ee - size: 12117 +- md5: 976a01a8c9f583918c00b5a81fddaf84 + size: 12112 path: test_subplot_outside_plotting_positioning.png