Skip to content
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

support bbox='tight' without needing to save the figure #7226

Closed
choldgraf opened this issue Oct 6, 2016 · 9 comments
Closed

support bbox='tight' without needing to save the figure #7226

choldgraf opened this issue Oct 6, 2016 · 9 comments

Comments

@choldgraf
Copy link
Contributor

This is a (hopefully quick?) feature request.

Right now I find it quite useful to use the bbox_inches='tight' parameter when saving figures to disk. However, as it becomes more common to plot figures interactively (e.g. with %matplotlib notebook), they often are cut off on the sides (e.g. jupyter creates a window for the figure based on its bbox, but some of the figure is now outside the bbox because you've created a legend or something).

I think this would be solved with the aforementioned parameter and whatever under-the-hood magic it performs, but to my knowledge it is only exposed to users when you save the figure. Would it be non-trivial to allow users to call this function without saving the figure to disk?

@Kojoley
Copy link
Member

Kojoley commented Oct 7, 2016

@WeatherGod
Copy link
Member

tight_layout and bbox_inches='tight' are not synonymous. The first one will
adjust the subplots and text to fit the figure size, while the latter will
adjust the figure size to fit the subplots and other artist elements
(provided that bbox_inches='tight' is told about the extra artists...).

I think it would be feasible to create a function that would compute what
the bbox should be and emit a resize event. The function that does the bbox
computation can also be used by savefig().

On Fri, Oct 7, 2016 at 7:18 AM, Nikita Kniazev notifications@github.com
wrote:

http://matplotlib.org/users/tight_layout_guide.html


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#7226 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AARy-DlvpJ6TACLoyKC2toCQXQMEf8n7ks5qxiqQgaJpZM4KQT0b
.

@choldgraf
Copy link
Contributor Author

+1 to what @WeatherGod said, I've tried tight_layout but it doesn't work for the problem of resizing the bbox.

The function that does the bbox computation can also be used by savefig()

That's what I'm imagining as well. It's being done somewhere already, but when I looked through the codebase it didn't seem to be coming from a specific function. I was concerned that perhaps it was handled differently depending on the backend or something, which is why I opened the issue.

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Oct 7, 2016
@tacaswell
Copy link
Member

I think effort would be better spent implementing a rule-based layout engine.

By 'shrink-wrapping' the figure you lose almost all control of how big the final image will be, which in turn is problematic for embedding the saved figure in any sort of document (if you do not rescale them, then all of your figures are different sizes, if you do rescale them then all of your fonts are different sizes).

@tacaswell
Copy link
Member

That said, factoring out the tight_bbox code would be a reasonable thing to do. I suspect it will have to rely on having a cached renderer as we need to draw some things (like text) to know how big they will be when we draw them.

@choldgraf
Copy link
Contributor Author

choldgraf commented Oct 7, 2016

FWIW it looks like the relevant code is here. Could it be added as another method to FigureCanvasBase? It seems like the code there is basically taking a few plotting parameters like dpi, and doing some stuff w/ self, creating a temporary file in the process then reading out the file's parameters.

So something like FigureCanvasBase.tight_bbox, and then a function somewhere like plt.tight_bbox?

or maybe just the method since this is probably not a super-common use case

@choldgraf
Copy link
Contributor Author

See PR for one possible what that (I think) would accomplish the same thing under the hood while still making it possible for people to call the tight_bbox method separately if they wanted. Very WIP, just a proof of concept.

@QuLogic QuLogic modified the milestones: unassigned, 2.1 (next point release) Jul 15, 2017
@ArchangeGabriel
Copy link
Contributor

Why was the issue closed? It looks similar to my problem, that is mostly https://stackoverflow.com/questions/16118291/matplotlib-make-final-figure-dimensions-match-figsize-with-savefig-and-bbox-e

Especially this:

Is there a way to specify the exact figure size after using bbox_inches='tight'? Basically, the extent of any whitespace around the figure ought to be decided in the document I'm inserting it into and not encoded into the figure pdf itself.

@ArchangeGabriel
Copy link
Contributor

Actually after reading more in depth this might be a bit different issue, but still highly related. I’ll open a new one for my specific issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants