Skip to content

Bug in AxesStack.add #7194

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
5 tasks
brianwa84 opened this issue Sep 28, 2016 · 8 comments
Closed
5 tasks

Bug in AxesStack.add #7194

brianwa84 opened this issue Sep 28, 2016 · 8 comments
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues

Comments

@brianwa84
Copy link

I think I've found a bug where the format of data actually stored in Stack is different from that expected in Stack.remove. It seems that the line below needs to be:
Stack.remove(self, self._entry_from_axes(a_existing))

I don't have a minimal repro case (probably something to do with multi-threading in a tensorflow py_func), but I've been seeing exceptions in the .remove method. Adding some more useful exception messaging shows me a missing self._ind 0th tuple element.


AxesStack.add(((1, 1, 1), ()),..): Axes(0.125,0.1;0.775x0.8) vs Axes(0.125,0.1;0.775x0.8)
                         ^key                ^ a                     ^a_existing

matplotlib/cbook.py", line 1410, in remove
    raise ValueError('Unknown element o: %s vs %s' % (o, self._elements))

Unknown element o: (((1, 1, 1), ()), <matplotlib.axes._subplots.AxesSubplot object at 0
x7fd416508550>) vs [(((1, 1, 1), ()), (1, <matplotlib.axes._subplots.AxesSubplot object at 0x7fd416
508550>))]

    def add(self, key, a):
        """
        Add Axes *a*, with key *key*, to the stack, and return the stack.
        If *a* is already on the stack, don't add it again, but
        return *None*.
        """
        # All the error checking may be unnecessary; but this method
        # is called so seldom that the overhead is negligible.
        if not isinstance(a, Axes):
            raise ValueError("second argument, %s, is not an Axes" % a)
        try:
            hash(key)
        except TypeError:
            raise ValueError("first argument, %s, is not a valid key" % key)

        a_existing = self.get(key)
        if a_existing is not None:
HERE-->            Stack.remove(self, (key, a_existing))
            warnings.warn(
                "key %s already existed; Axes is being replaced" % key)
            # I don't think the above should ever happen.

        if a in self:
            return None
        self._ind += 1
        return Stack.push(self, (key, (self._ind, a)))


To help us understand and resolve your issue please check that you have provided
the information below.

  • Matplotlib version, Python version and Platform (Windows, OSX, Linux ...)
  • How did you install Matplotlib and Python (pip, anaconda, from source ...)
  • If possible please supply a Short, Self Contained, Correct, Example
    that demonstrates the issue i.e a small piece of code which reproduces the issue
    and can be run with out any other (or as few as possible) external dependencies.
  • If this is an image generation bug attach a screenshot demonstrating the issue.
  • If this is a regression (Used to work in an earlier version of Matplotlib), please
    note where it used to work.
@brianwa84
Copy link
Author

Also: you'll want to change the warnings.warn line to use str(key). Otherwise once you fix the line in question, you get an exception

"key %s already existed; Axes is being replaced" % key)

TypeError: not all arguments converted during string formatting

@tacaswell tacaswell added this to the 2.0.1 (next bug fix release) milestone Sep 28, 2016
@tacaswell tacaswell added the status: needs clarification Issues that need more information to resolve. label Sep 28, 2016
@tacaswell
Copy link
Member

I am having a hard time understanding what is going on here (and which parts of the above are existing code, which parts are output, and which parts are your markup on either of the above).

@brianwa84
Copy link
Author

Sorry, I'll try to be a bit clearer:

The bug is here:
https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/figure.py#L130

The issue is that a_existing is an Axes object, whereas the items in self._elements are tuples of (key, (index, Axes)) (see the Stack.push call on L138). So when you try to remove a tuple of (key, Axes), Stack fails with the cryptic exception "Unknown element o". If you muck with the code in Stack.remove to throw ValueError("Unknown element o: %s vs %s" % (o, self._elements)), you get what I pasted in there. That's the indication that the (index,Axes) tuple has been swapped for Axes.

@brianwa84
Copy link
Author

brianwa84 commented Sep 28, 2016

>>> from matplotlib import figure
>>> a=figure.AxesStack()
>>> a.add('123',plt.figure().add_subplot(111))
<matplotlib.axes.AxesSubplot object at 0x7f13d27a3490>
>>> a._elements
[('123', (1, <matplotlib.axes.AxesSubplot object at 0x7f13d27a3490>))]
>>> a.add('123',plt.figure().add_subplot(111))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  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

@tacaswell tacaswell removed the status: needs clarification Issues that need more information to resolve. label Sep 28, 2016
@brianwa84
Copy link
Author

My local patch is as follows:

129c129
<             Stack.remove(self, (key, a_existing))
---
>             self.remove(a_existing)
131c131
<                 "key %s already existed; Axes is being replaced" % key)
---
>                 "key %s already existed; Axes is being replaced" % str(key))

@NelleV NelleV added Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Machine Shop labels Oct 8, 2016
@JunTan
Copy link
Contributor

JunTan commented Oct 18, 2016

Did you define plt somewhere else? I got this error when I the above code.

>>> from matplotlib import figure
>>> a=figure.AxesStack()
>>> a.add('123',plt.figure().add_subplot(111))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'plt' is not defined

@brianwa84
Copy link
Author

Yes somewhere I probably import matplotlib.pyplot as plt

On Oct 18, 2016 6:07 PM, "JunTan" notifications@github.com wrote:

Did you define plt somewhere else? I got this error when I the above code.

from matplotlib import figure>>> a=figure.AxesStack()>>> a.add('123',plt.figure().add_subplot(111))
Traceback (most recent call last):
File "", line 1, in NameError: name 'plt' is not defined


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#7194 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AVJZI_MuntnSXw3LzwX-IyHALxn895jLks5q1UMYgaJpZM4KJEeO
.

@QuLogic QuLogic modified the milestones: 2.0.1 (next bug fix release), 2.0.2 (next bug fix release) May 3, 2017
@anntzer
Copy link
Contributor

anntzer commented Sep 16, 2017

Closing as we're just going (in the long run) to deprecate AxesStack (or rather, make it private). See #9037 and linked issues.

@anntzer anntzer closed this as completed Sep 16, 2017
@QuLogic QuLogic modified the milestones: 2.1.1 (next bug fix release), unassigned Sep 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
Projects
None yet
Development

No branches or pull requests

6 participants