From 6641269c56ec25fadec0929fbfd74def2826755b Mon Sep 17 00:00:00 2001 From: core-man Date: Mon, 15 Mar 2021 19:19:02 +0800 Subject: [PATCH 01/18] Pass a numpy array to intensity --- pygmt/src/plot.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index 1d50bb4e416..3c62932fe72 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -1,6 +1,7 @@ """ plot - Plot in two dimensions. """ +import numpy as np from pygmt.clib import Session from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import ( @@ -218,6 +219,14 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs): ) extra_arrays.append(sizes) + if "I" in kwargs and isinstance(kwargs["I"], np.ndarray): + print(kwargs["I"]) + if kind != "vectors": + raise GMTInvalidInput( + "Can't use arrays for intensity if data is matrix or file." + ) + extra_arrays.append(kwargs["I"]) + if "t" in kwargs and is_nonstr_iter(kwargs["t"]): extra_arrays.append(kwargs["t"]) kwargs["t"] = "" From 3503319338cc8a64d52648434d29d6ee9b058814 Mon Sep 17 00:00:00 2001 From: core-man Date: Wed, 17 Mar 2021 21:05:14 +0800 Subject: [PATCH 02/18] Fix if --- pygmt/src/plot.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index 3c62932fe72..24e32a56a48 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -219,13 +219,14 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs): ) extra_arrays.append(sizes) - if "I" in kwargs and isinstance(kwargs["I"], np.ndarray): + if "I" in kwargs and is_nonstr_iter(kwargs["I"]): print(kwargs["I"]) if kind != "vectors": raise GMTInvalidInput( "Can't use arrays for intensity if data is matrix or file." ) extra_arrays.append(kwargs["I"]) + kwargs["I"] = "" if "t" in kwargs and is_nonstr_iter(kwargs["t"]): extra_arrays.append(kwargs["t"]) @@ -239,4 +240,6 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs): with file_context as fname: arg_str = " ".join([fname, build_arg_string(kwargs)]) + print(build_arg_string(kwargs)) + print(arg_str) lib.call_module("plot", arg_str) From 9adf27254555c04262d6408c8f83dc6629395b2f Mon Sep 17 00:00:00 2001 From: core-man Date: Wed, 17 Mar 2021 21:06:05 +0800 Subject: [PATCH 03/18] Add a test for array-like intensity --- pygmt/tests/test_plot.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/pygmt/tests/test_plot.py b/pygmt/tests/test_plot.py index 5ef8945e125..2a861edd10b 100644 --- a/pygmt/tests/test_plot.py +++ b/pygmt/tests/test_plot.py @@ -231,6 +231,42 @@ def test_plot_colors_sizes_proj(data, region): return fig +@check_figures_equal() +def test_plot_varying_intensity(): + """ + Plot the data with array-like intensity. + """ + x = np.arange(1, 10) + y = np.arange(1, 10) + intensity = np.linspace(-1, 1, len(x)) + + fig_ref, fig_test = Figure(), Figure() + # Use single-character arguments for the reference image + with GMTTempFile() as tmpfile: + np.savetxt(tmpfile.name, np.c_[x, y, intensity], fmt="%s") + fig_ref.plot( + data=tmpfile.name, + R="0/10/0/10", + J="X4i", + B="", + S="c0.2c", + G="blue", + I="", + ) + + fig_test.plot( + x=x, + y=y, + region=[0, 10, 0, 10], + projection="X4i", + frame=True, + style="c0.2c", + color="blue", + intensity=intensity, + ) + return fig_ref, fig_test + + @check_figures_equal() def test_plot_transparency(): """ From 41289ef6a445a09a975893cc34fa14a9fe630cf6 Mon Sep 17 00:00:00 2001 From: core-man Date: Wed, 17 Mar 2021 21:07:47 +0800 Subject: [PATCH 04/18] Remove some debug codes --- pygmt/src/plot.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index 24e32a56a48..dd62613f39c 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -240,6 +240,4 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs): with file_context as fname: arg_str = " ".join([fname, build_arg_string(kwargs)]) - print(build_arg_string(kwargs)) - print(arg_str) lib.call_module("plot", arg_str) From 6e7006f25186d130aaa16dd03b8822627df27756 Mon Sep 17 00:00:00 2001 From: core-man Date: Wed, 17 Mar 2021 21:13:51 +0800 Subject: [PATCH 05/18] Remove numpy --- pygmt/src/plot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index dd62613f39c..d8ccaa665eb 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -1,7 +1,6 @@ """ plot - Plot in two dimensions. """ -import numpy as np from pygmt.clib import Session from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import ( From 34ed95bf9c9cfa76d900ad9f633cf476f25904fb Mon Sep 17 00:00:00 2001 From: core-man Date: Wed, 17 Mar 2021 21:14:32 +0800 Subject: [PATCH 06/18] Remove some debug codes --- pygmt/src/plot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index d8ccaa665eb..edebf7f2c34 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -219,7 +219,6 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs): extra_arrays.append(sizes) if "I" in kwargs and is_nonstr_iter(kwargs["I"]): - print(kwargs["I"]) if kind != "vectors": raise GMTInvalidInput( "Can't use arrays for intensity if data is matrix or file." From 2d5e9adb65ecb482bc99f320e730ad6d88cea9a0 Mon Sep 17 00:00:00 2001 From: core-man Date: Wed, 17 Mar 2021 23:10:37 +0800 Subject: [PATCH 07/18] Add a test for array intensity used with matrix --- pygmt/tests/test_plot.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pygmt/tests/test_plot.py b/pygmt/tests/test_plot.py index 2a861edd10b..91d1467a59b 100644 --- a/pygmt/tests/test_plot.py +++ b/pygmt/tests/test_plot.py @@ -95,7 +95,8 @@ def test_plot_fail_no_data(data): def test_plot_fail_size_color(data): """ - Should raise an exception if array sizes and color are used with matrix. + Should raise an exception if array color, sizes and intensity are used with + matrix. """ fig = Figure() with pytest.raises(GMTInvalidInput): @@ -117,6 +118,16 @@ def test_plot_fail_size_color(data): color="red", frame="afg", ) + with pytest.raises(GMTInvalidInput): + fig.plot( + data=data, + region=region, + projection="X4i", + style="c0.2c", + color="red", + frame="afg", + intensity=data[:, 2], + ) @check_figures_equal() From 225ead66067818c3a5d3581769771e638c72c4d7 Mon Sep 17 00:00:00 2001 From: core-man Date: Fri, 19 Mar 2021 22:55:49 +0800 Subject: [PATCH 08/18] Add test_plot_varying_intensity.png into DVC --- pygmt/tests/baseline/test_plot_varying_intensity.png.dvc | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 pygmt/tests/baseline/test_plot_varying_intensity.png.dvc diff --git a/pygmt/tests/baseline/test_plot_varying_intensity.png.dvc b/pygmt/tests/baseline/test_plot_varying_intensity.png.dvc new file mode 100644 index 00000000000..a69c3023c0a --- /dev/null +++ b/pygmt/tests/baseline/test_plot_varying_intensity.png.dvc @@ -0,0 +1,4 @@ +outs: +- md5: 51382f3181c19da1eed2a0fe794aa6de + size: 18659 + path: test_plot_varying_intensity.png From 3b24385222a71f350917a93ce9ab298f323ac3b8 Mon Sep 17 00:00:00 2001 From: core-man Date: Fri, 19 Mar 2021 22:56:37 +0800 Subject: [PATCH 09/18] Use mpl_image_compare --- pygmt/tests/test_plot.py | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/pygmt/tests/test_plot.py b/pygmt/tests/test_plot.py index 91d1467a59b..7a72287c3e9 100644 --- a/pygmt/tests/test_plot.py +++ b/pygmt/tests/test_plot.py @@ -242,7 +242,7 @@ def test_plot_colors_sizes_proj(data, region): return fig -@check_figures_equal() +@pytest.mark.mpl_image_compare def test_plot_varying_intensity(): """ Plot the data with array-like intensity. @@ -251,21 +251,8 @@ def test_plot_varying_intensity(): y = np.arange(1, 10) intensity = np.linspace(-1, 1, len(x)) - fig_ref, fig_test = Figure(), Figure() - # Use single-character arguments for the reference image - with GMTTempFile() as tmpfile: - np.savetxt(tmpfile.name, np.c_[x, y, intensity], fmt="%s") - fig_ref.plot( - data=tmpfile.name, - R="0/10/0/10", - J="X4i", - B="", - S="c0.2c", - G="blue", - I="", - ) - - fig_test.plot( + fig = Figure() + fig.plot( x=x, y=y, region=[0, 10, 0, 10], @@ -275,7 +262,7 @@ def test_plot_varying_intensity(): color="blue", intensity=intensity, ) - return fig_ref, fig_test + return fig @check_figures_equal() From 92e2ac8ee4fd191104b876b109565e5b1a6a1bee Mon Sep 17 00:00:00 2001 From: core-man Date: Fri, 19 Mar 2021 23:10:13 +0800 Subject: [PATCH 10/18] Update documentation --- pygmt/src/plot.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index edebf7f2c34..a821038125f 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -143,11 +143,13 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs): the coordinates of a *refpoint* which will serve as a fixed external reference point for all groups. {G} - intensity : float or bool + intensity : float or bool or 1d array Provide an *intens* value (nominally in the -1 to +1 range) to modulate the fill color by simulating illumination [None]. If using ``intensity=True``, we will instead read *intens* from the first data column after the symbol parameters (if given). + *intensity* can also be a 1d array to set varying intensity for + symbols, but it is only valid if using ``x``/``y``. close : str [**+b**\|\ **d**\|\ **D**][**+xl**\|\ **r**\|\ *x0*]\ [**+yl**\|\ **r**\|\ *y0*][**+p**\ *pen*]. From f26b89fcad37ada5f283925029fb1f0ff10ec9d6 Mon Sep 17 00:00:00 2001 From: Yao Jiayuan Date: Sat, 20 Mar 2021 19:20:20 +0800 Subject: [PATCH 11/18] Apply suggestions from code review Co-authored-by: Dongdong Tian --- pygmt/src/plot.py | 6 +++--- pygmt/tests/test_plot.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index a821038125f..666eb4e07ae 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -144,12 +144,12 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs): reference point for all groups. {G} intensity : float or bool or 1d array - Provide an *intens* value (nominally in the -1 to +1 range) to + Provide an *intensity* value (nominally in the -1 to +1 range) to modulate the fill color by simulating illumination [None]. If - using ``intensity=True``, we will instead read *intens* from the + using ``intensity=True``, we will instead read *intensity* from the first data column after the symbol parameters (if given). *intensity* can also be a 1d array to set varying intensity for - symbols, but it is only valid if using ``x``/``y``. + symbols, but it is only valid for ``x``/``y`` pairs. close : str [**+b**\|\ **d**\|\ **D**][**+xl**\|\ **r**\|\ *x0*]\ [**+yl**\|\ **r**\|\ *y0*][**+p**\ *pen*]. diff --git a/pygmt/tests/test_plot.py b/pygmt/tests/test_plot.py index 7a72287c3e9..7a83630b7a8 100644 --- a/pygmt/tests/test_plot.py +++ b/pygmt/tests/test_plot.py @@ -256,9 +256,9 @@ def test_plot_varying_intensity(): x=x, y=y, region=[0, 10, 0, 10], - projection="X4i", + projection="X10c", frame=True, - style="c0.2c", + style="c0.5c", color="blue", intensity=intensity, ) From d6ec9906bd30cd3be20b3ceded9a397c28c96e54 Mon Sep 17 00:00:00 2001 From: core-man Date: Sat, 20 Mar 2021 19:28:24 +0800 Subject: [PATCH 12/18] Simplify and rename test_plot_fail_size_color --- pygmt/tests/test_plot.py | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/pygmt/tests/test_plot.py b/pygmt/tests/test_plot.py index 7a83630b7a8..bf6d0667a4c 100644 --- a/pygmt/tests/test_plot.py +++ b/pygmt/tests/test_plot.py @@ -93,41 +93,19 @@ def test_plot_fail_no_data(data): ) -def test_plot_fail_size_color(data): +def test_plot_fail_color_size_intensity(data): """ Should raise an exception if array color, sizes and intensity are used with matrix. """ fig = Figure() + kwargs = dict(data=data, region=region, projection="X10c", frame="afg") with pytest.raises(GMTInvalidInput): - fig.plot( - data=data, - region=region, - projection="X4i", - style="c0.2c", - color=data[:, 2], - frame="afg", - ) + fig.plot(style="c0.2c", color=data[:, 2], **kwargs) with pytest.raises(GMTInvalidInput): - fig.plot( - data=data, - region=region, - projection="X4i", - style="cc", - sizes=data[:, 2], - color="red", - frame="afg", - ) + fig.plot(style="cc", sizes=data[:, 2], color="red", **kwargs) with pytest.raises(GMTInvalidInput): - fig.plot( - data=data, - region=region, - projection="X4i", - style="c0.2c", - color="red", - frame="afg", - intensity=data[:, 2], - ) + fig.plot(style="c0.2c", color="red", intensity=data[:, 2], **kwargs) @check_figures_equal() From 41f2f2abbb24cc2941e33eb46ee3e77fd2b8ecfe Mon Sep 17 00:00:00 2001 From: core-man Date: Sat, 20 Mar 2021 19:56:37 +0800 Subject: [PATCH 13/18] Simplify GMTInvalidInput for I and t --- pygmt/src/plot.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index 666eb4e07ae..3f8aafd5cc7 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -220,17 +220,16 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs): ) extra_arrays.append(sizes) - if "I" in kwargs and is_nonstr_iter(kwargs["I"]): - if kind != "vectors": - raise GMTInvalidInput( - "Can't use arrays for intensity if data is matrix or file." - ) - extra_arrays.append(kwargs["I"]) - kwargs["I"] = "" - - if "t" in kwargs and is_nonstr_iter(kwargs["t"]): - extra_arrays.append(kwargs["t"]) - kwargs["t"] = "" + for flag in ["I", "t"]: + if flag in kwargs and is_nonstr_iter(kwargs[flag]): + if kind != "vectors": + raise GMTInvalidInput( + "Can't use arrays for {} if data is matrix or file.".format( + plot.aliases[flag] + ) + ) + extra_arrays.append(kwargs[flag]) + kwargs[flag] = "" with Session() as lib: # Choose how data will be passed in to the module From 66bd7b1305cfc2e008d2b277eacc10c05e9b071e Mon Sep 17 00:00:00 2001 From: core-man Date: Sat, 20 Mar 2021 20:19:20 +0800 Subject: [PATCH 14/18] Update basemap for varing intensity using plot --- pygmt/tests/baseline/test_plot_varying_intensity.png.dvc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/tests/baseline/test_plot_varying_intensity.png.dvc b/pygmt/tests/baseline/test_plot_varying_intensity.png.dvc index a69c3023c0a..534e2715b35 100644 --- a/pygmt/tests/baseline/test_plot_varying_intensity.png.dvc +++ b/pygmt/tests/baseline/test_plot_varying_intensity.png.dvc @@ -1,4 +1,4 @@ outs: -- md5: 51382f3181c19da1eed2a0fe794aa6de - size: 18659 +- md5: 31830242b87fd43409f4fee86ad78c16 + size: 20919 path: test_plot_varying_intensity.png From d7754f32010d9764a99ce72e965b74aa37ab1e23 Mon Sep 17 00:00:00 2001 From: Yao Jiayuan Date: Mon, 22 Mar 2021 18:54:05 +0800 Subject: [PATCH 15/18] Apply suggestions from code review Co-authored-by: Dongdong Tian --- pygmt/src/plot.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index 3f8aafd5cc7..57c1846bbb1 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -145,7 +145,7 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs): {G} intensity : float or bool or 1d array Provide an *intensity* value (nominally in the -1 to +1 range) to - modulate the fill color by simulating illumination [None]. If + modulate the fill color by simulating illumination. If using ``intensity=True``, we will instead read *intensity* from the first data column after the symbol parameters (if given). *intensity* can also be a 1d array to set varying intensity for @@ -224,9 +224,7 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs): if flag in kwargs and is_nonstr_iter(kwargs[flag]): if kind != "vectors": raise GMTInvalidInput( - "Can't use arrays for {} if data is matrix or file.".format( - plot.aliases[flag] - ) + f"Can't use arrays for {plot.aliases[flag]} if data is matrix or file." ) extra_arrays.append(kwargs[flag]) kwargs[flag] = "" From f8a6f5ab223f94eeea3425f6252816b74f17300e Mon Sep 17 00:00:00 2001 From: core-man Date: Mon, 22 Mar 2021 19:40:01 +0800 Subject: [PATCH 16/18] Reformat intensity docstring --- pygmt/src/plot.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index 57c1846bbb1..a90916f980a 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -145,11 +145,11 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs): {G} intensity : float or bool or 1d array Provide an *intensity* value (nominally in the -1 to +1 range) to - modulate the fill color by simulating illumination. If - using ``intensity=True``, we will instead read *intensity* from the - first data column after the symbol parameters (if given). - *intensity* can also be a 1d array to set varying intensity for - symbols, but it is only valid for ``x``/``y`` pairs. + modulate the fill color by simulating illumination. If using + ``intensity=True``, we will instead read *intensity* from the first + data column after the symbol parameters (if given). *intensity* can + also be a 1d array to set varying intensity for symbols, but it is only + valid for ``x``/``y`` pairs. close : str [**+b**\|\ **d**\|\ **D**][**+xl**\|\ **r**\|\ *x0*]\ [**+yl**\|\ **r**\|\ *y0*][**+p**\ *pen*]. From b3b77342cd36b0d288a5460a1bf52af1de2ea8b1 Mon Sep 17 00:00:00 2001 From: core-man Date: Mon, 22 Mar 2021 19:46:22 +0800 Subject: [PATCH 17/18] Update plot_varying_intensity.png --- pygmt/tests/baseline/test_plot_varying_intensity.png.dvc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/tests/baseline/test_plot_varying_intensity.png.dvc b/pygmt/tests/baseline/test_plot_varying_intensity.png.dvc index 534e2715b35..5d305846046 100644 --- a/pygmt/tests/baseline/test_plot_varying_intensity.png.dvc +++ b/pygmt/tests/baseline/test_plot_varying_intensity.png.dvc @@ -1,4 +1,4 @@ outs: -- md5: 31830242b87fd43409f4fee86ad78c16 - size: 20919 +- md5: f4b44bcae2670fac23c6e43324bda8fe + size: 16182 path: test_plot_varying_intensity.png From b2c45a81bca13d8a0ab56ddf1b97a1d28520bf60 Mon Sep 17 00:00:00 2001 From: core-man Date: Mon, 22 Mar 2021 19:49:19 +0800 Subject: [PATCH 18/18] Update test_plot_varying_intensity --- pygmt/tests/test_plot.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pygmt/tests/test_plot.py b/pygmt/tests/test_plot.py index bf6d0667a4c..a57692b76c0 100644 --- a/pygmt/tests/test_plot.py +++ b/pygmt/tests/test_plot.py @@ -225,17 +225,17 @@ def test_plot_varying_intensity(): """ Plot the data with array-like intensity. """ - x = np.arange(1, 10) - y = np.arange(1, 10) - intensity = np.linspace(-1, 1, len(x)) + x = np.arange(-1, 1.1, 0.1) + y = np.zeros(x.size) + intensity = x fig = Figure() fig.plot( x=x, y=y, - region=[0, 10, 0, 10], - projection="X10c", - frame=True, + region=[-1.1, 1.1, -0.5, 0.5], + projection="X15c/2c", + frame=["S", "xaf+lIntensity"], style="c0.5c", color="blue", intensity=intensity,