Skip to content

FIX: the new _AxesStack with np.array as input #9029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,38 @@ class _AxesStack(object):
"""Lightweight stack that tracks Axes in a Figure.
"""

@staticmethod
def __key_compare(k1, k2):
k1_args, k1_kwargs = k1
k2_args, k2_kwargs = k2

def np_safe_eq(left, right):
out = (left == right)
try:
out = bool(out)
except ValueError:
out = out.all()
try:
out &= len(left) == len(right)
except TypeError:
out = False
return out

for a, b in zip(k1_args, k2_args):
test = np_safe_eq(a, b)
if not test:
return False
if set(k1_kwargs) != set(k2_kwargs):
return False

for k in k1_kwargs:
a = k1_kwargs[k]
b = k2_kwargs[k]
test = np_safe_eq(a, b)
if not test:
return False
return True

def __init__(self):
# We maintain a list of (creation_index, key, axes) tuples.
# We do not use an OrderedDict because 1. the keys may not be hashable
Expand All @@ -179,7 +211,10 @@ def as_list(self):
def get(self, key):
"""Find the axes corresponding to a key; defaults to `None`.
"""
return next((ax for _, k, ax in self._items if k == key), None)
return next((ax
for _, k, ax in self._items
if self.__key_compare(k, key)),
None)

def current_key_axes(self):
"""Return the topmost `(key, axes)` pair, or `(None, None)` if empty.
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,11 @@ def test_subplots_shareax_loglabels():

for ax in ax_arr[:, 0]:
assert 0 < len(ax.yaxis.get_ticklabels(which='both'))


def test_axes_add_np_behavior():
ax1 = plt.axes(np.array([.1, .1, .8, .8]))
ax2 = plt.axes(np.array([.1, .1, .8, .8]))
# in the future this test will need to be changed to not assert
# that the axes are equal, but still check that this does not blowup.
assert ax1 is ax2