Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Implement bbox_inches="tight" as a post-processing step. #19923

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

Closed
wants to merge 1 commit into from

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Apr 9, 2021

The current implementation of bbox_inches="tight" is to no-output-draw
the figure, compute a tight bbox, and then shift all artists to their
"new" position in tight-bbox-coordinates, draw, and shift back -- which
causes all kinds of grief (especially for @jklymak :-)); see e.g. #19832
for a recent case.

Instead, we can perform the real draw the first time, and then crop the
resulting image (either actually crop it for raster backends, or just
adjust the viewport for vector ones).

This PR is a proof-of-concept for such an approach (tests fail, that's
expected). Currently it writes the first file to the filesystem and
then edits it; likely the first file should be just kept in memory
instead (after all the final output could also just be an in-memory
buffer). This would also avoid having to dupe the metadata handling
(for agg).

Perhaps the method should be on the renderer instead (the exact API is
needs to be discussed). Extra points if the API is designed in such a
way that mplcairo can directly call into it.

Currently not implemented for postscript because bbox_inches="tight"
doesn't work there anyways (although we should fix it for postscript)
(#16657 is related). Not sure about pgf output, but that's probably
workable...

Final version should keep some backcompat layer, not present here to
improve readability.


Note for myself: https://tex.stackexchange.com/questions/307797/how-to-measure-the-height-of-a-tikz-picture
likely provides a way to implement this for pgf.

See #20250 for a possible alternate approach.

PR Summary

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

The current implementation of bbox_inches="tight" is to no-output-draw
the figure, compute a tight bbox, and then shift all artists to their
"new" position in tight-bbox-coordinates, draw, and shift back -- which
causes all kinds of grief.

Instead, we can perform the real draw the first time, and then crop the
resulting image (either actually crop it for raster backends, or just
adjust the viewport for vector ones).

This PR is a proof-of-concept for such an approach (tests fail, that's
expected).  Currently it writes the first file to the filesystem and
then edits it; likely the first file should be just kept in memory
instead (after all the final output could also just be an im-memory
buffer).  This would also avoid having to dupe the metadata handling
(for agg).

Perhaps the method should be on the renderer instead (the exact API is
needs to be discussed).  Extra points if the API is designed in such a
way that mplcairo can directly call into it.

Currently not implemented for postscript because bbox_inches="tight"
doesn't work there anyways (although we should fix it for postscript).
Not sure about pgf output, but that's probably workable...

Final version should keep some backcompat layer, not present here to
improve readability.
Copy link

Since this Pull Request has not been updated in 60 days, it has been marked "inactive." This does not mean that it will be closed, though it may be moved to a "Draft" state. This helps maintainers prioritize their reviewing efforts. You can pick the PR back up anytime - please ping us if you need a review or guidance to move the PR forward! If you do not plan on continuing the work, please let us know so that we can either find someone to take the PR over, or close it.

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Dec 11, 2023
@anntzer
Copy link
Contributor Author

anntzer commented Dec 11, 2023

I think #20250 is more viable because it won't need extensive per-format cropping tricks. In any case we can close this for now, the idea is still around.

@anntzer anntzer closed this Dec 11, 2023
@anntzer anntzer deleted the posttight branch December 11, 2023 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: inactive Marked by the “Stale” Github Action status: needs rebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant