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

Large top/bottom padding for imshow images #553

Closed
marscher opened this issue Apr 25, 2024 · 5 comments
Closed

Large top/bottom padding for imshow images #553

marscher opened this issue Apr 25, 2024 · 5 comments

Comments

@marscher
Copy link

Describe the issue

When plotting an image using pylab.imshow(), the top and bottom padding are very large.
I'm using the Agg backend with ipympls frontend. I expected the image to be shown at the top. It seems centered in the output div.

The code to generate the plot is this snippet:

import matplotlib.pylab as plt
import numpy as np
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()

grafik

Versions

Jupyer lab environment:

ipympl version: 0.9.4
Selected Jupyter core packages...
IPython          : 8.18.1
ipykernel        : 6.29.3
ipywidgets       : 8.1.2
jupyter_client   : 8.6.1
jupyter_core     : 5.7.2
jupyter_server   : 2.13.0
jupyterlab       : 4.1.6
nbclient         : 0.10.0
nbconvert        : 7.16.3
nbformat         : 5.10.4
notebook         : 7.1.2
qtconsole        : 5.5.1
traitlets        : 5.14.2
JupyterLab v4.1.6
C:\software\Miniconda3\envs\jlab\share\jupyter\labextensions
        jupyter-matplotlib v0.11.4 enabled ok
        jupyterlab_pygments v0.3.0 enabled ok (python, jupyterlab_pygments)
        k3d v2.16.1 enabled ok (python, k3d)
        @jupyter-notebook/lab-extension v7.1.2 enabled ok
        @jupyter-widgets/jupyterlab-manager v5.0.10 enabled ok (python, jupyterlab_widgets)

**Client (kernel) environment**:

 3.11.8 | packaged by conda-forge | (main, Feb 16 2024, 20:40:50) [MSC v.1937 64 bit (AMD64)]
ipympl version: 0.9.4
Selected Jupyter core packages...
IPython          : 8.22.2
ipykernel        : 6.29.3
ipywidgets       : 8.1.2
jupyter_client   : 8.6.1
jupyter_core     : 5.7.2
jupyter_server   : 2.13.0
jupyterlab       : 4.1.6
nbclient         : 0.10.0
nbconvert        : 7.16.3
nbformat         : 5.10.4
notebook         : 7.1.2
qtconsole        : 5.5.1
traitlets        : 5.14.2
JupyterLab v4.1.6
C:\software\Miniconda3\envs\cv\share\jupyter\labextensions
        jupyter-matplotlib v0.11.4 enabled ok
        jupyterlab_pygments v0.3.0 enabled ok (python, jupyterlab_pygments)
        k3d v2.16.1 enabled ok (python, k3d)
        @jupyter-notebook/lab-extension v7.1.2 enabled ok
        @jupyter-widgets/jupyterlab-manager v5.0.10 enabled ok (python, jupyterlab_widgets)


@marscher
Copy link
Author

The figsize argument has no effect on the output. Please note that you can manually resize the canvas using the gray triangle located in the lower-left corner. After resizing, the image will be aligned to the top.

grafik

@rcomer
Copy link
Member

rcomer commented Apr 25, 2024

Centring the image is standard Matplotlib behaviour. If you want the image at the top of its axes you can set the anchor to 'N' for north:

plt.gca().set_anchor('N')

@marscher
Copy link
Author

Thanks, centering helps a bit in the sense that the viable information is aligned on top. But the the canvas is still far too large for actual figure. Now the free space is just all in the south :)

@rcomer
Copy link
Member

rcomer commented Apr 29, 2024

There may be some confusion about terms here:

  • The figure is the whole thing, and you have requested a figure that is twice as tall (20 inches) than it is wide (10 inches)
  • Within the figure sits the axes, which is where the data is plotted. You are plotting an image that is nearly twice as wide as it is tall. By default, imshow uses a fixed aspect ratio to keep pixels square.

If you want to keep the figure this size but stretch the image so it fits better, try aspect="auto" in the call to imshow. If you want the image the same shape but want the figure to fit around it, choose a different figsize.

@marscher
Copy link
Author

Right, I made a mistake when setting the figsize in my mimimum reproducing example. Actually I compute the figsize using data shape and the current DPI. This leads to the desired output. Thanks for your support.

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

2 participants