matplotlib / ipympl

Matplotlib Jupyter Integration
https://matplotlib.org/ipympl/
BSD 3-Clause "New" or "Revised" License
1.59k stars 225 forks source link

Large top/bottom padding for imshow images #553

Closed marscher closed 6 months ago

marscher commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 6 months ago

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 commented 6 months ago

There may be some confusion about terms here:

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 commented 6 months ago

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.