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

Unnecessarily large padding #72

Closed
thomasaarholt opened this issue Nov 23, 2018 · 15 comments
Closed

Unnecessarily large padding #72

thomasaarholt opened this issue Nov 23, 2018 · 15 comments

Comments

@thomasaarholt
Copy link
Contributor

%matplotlib widget introduces a lot of padding around the plotted image, emphasised when compared to %matplotlib inline
I have tried various matplotlib calls, but can't figure out a way to reduce it. In any case, I think it should be default be smaller than it is?
I'm sure I'm not the only one who's had to scroll a bit too much to get past very large images.

Two additional questions:

  • what controls the increased size of the plot with widget?
  • Can we reduce or change the size/shape of the "Figure 1" toolbar? I understand the need to close the interactive plot, but do we need the full toolbar?

image
image

Test code:

%matplotlib widget
import numpy as np
import matplotlib.pyplot as plt
plt.figure()
plt.imshow(np.random.random((32,32)));
@thomasaarholt
Copy link
Contributor Author

The padding seems particularly unnecessary with plt.subplots():
image

@hadim
Copy link

hadim commented May 14, 2019

I agree with this!

@coolsurf
Copy link

Any suggestions or workaround? Thanks.

@GarrisonD
Copy link

I agree with this too! Any updates? @martinRenou

@GarrisonD
Copy link

@martinRenou As far as I understand after reading the source code, ipympl has nothing to do with this issue since it's just fetching the image from the backend and rendering on the canvas without any space being added. So the problem is on the backend side, right?

@martinRenou
Copy link
Member

martinRenou commented May 13, 2020

As far as I understand after reading the source code, ipympl has nothing to do with this issue since it's just fetching the image from the backend and rendering on the canvas without any space being added. So the problem is on the backend side, right?

Yeah you're right. Although the header/footer and the toolbox are taking some space. But they can still be hidden with the header_visible, footer_visible flags so there should not be any problem.

Actually this issue starts to be a bit old, and lots of changes have happened since those screenshots were taken. Shouldn't we close this issue? Or did you find that ipympl was still taking too much space with recent releases @GarrisonD ?

@GarrisonD
Copy link

GarrisonD commented May 13, 2020

I am using the latest version (0.5.6) and can confirm that there is still too much space around. For some cases plt.tight_layout() helps but passing bbox_inches='tight' to plt.savefig is much more robust but I can't figure out how to use that with ipympl 😢

With inline:

Screenshot 2020-05-13 at 16 30 52

With widget:

Screenshot 2020-05-13 at 16 31 05

With widget and tight layout:

Screenshot 2020-05-13 at 16 32 56

@GarrisonD
Copy link

@martinRenou matplotlib.backend_bases.FigureCanvasBase.print_figure accepts bbox_inches option and is used in InlineBackend (see source).
Is it possible for nbAgg backend to work this way?

@martinRenou
Copy link
Member

Maybe @tacaswell you would have some comments? I am not familiar with the Matplotlib API.

@ianhi
Copy link
Collaborator

ianhi commented Jan 6, 2021

On ipympl 0.6.2 and matplotlib '3.3.2.post2048+g448100ae0' I believe that ipympl is doing the correct thing - giving the same result as other interactive backends. Here is a comparison showing that ipympl produces the same padding as the TkAgg backend.
image

The inline backend is essentially calling tight_layout because the default argument to print_figure is bbox_inches='tight'
https://github.com/ipython/ipython/blob/3587f5bb6c8570e7bbb06cf5f7e3bc9b9467355a/IPython/core/pylabtools.py#L97

To get permanent results consider this quote from the tight_layout tutorial

Note that matplotlib.pyplot.tight_layout() will only adjust the subplot params when it is called. In order to perform this adjustment each time the figure is redrawn, you can call fig.set_tight_layout(True), or, equivalently, set rcParams["figure.autolayout"] (default: False) to True.

@ianhi
Copy link
Collaborator

ianhi commented Jan 6, 2021

that there is still too much space around.

I think the place to take this up would be on the matplotlib repo as this isn't something ipympl controls.

@martinRenou
Copy link
Member

This should be more flexible now, as you can optionally hide the header/footer/toolbar. At some point we could improve the toolbar space by fixing #299

@marscher
Copy link

Could it be, that we have some kind of regression here?
grafik
The top and bottom padding are very large. The screenshot only shows the top padding, but the lower one is of the same size as the top one.

The code to generate the plot is this snippet:

plt.figure(figsize=(10, 20))
plt.title("Epipolar lines in rectified images.")
img_epilines2 = np.random.random((1280, 2048, 3))
%matplotlib widget
plt.imshow(img_epilines2)
plt.tight_layout()
plt.show()

matplotlib 3.8.4
ipympl 0.9.4

Or dou you think it'd be better to raise a new issue somewhere @martinRenou?

@martinRenou
Copy link
Member

Yes, probably a new issue would be better 👍🏽

@marscher
Copy link

xref #553

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

7 participants