8000 Uses tight_layout.get_subplotspec_list to check if all axes are compatible w/ tight_layout by leejjoon · Pull Request #1170 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 2 commits into from
Sep 1, 2012
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
implement tight_layout.get_subplotspec_list and uses this to check if…
… all the axes are supported by tight_layout
  • Loading branch information
leejjoon committed Aug 29, 2012
commit 77e245a5b9dc6da86a6529f2e4db59f9d23a60da
8 changes: 4 additions & 4 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Copy link
Member

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.

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.")
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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,
Copy link
Member

Choose a reason for hiding this comment

The 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)

Expand Down
47 changes: 33 additions & 14 deletions lib/matplotlib/tight_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,33 @@ def get_renderer(fig):
return renderer


def get_subplotspec_list(axes_list):
Copy link
Member

Choose a reason for hiding this comment

The 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 \
Copy link
Member

Choose a reason for hiding this comment

The 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):
"""
Expand Down Expand Up @@ -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, [])
Expand Down
0