Skip to content

Commit 5ebc9e1

Browse files
committed
Minor internals _add_canvas cleanup
1 parent a6022de commit 5ebc9e1

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

proplot/figure.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ def _get_journal_size(preset):
421421
return figwidth, figheight
422422

423423

424-
def _add_canvas_preprocessor(canvas, method):
424+
def _add_canvas_preprocessor(canvas, method, cache=False):
425425
"""
426426
Return a pre-processer that can be used to override instance-level
427427
canvas draw() and print_figure() methods. This applies tight layout
@@ -456,15 +456,8 @@ def _canvas_preprocess(self, *args, **kwargs):
456456
# Adjust layout
457457
# NOTE: The authorized_context is needed because some backends disable
458458
# constrained layout or tight layout before printing the figure.
459-
# NOTE: *Critical* to not add print_figure renderer to the cache when the print
460-
# method (print_pdf, print_png, etc.) calls Figure.draw(). Otherwise have issues
461-
# where (1) figure size and/or figure bounds are incorrect after saving figure
462-
# *then* displaying it in qt or inline notebook backends, and (2) figure fails
463-
# to update correctly after successively modifying and displaying within inline
464-
# notebook backend (previously worked around this by forcing additional draw()
465-
# call in this function before proceeding with print_figure).
466-
ctx1 = fig._context_adjusting(cache=(method != 'print_figure'))
467-
ctx2 = fig._context_authorized() # backends might call set_constrained_layout()
459+
ctx1 = fig._context_adjusting(cache=cache)
460+
ctx2 = fig._context_authorized() # skip backend set_constrained_layout()
468461
ctx3 = rc.context(fig._render_context) # draw with figure-specific setting
469462
with ctx1, ctx2, ctx3:
470463
fig.auto_layout()
@@ -1737,14 +1730,20 @@ def set_canvas(self, canvas):
17371730
--------
17381731
matplotlib.figure.Figure.set_canvas
17391732
"""
1740-
# Set the canvas and add monkey patches to the instance-level draw and
1741-
# print_figure methods. The latter is called by save() and by the inline
1742-
# backend. See `_add_canvas_preprocessor` for details.
1743-
_add_canvas_preprocessor(canvas, 'print_figure')
1744-
if callable(getattr(canvas, '_draw', None)): # for macosx backend
1745-
_add_canvas_preprocessor(canvas, '_draw')
1746-
else:
1747-
_add_canvas_preprocessor(canvas, 'draw')
1733+
# NOTE: Use the _draw method if it exists, e.g. for osx backends. Critical
1734+
# or else wrong renderer size is used.
1735+
# NOTE: See _add_canvas_preprocessor for details. Critical to not add cache
1736+
# print_figure renderer when the print method (print_pdf, print_png, etc.)
1737+
# calls Figure.draw(). Otherwise have issues where (1) figure size and/or
1738+
# bounds are incorrect after saving figure *then* displaying it in qt or inline
1739+
# notebook backends, and (2) figure fails to update correctly after successively
1740+
# modifying and displaying within inline notebook backend (previously worked
1741+
# around this by forcing additional draw() call in this function before
1742+
# proceeding with print_figure). Set the canvas and add monkey patches
1743+
# to the instance-level draw and print_figure methods.
1744+
method = '_draw' if callable(getattr(canvas, '_draw', None)) else 'draw'
1745+
_add_canvas_preprocessor(canvas, 'print_figure', cache=False) # saves, inlines
1746+
_add_canvas_preprocessor(canvas, method, cache=True) # renderer displays
17481747
super().set_canvas(canvas)
17491748

17501749
def _is_same_size(self, figsize, eps=None):

0 commit comments

Comments
 (0)