-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Uses tight_layout.get_subplotspec_list to check if all axes are compatible w/ tight_layout #1170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
… all the axes are supported by tight_layout
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mdboom wanted to replace "descend" with "inherit" in the warning text.. you could change that in this PR so mdboom doesn't have to do it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But with this change, the warning itself is now incorrect and must be fixed. @leejjoon, should it say something like "This figure includes Axes that do not inherit from SubplotBase and/or were not created using axes_locator; tight_layout will not take such Axes into account, so its 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now we are having to call get_subplotspec_list twice, once to figure out whether to issue a warning, and a second time inside of get_tight_layout_figure. This is not good at all. Either save and reuse the calculation here, or maybe move the warning into get_tight_layout_figure. |
||
pad=pad, h_pad=h_pad, w_pad=w_pad, | ||
rect=rect) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -209,6 +209,33 @@ def get_renderer(fig): | |
return renderer | ||
|
||
|
||
def get_subplotspec_list(axes_list): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I worry about the complexity here. Apart from the difficulty in understanding what is going on, there is the fact that tight_layout is called on every draw(). |
||
""" | ||
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 \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PEP8: please use parentheses, not backslash, for line continuation. |
||
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, []) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style police: No backslash line continuations, please. You may use parentheses here instead.