Skip to content

Bug in AxesStack.add #7194

Closed
Closed

Description

@brianwa84

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.

Activity

  1. brianwa84 commented on Sep 28, 2016

    @brianwa84
    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

  2. added this to the milestone on Sep 28, 2016
  3. tacaswell commented on Sep 28, 2016

    @tacaswell
    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).

  4. brianwa84 commented on Sep 28, 2016

    @brianwa84
    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.

  5. brianwa84 commented on Sep 28, 2016

    @brianwa84
    Author
    >>> 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
    
  6. brianwa84 commented on Sep 29, 2016

    @brianwa84
    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))
    
  7. added
    Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
    on Oct 8, 2016
  8. JunTan commented on Oct 18, 2016

    @JunTan
    Contributor

    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
  9. brianwa84 commented on Oct 19, 2016

    @brianwa84
    Author

    Yes somewhere I probably import matplotlib.pyplot as plt

    On Oct 18, 2016 6:07 PM, "JunTan" [email protected] 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
    .

  10. modified the milestones: , on May 3, 2017
  11. anntzer commented on Sep 16, 2017

    @anntzer
    Contributor

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

  12. modified the milestones: , on Sep 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions