From db343c3574c1dabd2854ead7b3272c380a43243f Mon Sep 17 00:00:00 2001 From: Jun Tan Date: Sun, 23 Oct 2016 17:43:15 -0700 Subject: [PATCH 01/10] fix the stack remove error --- lib/matplotlib/figure.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 287dc64dc9af..a9efb847d91e 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -125,11 +125,11 @@ def add(self, key, a): except TypeError: raise ValueError("first argument, %s, is not a valid key" % key) - a_existing = self.get(key) + a_existing = dict(self._elements).get(key) if a_existing is not None: Stack.remove(self, (key, a_existing)) warnings.warn( - "key %s already existed; Axes is being replaced" % key) + "key %s already existed; Axes is being replaced" % str(key)) # I don't think the above should ever happen. if a in self: @@ -362,7 +362,7 @@ def _repr_html_(self): # We can't use "isinstance" here, because then we'd end up importing # webagg unconditiionally. if (self.canvas is not None and - 'WebAgg' in self.canvas.__class__.__name__): + 'WebAgg' in self.canvas.__class__.__name__): from matplotlib.backends import backend_webagg return backend_webagg.ipython_inline_display(self) @@ -816,7 +816,7 @@ def _make_key(self, *args, **kwargs): 'make a hashable key out of args and kwargs' def fixitems(items): - #items may have arrays and lists in them, so convert them + # items may have arrays and lists in them, so convert them # to tuples for the key ret = [] for k, v in items: @@ -1745,7 +1745,7 @@ def subplots_adjust(self, *args, **kwargs): if not isinstance(ax, SubplotBase): # Check if sharing a subplots axis if (ax._sharex is not None and - isinstance(ax._sharex, SubplotBase)): + isinstance(ax._sharex, SubplotBase)): ax._sharex.update_params() ax.set_position(ax._sharex.figbox) elif (ax._sharey is not None and @@ -1921,8 +1921,8 @@ def figaspect(arg): # could become rc parameters, for now they're hardwired. figsize_min = np.array((4.0, 2.0)) # min length for width/height figsize_max = np.array((16.0, 16.0)) # max length for width/height - #figsize_min = rcParams['figure.figsize_min'] - #figsize_max = rcParams['figure.figsize_max'] + # figsize_min = rcParams['figure.figsize_min'] + # figsize_max = rcParams['figure.figsize_max'] # Extract the aspect ratio of the array if isarray: From 25492ce34efef9b216f5ea76668e9458273963bb Mon Sep 17 00:00:00 2001 From: Jun Tan Date: Mon, 24 Oct 2016 21:01:36 -0700 Subject: [PATCH 02/10] add test for the bug in AxesStack.add --- lib/matplotlib/tests/test_axesstack_add.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 lib/matplotlib/tests/test_axesstack_add.py diff --git a/lib/matplotlib/tests/test_axesstack_add.py b/lib/matplotlib/tests/test_axesstack_add.py new file mode 100644 index 000000000000..5a6f1a30bf8c --- /dev/null +++ b/lib/matplotlib/tests/test_axesstack_add.py @@ -0,0 +1,17 @@ +''' +Before fixing the bug, doing the following will give you following error: +>>> a.add('123',plt.figure().add_subplot(111)) +Traceback (most recent call last): + File "", line 1, in + File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 120, in add + Stack.remove(self, (key, a_existing)) + File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 1343, in remove + raise ValueError('Unknown element o') +ValueError: Unknown element o +''' +from matplotlib import figure +import matplotlib.pyplot as plt +a = figure.AxesStack() +a.add('123', plt.figure().add_subplot(111)) +print(a._elements) +a.add('123', plt.figure().add_subplot(111)) From 23cfcdad96505aabb28c8d9270e6a3abd2b90cb1 Mon Sep 17 00:00:00 2001 From: Jun Tan Date: Mon, 31 Oct 2016 19:29:59 -0700 Subject: [PATCH 03/10] move testing for Axes Stack add into test_figure.py --- lib/matplotlib/tests/test_figure.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 651a3b78f4c6..e7953abab48c 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -5,11 +5,12 @@ from six.moves import xrange from nose.tools import assert_equal, assert_true -from matplotlib import rcParams +from matplotlib import rcParams, figure from matplotlib.testing.decorators import image_comparison, cleanup from matplotlib.axes import Axes import matplotlib.pyplot as plt import numpy as np +import warnings @cleanup @@ -215,6 +216,20 @@ def test_figaspect(): w, h = plt.figaspect(np.zeros((2, 2))) assert h / w == 1 +@cleanup +def test_stack_remove(): + with warnings.catch_warnings(record=True) as w: + # Cause all warnings to always be triggered. + warnings.simplefilter("always") + # Trigger a warning. + key = '123' + a = figure.AxesStack() + a.add(key, plt.figure().add_subplot(111)) + a.add(key, plt.figure().add_subplot(111)) + # Verify some things + assert len(w) == 1 + assert key in str(w[-1].message) + if __name__ == "__main__": import nose From 6932d7cf30f2a72091130cb894b2c1898fa9f045 Mon Sep 17 00:00:00 2001 From: Jun Tan Date: Mon, 31 Oct 2016 19:33:05 -0700 Subject: [PATCH 04/10] fix style and add explanation --- lib/matplotlib/tests/test_figure.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index e7953abab48c..1c37ace80545 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -216,8 +216,22 @@ def test_figaspect(): w, h = plt.figaspect(np.zeros((2, 2))) assert h / w == 1 + @cleanup def test_stack_remove(): + ''' + Before fixing the bug, doing the following will give you following error: + >>> a.add('123',plt.figure().add_subplot(111)) + Traceback (most recent call last): + File "", line 1, in + File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 120, in + add + Stack.remove(self, (key, a_existing)) + File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 1343, in + remove + raise ValueError('Unknown element o') + ValueError: Unknown element o + ''' with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") From 0ab7a30571128c678786f19cf3d60b9f473ba125 Mon Sep 17 00:00:00 2001 From: JunTan Date: Tue, 1 Nov 2016 11:17:57 -0700 Subject: [PATCH 05/10] Delete test_axesstack_add.py remove unnecessary test --- lib/matplotlib/tests/test_axesstack_add.py | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 lib/matplotlib/tests/test_axesstack_add.py diff --git a/lib/matplotlib/tests/test_axesstack_add.py b/lib/matplotlib/tests/test_axesstack_add.py deleted file mode 100644 index 5a6f1a30bf8c..000000000000 --- a/lib/matplotlib/tests/test_axesstack_add.py +++ /dev/null @@ -1,17 +0,0 @@ -''' -Before fixing the bug, doing the following will give you following error: ->>> a.add('123',plt.figure().add_subplot(111)) -Traceback (most recent call last): - File "", line 1, in - File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 120, in add - Stack.remove(self, (key, a_existing)) - File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 1343, in remove - raise ValueError('Unknown element o') -ValueError: Unknown element o -''' -from matplotlib import figure -import matplotlib.pyplot as plt -a = figure.AxesStack() -a.add('123', plt.figure().add_subplot(111)) -print(a._elements) -a.add('123', plt.figure().add_subplot(111)) From 9d2d33e729465d8302b1bf723c70ddfc4f1e5b9f Mon Sep 17 00:00:00 2001 From: Jun Tan Date: Tue, 1 Nov 2016 14:07:56 -0700 Subject: [PATCH 06/10] change warning to raise error --- lib/matplotlib/figure.py | 8 +++----- lib/matplotlib/tests/test_figure.py | 16 +++++----------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index a9efb847d91e..5b662a48367a 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -124,13 +124,11 @@ def add(self, key, a): hash(key) except TypeError: raise ValueError("first argument, %s, is not a valid key" % key) - + a_existing = dict(self._elements).get(key) if a_existing is not None: - Stack.remove(self, (key, a_existing)) - warnings.warn( - "key %s already existed; Axes is being replaced" % str(key)) - # I don't think the above should ever happen. + raise KeyError('Key %s already exists in the AxesStack' % key) + # This only happens if user call this function directly if a in self: return None diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 1c37ace80545..610bd9d1945a 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -232,17 +232,11 @@ def test_stack_remove(): raise ValueError('Unknown element o') ValueError: Unknown element o ''' - with warnings.catch_warnings(record=True) as w: - # Cause all warnings to always be triggered. - warnings.simplefilter("always") - # Trigger a warning. - key = '123' - a = figure.AxesStack() - a.add(key, plt.figure().add_subplot(111)) - a.add(key, plt.figure().add_subplot(111)) - # Verify some things - assert len(w) == 1 - assert key in str(w[-1].message) + key = '123' + a = figure.AxesStack() + a.add(key, plt.figure().add_subplot(111)) + assert_raises(KeyError, a.add, key, plt.figure().add_subplot(111)) + # Verify some things if __name__ == "__main__": From 23a8d73f289f4606adf0016a82ae4f02e5eeb154 Mon Sep 17 00:00:00 2001 From: Jun Tan Date: Wed, 2 Nov 2016 11:43:26 -0700 Subject: [PATCH 07/10] correct pep8 style --- lib/matplotlib/figure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 5b662a48367a..c947631b75c4 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -124,7 +124,7 @@ def add(self, key, a): hash(key) except TypeError: raise ValueError("first argument, %s, is not a valid key" % key) - + a_existing = dict(self._elements).get(key) if a_existing is not None: raise KeyError('Key %s already exists in the AxesStack' % key) From 33318e3d2c4736a5714d1d3604cdd57bf692a310 Mon Sep 17 00:00:00 2001 From: Jun Tan Date: Wed, 2 Nov 2016 16:20:43 -0700 Subject: [PATCH 08/10] one more assert verifies re-add a key to the stack should not succeed or modify the old one --- lib/matplotlib/tests/test_figure.py | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 610bd9d1945a..4854a045ca2a 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -4,7 +4,7 @@ import six from six.moves import xrange -from nose.tools import assert_equal, assert_true +from nose.tools import assert_equal, assert_true, assert_raises from matplotlib import rcParams, figure from matplotlib.testing.decorators import image_comparison, cleanup from matplotlib.axes import Axes @@ -219,24 +219,13 @@ def test_figaspect(): @cleanup def test_stack_remove(): - ''' - Before fixing the bug, doing the following will give you following error: - >>> a.add('123',plt.figure().add_subplot(111)) - Traceback (most recent call last): - File "", line 1, in - File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 120, in - add - Stack.remove(self, (key, a_existing)) - File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 1343, in - remove - raise ValueError('Unknown element o') - ValueError: Unknown element o - ''' - key = '123' a = figure.AxesStack() - a.add(key, plt.figure().add_subplot(111)) - assert_raises(KeyError, a.add, key, plt.figure().add_subplot(111)) + key = '123' + axes = plt.figure().add_subplot(111) + a.add(key, axes) # Verify some things + assert_raises(KeyError, a.add, key, plt.figure().add_subplot(111)) + assert_equal(a._elements, [(key, (1, axes))]) if __name__ == "__main__": From 6dc9248371d80eac81c6ada3fc418b79e2ea67e1 Mon Sep 17 00:00:00 2001 From: Jun Tan Date: Mon, 21 Nov 2016 18:32:56 -0800 Subject: [PATCH 09/10] document the changes in AxesStack add --- doc/users/whats_new.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/doc/users/whats_new.rst b/doc/users/whats_new.rst index 1740f66c9ff2..2e58265104b6 100644 --- a/doc/users/whats_new.rst +++ b/doc/users/whats_new.rst @@ -19,6 +19,13 @@ New in matplotlib 2.0 matplotlib 2.0 supports Python 2.7, and 3.4+ +The behavior of AxesStack re-add key changes +-------------------------------------------- + +The AxesStack does not allow users to re-add the same key. If the user try +to do so, a KeyError will raise. + +:func:`~figure.AxesStack.add` Default style changes From 4c72fe7d5ad5e2e0cadeb7f0dfa7a3a49bcac28a Mon Sep 17 00:00:00 2001 From: Jun Tan Date: Tue, 22 Nov 2016 11:18:56 -0800 Subject: [PATCH 10/10] made the documentation of AxesStack.add nicer and clearer --- doc/users/whats_new.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/users/whats_new.rst b/doc/users/whats_new.rst index 2e58265104b6..0b60fb54fa0e 100644 --- a/doc/users/whats_new.rst +++ b/doc/users/whats_new.rst @@ -22,8 +22,10 @@ New in matplotlib 2.0 The behavior of AxesStack re-add key changes -------------------------------------------- -The AxesStack does not allow users to re-add the same key. If the user try -to do so, a KeyError will raise. +The AxesStack does not allow users to re-add the same key. If the user tries +to do so, a KeyError will be raised. +If the user really wants to re-add the same key, then the old one should +be removed first by using :func:`AxesStack.remove` and re-add the new key. :func:`~figure.AxesStack.add`