From 0d07cccef60da5d4874bb2c55c9824ffd7faf646 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 8 Oct 2021 07:28:04 +0800 Subject: [PATCH] Allow non-string color when input data is a matrix or a file for plot and plot3d (#1526) * Allow non-string color when input data is a matrix or a file for plot and plot3d * Expand existing tests to test int type colors --- pygmt/src/plot.py | 2 +- pygmt/src/plot3d.py | 2 +- pygmt/tests/test_plot.py | 7 ++++--- pygmt/tests/test_plot3d.py | 6 ++++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index d684e1ceb94..5f627383517 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -241,7 +241,7 @@ def plot(self, data=None, x=None, y=None, size=None, direction=None, **kwargs): kwargs["S"] = "s0.2c" except FileNotFoundError: pass - if "G" in kwargs and not isinstance(kwargs["G"], str): + if "G" in kwargs and is_nonstr_iter(kwargs["G"]): if kind != "vectors": raise GMTInvalidInput( "Can't use arrays for color if data is matrix or file." diff --git a/pygmt/src/plot3d.py b/pygmt/src/plot3d.py index 6978db5370a..27164eda470 100644 --- a/pygmt/src/plot3d.py +++ b/pygmt/src/plot3d.py @@ -211,7 +211,7 @@ def plot3d( kwargs["S"] = "u0.2c" except FileNotFoundError: pass - if "G" in kwargs and not isinstance(kwargs["G"], str): + if "G" in kwargs and is_nonstr_iter(kwargs["G"]): if kind != "vectors": raise GMTInvalidInput( "Can't use arrays for color if data is matrix or file." diff --git a/pygmt/tests/test_plot.py b/pygmt/tests/test_plot.py index 46d7ae5bc33..a29e5194b0a 100644 --- a/pygmt/tests/test_plot.py +++ b/pygmt/tests/test_plot.py @@ -302,12 +302,13 @@ def test_plot_sizes_colors_transparencies(): return fig -@pytest.mark.mpl_image_compare +@pytest.mark.mpl_image_compare(filename="test_plot_matrix.png") +@pytest.mark.parametrize("color", ["#aaaaaa", 170]) @pytest.mark.xfail( condition=gmt_version <= Version("6.2.0"), reason="Upstream bug fixed in https://github.com/GenericMappingTools/gmt/pull/5799.", ) -def test_plot_matrix(data): +def test_plot_matrix(data, color): """ Plot the data passing in a matrix and specifying columns. """ @@ -317,7 +318,7 @@ def test_plot_matrix(data): region=[10, 70, -5, 10], projection="M15c", style="cc", - color="#aaaaaa", + color=color, frame="a", incols="0,1,2+s0.5", ) diff --git a/pygmt/tests/test_plot3d.py b/pygmt/tests/test_plot3d.py index 355d77bce71..2b91e608eb4 100644 --- a/pygmt/tests/test_plot3d.py +++ b/pygmt/tests/test_plot3d.py @@ -313,7 +313,9 @@ def test_plot3d_sizes_colors_transparencies(): @pytest.mark.mpl_image_compare -def test_plot3d_matrix(data, region): +@pytest.mark.mpl_image_compare(filename="test_plot3d_matrix.png") +@pytest.mark.parametrize("color", ["#aaaaaa", 170]) +def test_plot3d_matrix(data, region, color): """ Plot the data passing in a matrix and specifying incols. """ @@ -325,7 +327,7 @@ def test_plot3d_matrix(data, region): region=region, projection="M20c", style="c1c", - color="#aaaaaa", + color=color, frame=["a", "za"], incols="0,1,2", )