From b1b0006253f59bb42df36d28cd3e73ea3343a507 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 7 Sep 2020 00:29:11 +0200 Subject: [PATCH 1/4] 2-D array RGB and RGBA values not understood in plt.plot() Issue #18411 https://github.com/matplotlib/matplotlib/issues/18411 --- lib/matplotlib/colors.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 0afa6a480e54..387f55b3b09c 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -265,6 +265,10 @@ def _to_rgba_no_colorcycle(c, alpha=None): f"Value must be within 0-1 range") return c, c, c, alpha if alpha is not None else 1. raise ValueError(f"Invalid RGBA argument: {orig_c!r}") + # turn 2-D array into 1-D array + if isinstance(c, np.ndarray): + if c.ndim == 2 and c.shape[0] == 1: + c = c.reshape((-1)) # tuple color. if not np.iterable(c): raise ValueError(f"Invalid RGBA argument: {orig_c!r}") From a6f13e9f2192e4768fe05fc43e7eb1f1b12d3ea9 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 10 Sep 2020 01:38:05 +0200 Subject: [PATCH 2/4] Tests have been added that check a figure created with 1d color arrays against one with 2d color arrays. They should be identical if 2d arrays are read correctly. + Rebase with master + Change .reshape((-1)) to .reshape(-1) --- lib/matplotlib/tests/test_axes.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 5075babd9452..9607e8937332 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -6590,3 +6590,22 @@ def test_sharing_does_not_link_positions(): init_pos = ax1.get_position() fig.subplots_adjust(left=0) assert (ax1.get_position().get_points() == init_pos.get_points()).all() + + +@check_figures_equal(extensions=["pdf"]) +def test_2dcolor_plot(fig_test, fig_ref): + color = np.array([0.1, 0.2, 0.3]) + # plot with 1D-color: + axs = fig_test.subplots(5) + axs[0].plot([1, 2], [1, 2], c=color.reshape(-1)) + axs[1].scatter([1, 2], [1, 2], c=color.reshape(-1)) + axs[2].step([1, 2], [1, 2], c=color.reshape(-1)) + axs[3].hist(np.arange(10), color=color.reshape(-1)) + axs[4].bar(np.arange(10), np.arange(10), color=color.reshape(-1)) + # plot with 2D-color: + axs = fig_ref.subplots(5) + axs[0].plot([1, 2], [1, 2], c=color.reshape((1, -1))) + axs[1].scatter([1, 2], [1, 2], c=color.reshape((1, -1))) + axs[2].step([1, 2], [1, 2], c=color.reshape((1, -1))) + axs[3].hist(np.arange(10), color=color.reshape((1, -1))) + axs[4].bar(np.arange(10), np.arange(10), color=color.reshape((1, -1))) From 369ba34281bd1d8b8eb6c83ee86dec3b3da0213e Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 10 Sep 2020 18:55:45 +0200 Subject: [PATCH 3/4] Testing 2d to 1d conversion function in colors.to_rgba() + Rebase with master + Change .reshape((-1)) to .reshape(-1) --- lib/matplotlib/tests/test_colors.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index b91e4d2a58ee..42eb2912a9e4 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -1211,3 +1211,10 @@ def test_colormap_bad_data_with_alpha(): assert_array_equal(c[0, 0], (0, 0, 0, 0)) c = cmap([[np.nan, 0.5], [0, 0]], alpha=np.full((2, 2), 0.5)) assert_array_equal(c[0, 0], (0, 0, 0, 0)) + + +def test_2d_to_rgba(): + color = np.array([0.1, 0.2, 0.3]) + rgba_1d = mcolors.to_rgba(color.reshape(-1)) + rgba_2d = mcolors.to_rgba(color.reshape((1, -1))) + assert rgba_1d == rgba_2d From fa2acccb10e7b8e941eb6cf343df374bd200a27a Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 16 Sep 2020 15:45:03 +0200 Subject: [PATCH 4/4] Change .reshape((-1)) to .reshape(-1) remove white space --- lib/matplotlib/colors.py | 2 +- lib/matplotlib/tests/test_axes.py | 2 +- lib/matplotlib/tests/test_colors.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 387f55b3b09c..165fa2abbf0c 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -268,7 +268,7 @@ def _to_rgba_no_colorcycle(c, alpha=None): # turn 2-D array into 1-D array if isinstance(c, np.ndarray): if c.ndim == 2 and c.shape[0] == 1: - c = c.reshape((-1)) + c = c.reshape(-1) # tuple color. if not np.iterable(c): raise ValueError(f"Invalid RGBA argument: {orig_c!r}") diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 9607e8937332..76dba71045b1 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -6590,7 +6590,7 @@ def test_sharing_does_not_link_positions(): init_pos = ax1.get_position() fig.subplots_adjust(left=0) assert (ax1.get_position().get_points() == init_pos.get_points()).all() - + @check_figures_equal(extensions=["pdf"]) def test_2dcolor_plot(fig_test, fig_ref): diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 42eb2912a9e4..d180fb28afa5 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -1211,8 +1211,8 @@ def test_colormap_bad_data_with_alpha(): assert_array_equal(c[0, 0], (0, 0, 0, 0)) c = cmap([[np.nan, 0.5], [0, 0]], alpha=np.full((2, 2), 0.5)) assert_array_equal(c[0, 0], (0, 0, 0, 0)) - - + + def test_2d_to_rgba(): color = np.array([0.1, 0.2, 0.3]) rgba_1d = mcolors.to_rgba(color.reshape(-1))