Skip to content

Shorten tight_layout code. #17205

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 1 commit into from
Apr 23, 2020
Merged

Shorten tight_layout code. #17205

merged 1 commit into from
Apr 23, 2020

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Apr 21, 2020

  • Turn hspaces/vspaces into 2D arrays of lists, rather than flat lists
    that need to be repeatedly accessed with flattened indices.
  • Merge the num2 is None case by treating it as a slice of length 1,
    which then allows just inlining get{left,right,top,bottom}.
  • Minor refactorings.

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@anntzer anntzer added this to the v3.3.0 milestone Apr 21, 2020
vspaces = [[] for _ in range((rows + 1) * cols)]
hspaces = [[] for _ in range(rows * (cols + 1))]

union = Bbox.union
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this an optimization?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was just a typing optimization :); saving an attribute access is unlikely to matter here especially given that we're about to rerender the figure after tightlayouting it...

hspaces = [[] for _ in range(rows * (cols + 1))]

union = Bbox.union
vspaces = np.full((rows + 1, cols), None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The third dimension is len(zip(subplot_list, ax_bbox_list, num1num2_list)) minus the subplots with completely invisible Axes. But since we take the sum of the lists in vspaces/hspaces, then we can just fill the empty locations with 0, and make these truly 3D?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we can just directly accumulate the values (it's not clear to me why you'd want to add paddings when multiple axes share the same subplotspec (which the comment says is possible in axes_grid?? well let's not get into that for now), but let's not change that here).
The last dim is not actyally len(zip(...)), but the max number of axes that share the same subplotspec.

@anntzer anntzer force-pushed the tl branch 2 times, most recently from ac235b2 to dc63fdf Compare April 22, 2020 08:43
margin_left = max([sum(s) for s in hspaces[::cols + 1]] + [0])
margin_left += pad_inches / fig_width_inch

margin_left = (max(hspaces[:, 0].max(), 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What did the sum do? And why don‘t we need it anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reasons I don't understand, this used to sum the margins of all subplots at a given position -- I'd have thought it's the max that matters, but let's not touch that here. (Actually I don't even know how you can realistically have multiple subplots at the same position, but there's a comment specifically mentioning this happens in axes_grid, so sure...)
Previously the code accumulated all margins in lists, and summed them at the end (hspaces[...].append(...)). Now I just make a 2D array of floats and directly sum the margins there (hspaces[...] += ...).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, looking at it again, I see at least one reason for the sum: you need to add the right margin (between the spines and the label edges) of the axes on the left and the left margin of the axes on the right.

@jklymak
Copy link
Member

jklymak commented Apr 22, 2020

Artifacts are missing, so I can't check the tight layout tutorial.

- Turn hspaces/vspaces into 2D arrays, rather than flat lists that
  need to be repeatedly accessed with flattened indices, and directly
  accumulate widths/heights in there.
- Merge the num2 is None case by treating it as a slice of length 1,
  which then allows just inlining _get_{left,right,top,bottom}.
- Minor refactorings.
@anntzer
Copy link
Contributor Author

anntzer commented Apr 22, 2020

sure, triggered a rebuild.

Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jklymak jklymak merged commit cc5ddce into matplotlib:master Apr 23, 2020
@anntzer anntzer deleted the tl branch April 23, 2020 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants