From 308d2ac9f9d7be07b37b01fd9206dffc1851bec0 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 7 Sep 2018 16:16:45 +0200 Subject: [PATCH] Avoid TransformedBbox where unneeded. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit transform_bbox is much faster when one does not need a TransformedBbox, i.e. something that keeps track of later changes in the transform: ``` In [1]: from pylab import * ...: from matplotlib.transforms import * ...: tr = plt.gca().transAxes ...: bb = Bbox([[0, 0], [1, 1]]) ...: %timeit TransformedBbox(bb, tr).x0 ...: %timeit tr.transform_bbox(bb).x0 ...: %timeit TransformedBbox(bb, tr).width ...: %timeit tr.transform_bbox(bb).width 11.8 µs ± 145 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 2.72 µs ± 31.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 11.9 µs ± 183 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 2.83 µs ± 16.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) ``` --- lib/matplotlib/tight_bbox.py | 4 +--- lib/matplotlib/tight_layout.py | 5 ++--- lib/mpl_toolkits/axes_grid1/inset_locator.py | 2 +- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/tight_bbox.py b/lib/matplotlib/tight_bbox.py index 5904ebc1fa1c..bd58833baf88 100644 --- a/lib/matplotlib/tight_bbox.py +++ b/lib/matplotlib/tight_bbox.py @@ -57,11 +57,9 @@ def restore_bbox(): tr = Affine2D().scale(fixed_dpi) dpi_scale = fixed_dpi / fig.dpi - _bbox = TransformedBbox(bbox_inches, tr) - fig.bbox_inches = Bbox.from_bounds(0, 0, bbox_inches.width, bbox_inches.height) - x0, y0 = _bbox.x0, _bbox.y0 + x0, y0 = tr.transform(bbox_inches.p0) w1, h1 = fig.bbox.width * dpi_scale, fig.bbox.height * dpi_scale fig.transFigure._boxout = Bbox.from_bounds(-x0, -y0, w1, h1) fig.transFigure.invalidate() diff --git a/lib/matplotlib/tight_layout.py b/lib/matplotlib/tight_layout.py index 809b970915a9..66283c7d7348 100644 --- a/lib/matplotlib/tight_layout.py +++ b/lib/matplotlib/tight_layout.py @@ -13,7 +13,7 @@ from matplotlib import _api, rcParams from matplotlib.font_manager import FontProperties -from matplotlib.transforms import TransformedBbox, Bbox +from matplotlib.transforms import Bbox def _auto_adjust_subplotpars( @@ -84,8 +84,7 @@ def _auto_adjust_subplotpars( bb += [ax.get_tightbbox(renderer)] tight_bbox_raw = Bbox.union(bb) - tight_bbox = TransformedBbox(tight_bbox_raw, - fig.transFigure.inverted()) + tight_bbox = fig.transFigure.inverted().transform_bbox(tight_bbox_raw) hspaces[rowspan, colspan.start] += ax_bbox.xmin - tight_bbox.xmin # l hspaces[rowspan, colspan.stop] += tight_bbox.xmax - ax_bbox.xmax # r diff --git a/lib/mpl_toolkits/axes_grid1/inset_locator.py b/lib/mpl_toolkits/axes_grid1/inset_locator.py index 5193cc540c31..fcdb32851b8c 100644 --- a/lib/mpl_toolkits/axes_grid1/inset_locator.py +++ b/lib/mpl_toolkits/axes_grid1/inset_locator.py @@ -126,7 +126,7 @@ def __init__(self, parent_axes, zoom, loc, bbox_transform=bbox_transform) def get_extent(self, renderer): - bb = TransformedBbox(self.axes.viewLim, self.parent_axes.transData) + bb = self.parent_axes.transData.transform_bbox(self.axes.viewLim) fontsize = renderer.points_to_pixels(self.prop.get_size_in_points()) pad = self.pad * fontsize return (abs(bb.width * self.zoom) + 2 * pad,