Skip to content

Commit 5a5edb1

Browse files
committed
Re-apply tight layout in Figure.draw for popup backends
1 parent d53dd09 commit 5a5edb1

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

proplot/subplots.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,14 @@ def _canvas_preprocess(canvas, method):
499499
# complicated, dangerous, and result in unnecessary extra draws.
500500
def _preprocess(self, *args, **kwargs):
501501
fig = self.figure # update even if not stale! needed after saves
502+
if method == 'draw_idle' and (
503+
self._is_idle_drawing # standard
504+
or getattr(self, '_draw_pending', None) # pyqt5
505+
):
506+
# For now we override 'draw' and '_draw' rather than 'draw_idle'
507+
# but may change mind in the future. This breakout condition is
508+
# copied from the matplotlib source.
509+
return
502510
if method == 'print_figure':
503511
# When re-generating inline figures, the tight layout algorithm
504512
# can get figure size *or* spacing wrong unless we force additional
@@ -1582,6 +1590,23 @@ def colorbar(self, *args,
15821590
row=row, col=col, rows=rows, cols=cols)
15831591
return ax.colorbar(*args, loc='_fill', **kwargs)
15841592

1593+
def draw(self, renderer):
1594+
# Certain backends *still* have issues with the tight layout
1595+
# algorithm e.g. due to opening windows in *tabs*. Have not found way
1596+
# to intervene in the FigureCanvas. For this reason we *also* apply
1597+
# the algorithm inside Figure.draw in the same way that matplotlib
1598+
# applies its tight layout algorithm. So far we just do this for Qt*
1599+
# and MacOSX; corrections are generally *small* but notable!
1600+
if not self.get_visible():
1601+
return
1602+
if self._auto_tight and (
1603+
rc['backend'] == 'MacOSX' or rc['backend'][:2] == 'Qt'
1604+
):
1605+
self._adjust_tight_layout(renderer, resize=False)
1606+
self._align_axislabels(True) # if spaces changed need to realign
1607+
self._align_labels(renderer)
1608+
return super().draw(renderer)
1609+
15851610
def legend(self, *args,
15861611
loc='r', width=None, space=None,
15871612
row=None, col=None, rows=None, cols=None, span=None,
@@ -1651,9 +1676,9 @@ def set_canvas(self, canvas):
16511676
# `~matplotlib.backend_bases.FigureCanvasBase.print_figure`
16521677
# methods. The latter is called by save() and by the inline backend.
16531678
# See `_canvas_preprocess` for details."""
1654-
# NOTE: Cannot use draw_idle() because it causes *major* complications
1655-
# for qt5 backend. Even though usage is less consistent we *must*
1656-
# use draw() and _draw().
1679+
# NOTE: Cannot use draw_idle() because it causes complications for qt5
1680+
# backend (wrong figure size). Even though usage is less consistent we
1681+
# *must* use draw() and _draw() instead.
16571682
if hasattr(canvas, '_draw'):
16581683
canvas._draw = _canvas_preprocess(canvas, '_draw')
16591684
else:

0 commit comments

Comments
 (0)