FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Unexpected error after using Figure.canvas.draw on macosx backend · Issue #19197 · matplotlib/matplotlib · GitHub

Repository navigation

Unexpected error after using Figure.canvas.draw on macosx backend #19197

Closed

Description

Bug report

Bug summary

It has been claimed that "fig.canvas.draw() is the canonical way to trigger a draw", replacing fig.draw(fig.canvas.get_renderer()). But on the macosx backend, this seems not to work.

Code for reproduction

import matplotlib.pyplot as plt
f, ax = plt.subplots()
lines = ax.plot([0, 1])
legend = ax.legend(lines, ["a line"])
f.canvas.draw()
# f.draw(f.canvas.get_renderer())
print(legend.get_window_extent())

Actual outcome

Traceback (most recent call last):
  File "mpl_issue.py", line 7, in <module>
    print(legend.get_window_extent())
  File "/Users/mwaskom/miniconda3/lib/python3.7/site-packages/matplotlib/legend.py", line 897, in get_window_extent
    return self._legend_box.get_window_extent(renderer=renderer)
  File "/Users/mwaskom/miniconda3/lib/python3.7/site-packages/matplotlib/offsetbox.py", line 346, in get_window_extent
    w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
  File "/Users/mwaskom/miniconda3/lib/python3.7/site-packages/matplotlib/offsetbox.py", line 462, in get_extent_offsets
    dpicor = renderer.points_to_pixels(1.)
AttributeError: 'NoneType' object has no attribute 'points_to_pixels'

Expected outcome

On the qt5agg backend, or when uncommenting the (deprecated?) f.draw(f.canvas.get_renderer()) line, the code runs and prints the extent of the legend.

Matplotlib version

  • Operating system: Macos
  • Matplotlib version: 3.3.3
  • Matplotlib backend (print(matplotlib.get_backend())): macosx
  • Python version: 3.8
  • Jupyter version (if applicable): na
  • Other libraries:

xref #18407, mwaskom/seaborn#2399

Activity

  1. added this to the v3.4.0 milestone on Dec 30, 2020
  2. tacaswell commented on Dec 30, 2020

    Member

    The issues appears to be that draw defers to draw_idle with the OSX backend

    def draw(self):
    # docstring inherited
    self.draw_idle()
    self.flush_events()
    . I think the fix is to add a call to self._draw() before the self.draw_idle() in L50.

    I suspect some of this complexity is left from the previous version of the OSX backend which did the rendering via osx-native methods (rather than pushing Agg output into the GUI like it does now (and like the rest of the GUI backends do)) which meant we could not get access to the renderer except when we were handed it by the OSX event loop.

  3. mwaskom commented on Dec 30, 2020

    Author

    Is there a good way to programmatically check that the draw actually happened? i.e so something like

    fig.canvas.draw()
    if not fig.canvas.has_been_drawn():
        fig.draw(fig.canvas.get_renderer())

    could be used for backwards compatibility.

  4. tacaswell commented on Dec 30, 2020

    Member

    If you look at fig.stale (which comes from Artist so every object in the tree has this state). That state tracks if the artist (or any of its children) have changed from the last time it has rendered. It get initialized to False should should mostly solve the problem you are having.

  5. modified the milestones: v3.4.0, v3.4.1 on Feb 18, 2021
  6. modified the milestones: v3.4.1, v3.4.2 on Mar 30, 2021
  7. added a commit that references this issue on Apr 26, 2021
  8. modified the milestones: v3.4.2, v3.4.3 on May 5, 2021
  9. modified the milestones: v3.4.3, v3.4.4 on Aug 12, 2021
  10. modified the milestones: v3.4.4, v3.5.1 on Nov 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL