From a2c4340956d85ff3bd7010dcd7dd5fca5f635db0 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 5 Mar 2017 21:35:11 -0800 Subject: [PATCH] Avoid inverting axes when tight_layout is cramped. I chose to not raise an exception in this case as uncaught exceptions are fatal for the Qt5 backend. --- lib/matplotlib/tight_layout.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/tight_layout.py b/lib/matplotlib/tight_layout.py index 1f61d95e9f2f..ae8d06ae65a4 100644 --- a/lib/matplotlib/tight_layout.py +++ b/lib/matplotlib/tight_layout.py @@ -11,11 +11,12 @@ import warnings +import numpy as np + import matplotlib +from matplotlib import rcParams from matplotlib.transforms import TransformedBbox, Bbox - from matplotlib.font_manager import FontProperties -rcParams = matplotlib.rcParams def _get_left(tight_bbox, axes_bbox): @@ -193,13 +194,19 @@ def auto_adjust_subplotpars(fig, renderer, for i in range(rows) for s in hspaces[i * (cols + 1) + 1:(i + 1) * (cols + 1) - 1]) + hpad_inches / fig_width_inch) - h_axes = (1 - margin_right - margin_left - hspace * (cols - 1)) / cols + # Make sure that `h_axes` (and, below, `v_axes`) stays positive, even + # when the axes are cramped. + h_axes = max( + (1 - margin_right - margin_left - hspace * (cols - 1)) / cols, + np.finfo(float).resolution) kwargs["wspace"] = hspace / h_axes if rows > 1: vspace = (max(sum(s) for s in vspaces[cols:-cols]) + vpad_inches / fig_height_inch) - v_axes = (1 - margin_top - margin_bottom - vspace * (rows - 1)) / rows + v_axes = max( + (1 - margin_top - margin_bottom - vspace * (rows - 1)) / rows, + np.finfo(float).resolution) kwargs["hspace"] = vspace / v_axes return kwargs