Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

implement tight_layout.get_subplotspec_list and uses this to check if…

… all the axes are supported by tight_layout
  • Loading branch information...
commit 77e245a5b9dc6da86a6529f2e4db59f9d23a60da 1 parent 0c7cdaa
@leejjoon authored
Showing with 37 additions and 18 deletions.
  1. +4 −4 lib/matplotlib/figure.py
  2. +33 −14 lib/matplotlib/tight_layout.py
View
8 lib/matplotlib/figure.py
@@ -1422,17 +1422,17 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None, rect=Non
labels) will fit into. Default is (0, 0, 1, 1).
"""
- from tight_layout import get_renderer, get_tight_layout_figure
+ from tight_layout import get_renderer, get_tight_layout_figure, \
+ get_subplotspec_list
- subplot_axes = [ax for ax in self.axes if isinstance(ax, SubplotBase)]
- if len(subplot_axes) < len(self.axes):
+ if None in get_subplotspec_list(self.axes):
warnings.warn("tight_layout can only process Axes that descend "
"from SubplotBase; results might be incorrect.")
if renderer is None:
renderer = get_renderer(self)
- kwargs = get_tight_layout_figure(self, subplot_axes, renderer,
+ kwargs = get_tight_layout_figure(self, self.axes, renderer,
pad=pad, h_pad=h_pad, w_pad=w_pad,
rect=rect)
View
47 lib/matplotlib/tight_layout.py
@@ -209,6 +209,33 @@ def get_renderer(fig):
return renderer
+def get_subplotspec_list(axes_list):
+ """
+ Return a list of subplotspec from the given list of axes. For an
+ instance of axes that does not support subplotspec, None is
+ inserted in the list.
+
+ """
+ subplotspec_list = []
+ for ax in axes_list:
+ locator = ax.get_axes_locator()
+ if hasattr(locator, "get_subplotspec"):
+ subplotspec = locator.get_subplotspec().get_topmost_subplotspec()
+ elif hasattr(ax, "get_subplotspec"):
+ subplotspec = ax.get_subplotspec().get_topmost_subplotspec()
+ else:
+ subplotspec = None
+
+ if subplotspec is not None and \
+ subplotspec.get_gridspec().locally_modified_subplot_params():
+
+ subplotspec = None
+
+ subplotspec_list.append(subplotspec)
+
+ return subplotspec_list
+
+
def get_tight_layout_figure(fig, axes_list, renderer,
pad=1.08, h_pad=None, w_pad=None, rect=None):
"""
@@ -244,21 +271,13 @@ def get_tight_layout_figure(fig, axes_list, renderer,
ncols_list = []
ax_bbox_list = []
- subplot_dict = {} # for axes_grid1, multiple axes can share
- # same subplot_interface. Thus we need to
- # join them together.
-
- for ax in axes_list:
- locator = ax.get_axes_locator()
- if hasattr(locator, "get_subplotspec"):
- subplotspec = locator.get_subplotspec().get_topmost_subplotspec()
- elif hasattr(ax, "get_subplotspec"):
- subplotspec = ax.get_subplotspec().get_topmost_subplotspec()
- else:
- continue
+ subplot_dict = {} # multiple axes can share
+ # same subplot_interface (e.g, axes_grid1). Thus
+ # we need to join them together.
- if (subplotspec is None) or \
- subplotspec.get_gridspec().locally_modified_subplot_params():
+ for ax, subplotspec in zip(axes_list,
+ get_subplotspec_list(axes_list)):
+ if subplotspec is None:
continue
subplots = subplot_dict.setdefault(subplotspec, [])
Please sign in to comment.
Something went wrong with that request. Please try again.