From 8e6a3e5ef1f7a06db390df298612a722f995e8ed Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 25 Apr 2021 16:56:56 +0200 Subject: [PATCH] Support SubFigures in AxesDivider. --- examples/axes_grid1/simple_axes_divider1.py | 37 ++++++++++----------- examples/axes_grid1/simple_axes_divider3.py | 2 ++ lib/mpl_toolkits/axes_grid1/axes_divider.py | 18 +++++----- tutorials/toolkits/axes_grid.py | 2 +- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/examples/axes_grid1/simple_axes_divider1.py b/examples/axes_grid1/simple_axes_divider1.py index 0b4eb9dd904a..13c8f04afe64 100644 --- a/examples/axes_grid1/simple_axes_divider1.py +++ b/examples/axes_grid1/simple_axes_divider1.py @@ -2,6 +2,8 @@ ===================== Simple Axes Divider 1 ===================== + +See also :doc:`/tutorials/toolkits/axes_grid`. """ from mpl_toolkits.axes_grid1 import Size, Divider @@ -16,51 +18,48 @@ def label_axes(ax, text): left=False, labelleft=False) -############################################################################## -# Fixed axes sizes; fixed paddings. +fig = plt.figure(figsize=(12, 6)) +sfs = fig.subfigures(1, 2) -fig = plt.figure(figsize=(6, 6)) -fig.suptitle("Fixed axes sizes, fixed paddings") +sfs[0].suptitle("Fixed axes sizes, fixed paddings") # Sizes are in inches. horiz = [Size.Fixed(1.), Size.Fixed(.5), Size.Fixed(1.5), Size.Fixed(.5)] vert = [Size.Fixed(1.5), Size.Fixed(.5), Size.Fixed(1.)] rect = (0.1, 0.1, 0.8, 0.8) # Divide the axes rectangle into a grid with sizes specified by horiz * vert. -divider = Divider(fig, rect, horiz, vert, aspect=False) +div = Divider(sfs[0], rect, horiz, vert, aspect=False) # The rect parameter will actually be ignored and overridden by axes_locator. -ax1 = fig.add_axes(rect, axes_locator=divider.new_locator(nx=0, ny=0)) +ax1 = sfs[0].add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0)) label_axes(ax1, "nx=0, ny=0") -ax2 = fig.add_axes(rect, axes_locator=divider.new_locator(nx=0, ny=2)) +ax2 = sfs[0].add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2)) label_axes(ax2, "nx=0, ny=2") -ax3 = fig.add_axes(rect, axes_locator=divider.new_locator(nx=2, ny=2)) +ax3 = sfs[0].add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2)) label_axes(ax3, "nx=2, ny=2") -ax4 = fig.add_axes(rect, axes_locator=divider.new_locator(nx=2, nx1=4, ny=0)) +ax4 = sfs[0].add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0)) label_axes(ax4, "nx=2, nx1=4, ny=0") -############################################################################## -# Axes sizes that scale with the figure size; fixed paddings. - -fig = plt.figure(figsize=(6, 6)) -fig.suptitle("Scalable axes sizes, fixed paddings") +sfs[1].suptitle("Scalable axes sizes, fixed paddings") +# Fixed sizes are in inches, scaled sizes are relative. horiz = [Size.Scaled(1.5), Size.Fixed(.5), Size.Scaled(1.), Size.Scaled(.5)] vert = [Size.Scaled(1.), Size.Fixed(.5), Size.Scaled(1.5)] rect = (0.1, 0.1, 0.8, 0.8) # Divide the axes rectangle into a grid with sizes specified by horiz * vert. -divider = Divider(fig, rect, horiz, vert, aspect=False) +div = Divider(sfs[1], rect, horiz, vert, aspect=False) # The rect parameter will actually be ignored and overridden by axes_locator. -ax1 = fig.add_axes(rect, axes_locator=divider.new_locator(nx=0, ny=0)) +ax1 = sfs[1].add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0)) label_axes(ax1, "nx=0, ny=0") -ax2 = fig.add_axes(rect, axes_locator=divider.new_locator(nx=0, ny=2)) +ax2 = sfs[1].add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2)) label_axes(ax2, "nx=0, ny=2") -ax3 = fig.add_axes(rect, axes_locator=divider.new_locator(nx=2, ny=2)) +ax3 = sfs[1].add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2)) label_axes(ax3, "nx=2, ny=2") -ax4 = fig.add_axes(rect, axes_locator=divider.new_locator(nx=2, nx1=4, ny=0)) +ax4 = sfs[1].add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0)) label_axes(ax4, "nx=2, nx1=4, ny=0") + plt.show() diff --git a/examples/axes_grid1/simple_axes_divider3.py b/examples/axes_grid1/simple_axes_divider3.py index 6b7d9b4dd096..624a3be359f8 100644 --- a/examples/axes_grid1/simple_axes_divider3.py +++ b/examples/axes_grid1/simple_axes_divider3.py @@ -3,7 +3,9 @@ Simple Axes Divider 3 ===================== +See also :doc:`/tutorials/toolkits/axes_grid`. """ + import mpl_toolkits.axes_grid1.axes_size as Size from mpl_toolkits.axes_grid1 import Divider import matplotlib.pyplot as plt diff --git a/lib/mpl_toolkits/axes_grid1/axes_divider.py b/lib/mpl_toolkits/axes_grid1/axes_divider.py index 1ee42bfb571b..d459a2269c23 100644 --- a/lib/mpl_toolkits/axes_grid1/axes_divider.py +++ b/lib/mpl_toolkits/axes_grid1/axes_divider.py @@ -188,21 +188,21 @@ def locate(self, nx, ny, nx1=None, ny1=None, axes=None, renderer=None): renderer """ - figW, figH = self._fig.get_size_inches() + fig_w, fig_h = self._fig.bbox.size / self._fig.dpi x, y, w, h = self.get_position_runtime(axes, renderer) hsizes = self.get_horizontal_sizes(renderer) vsizes = self.get_vertical_sizes(renderer) - k_h = self._calc_k(hsizes, figW*w) - k_v = self._calc_k(vsizes, figH*h) + k_h = self._calc_k(hsizes, fig_w * w) + k_v = self._calc_k(vsizes, fig_h * h) if self.get_aspect(): k = min(k_h, k_v) ox = self._calc_offsets(hsizes, k) oy = self._calc_offsets(vsizes, k) - ww = (ox[-1] - ox[0]) / figW - hh = (oy[-1] - oy[0]) / figH + ww = (ox[-1] - ox[0]) / fig_w + hh = (oy[-1] - oy[0]) / fig_h pb = mtransforms.Bbox.from_bounds(x, y, w, h) pb1 = mtransforms.Bbox.from_bounds(x, y, ww, hh) pb1_anchored = pb1.anchored(self.get_anchor(), pb) @@ -218,8 +218,8 @@ def locate(self, nx, ny, nx1=None, ny1=None, axes=None, renderer=None): if ny1 is None: ny1 = ny + 1 - x1, w1 = x0 + ox[nx] / figW, (ox[nx1] - ox[nx]) / figW - y1, h1 = y0 + oy[ny] / figH, (oy[ny1] - oy[ny]) / figH + x1, w1 = x0 + ox[nx] / fig_w, (ox[nx1] - ox[nx]) / fig_w + y1, h1 = y0 + oy[ny] / fig_h, (oy[ny1] - oy[ny]) / fig_h return mtransforms.Bbox.from_bounds(x1, y1, w1, h1) @@ -649,7 +649,7 @@ def new_locator(self, nx, nx1=None): def locate(self, nx, ny, nx1=None, ny1=None, axes=None, renderer=None): # docstring inherited - fig_w, fig_h = self._fig.get_size_inches() + fig_w, fig_h = self._fig.bbox.size / self._fig.dpi x, y, w, h = self.get_position_runtime(axes, renderer) summed_ws = self.get_horizontal_sizes(renderer) equal_hs = self.get_vertical_sizes(renderer) @@ -684,7 +684,7 @@ def new_locator(self, ny, ny1=None): def locate(self, nx, ny, nx1=None, ny1=None, axes=None, renderer=None): # docstring inherited - fig_w, fig_h = self._fig.get_size_inches() + fig_w, fig_h = self._fig.bbox.size / self._fig.dpi x, y, w, h = self.get_position_runtime(axes, renderer) summed_hs = self.get_vertical_sizes(renderer) equal_ws = self.get_horizontal_sizes(renderer) diff --git a/tutorials/toolkits/axes_grid.py b/tutorials/toolkits/axes_grid.py index 7ce0ff35988f..433f578a312f 100644 --- a/tutorials/toolkits/axes_grid.py +++ b/tutorials/toolkits/axes_grid.py @@ -331,7 +331,7 @@ See the example, -.. figure:: ../../gallery/axes_grid1/images/sphx_glr_simple_axes_divider1_002.png +.. figure:: ../../gallery/axes_grid1/images/sphx_glr_simple_axes_divider1_001.png :target: ../../gallery/axes_grid1/simple_axes_divider1.html :align: center :scale: 50