Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Closed
@mwaskom

Description

@mwaskom

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions