From 981f273b336b2a8d998f5976cc2d60bc0d13a63b Mon Sep 17 00:00:00 2001 From: vidursatija Date: Sat, 25 Mar 2017 00:56:53 +0530 Subject: [PATCH 1/3] Issue #8299, implemented copy, added test --- lib/matplotlib/colors.py | 11 +++++++++++ lib/matplotlib/tests/test_colors.py | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 59c17c8a9392..4fe27739e7e8 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -530,6 +530,17 @@ def __call__(self, X, alpha=None, bytes=False): rgba = tuple(rgba[0, :]) return rgba + def __copy__(self): + """Create new object with the same class and update attributes + If object is initialized, copy the elements of _lut list + """ + cls = self.__class__ + newCMapObj = cls.__new__(cls) + newCMapObj.__dict__.update(self.__dict__) + if self._isinit: + newCMapObj._lut = np.copy(self._lut) + return newCMapObj + def set_bad(self, color='k', alpha=None): """Set color to be used for masked values. """ diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 0d1243c94552..e515b2af12b5 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -1,6 +1,7 @@ from __future__ import (absolute_import, division, print_function, unicode_literals) +import copy import six import itertools import warnings @@ -44,6 +45,32 @@ def test_resample(): assert_array_almost_equal(lc3([0, 0.5, 1]), expected) +def test_colormap_copy(): + cm = plt.cm.Reds + cm([-1, 0, .5, np.nan, np.inf]) + cm.set_bad('y') + cm2 = copy.copy(cm) + cm2.set_bad('b') + cm.set_under('k') + cm2.set_under('r') + cm.set_over('c') + cm2.set_over('m') + + expected1 = plt.cm.Reds + expected2 = plt.cm.Reds + expected1([-1, 0, .5, np.nan, np.inf]) + expected2([-1, 0, .5, np.nan, np.inf]) + expected1.set_bad('y') + expected2.set_bad('b') + expected1.set_under('k') + expected2.set_under('r') + expected1.set_over('c') + expected2.set_over('m') + + assert_array_equal(cm._lut, expected1._lut) + assert_array_equal(cm2._lut, expected2._lut) + + def test_colormap_endian(): """ Github issue #1005: a bug in putmask caused erroneous From 90f5f15549d4a891849e64d39f070bb8e1eb7c75 Mon Sep 17 00:00:00 2001 From: Vidur Satija Date: Sat, 25 Mar 2017 12:27:31 +0530 Subject: [PATCH 2/3] Removed _lut from docs, changed to snake_case --- lib/matplotlib/colors.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 4fe27739e7e8..92d41bd8100b 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -531,15 +531,14 @@ def __call__(self, X, alpha=None, bytes=False): return rgba def __copy__(self): - """Create new object with the same class and update attributes - If object is initialized, copy the elements of _lut list + """Create new object with the same class, update attributes """ cls = self.__class__ - newCMapObj = cls.__new__(cls) - newCMapObj.__dict__.update(self.__dict__) + cmapobject = cls.__new__(cls) + cmapobject.__dict__.update(self.__dict__) if self._isinit: - newCMapObj._lut = np.copy(self._lut) - return newCMapObj + cmapobject._lut = np.copy(self._lut) + return cmapobject def set_bad(self, color='k', alpha=None): """Set color to be used for masked values. From 157936419c452ed9310a3c79647449895c1ec599 Mon Sep 17 00:00:00 2001 From: Vidur Satija Date: Sun, 14 May 2017 10:39:23 +0530 Subject: [PATCH 3/3] Issue #8375 and #8299 fixed Implemented with no leak into Reds --- lib/matplotlib/tests/test_colors.py | 30 ++++++++--------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index e515b2af12b5..c9b0be4837c3 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -47,28 +47,14 @@ def test_resample(): def test_colormap_copy(): cm = plt.cm.Reds - cm([-1, 0, .5, np.nan, np.inf]) - cm.set_bad('y') - cm2 = copy.copy(cm) - cm2.set_bad('b') - cm.set_under('k') - cm2.set_under('r') - cm.set_over('c') - cm2.set_over('m') - - expected1 = plt.cm.Reds - expected2 = plt.cm.Reds - expected1([-1, 0, .5, np.nan, np.inf]) - expected2([-1, 0, .5, np.nan, np.inf]) - expected1.set_bad('y') - expected2.set_bad('b') - expected1.set_under('k') - expected2.set_under('r') - expected1.set_over('c') - expected2.set_over('m') - - assert_array_equal(cm._lut, expected1._lut) - assert_array_equal(cm2._lut, expected2._lut) + cm_copy = copy.copy(cm) + with np.errstate(invalid='ignore'): + ret1 = cm_copy([-1, 0, .5, 1, np.nan, np.inf]) + cm2 = copy.copy(cm_copy) + cm2.set_bad('g') + with np.errstate(invalid='ignore'): + ret2 = cm_copy([-1, 0, .5, 1, np.nan, np.inf]) + assert_array_equal(ret1, ret2) def test_colormap_endian():