ehpor / hcipy

A framework for performing optical propagation simulations, meant for high contrast imaging, in Python.
https://hcipy.org
MIT License
90 stars 30 forks source link

Issue with imshow_field #233

Open AyanS13 opened 1 day ago

AyanS13 commented 1 day ago

Hi hcipy team, Thanks a lot for this amazing package.

I was looking at the tutorials, specifically "Telescope Pupils and Grids". When I implement the part of code that zooms into the spider to show pixelation, the xlim and ylim are behaving in a rather odd way. Below I have attached this. I have cross-checked if the issue is with Python but found that it works well with normal plt.imshow image

NuggetOfficial commented 1 day ago

Hi Ayan,

Which OS, Python version and HCIpy version are you using? Also could you please provide a MRE?

I ran the following code on my system (NixOS, linux x86_64) using a freshly installed HCIpy 0.6.0 on Python 3.11.9:

from hcipy import *
from matplotlib import pyplot as plt

vlt_aperture = make_vlt_aperture()
grid = make_pupil_grid(128, diameter=10)

aperture = vlt_aperture(grid)

imshow_field(aperture, cmap='gray', interpolation='nearest')
plt.plot(grid.x, grid.y, '.')
plt.xlabel('x [m]')
plt.ylabel('y [m]')
plt.xlim(2.5, 4.5)
plt.ylim(-0.85, 0.85)
plt.show()

The output is as presented in the tutorial Telescope pupils and Grids.

image

I suspect it will either be something with your versioning or mabye you changed something in the matplotlib rc_params and didnt change it back?

Hope it helps, let me know if it doesn't Cheers, Tom

AyanS13 commented 1 day ago

Hi Tom, Thanks a lot for responding. I am using the latest 0.6.0 version of hcipy and Python 3.9.6 on MacOS. I also checked if I have altered matplotlib rc_params somehow. I did not find any. Still, I reverted them back to the defaults. But the output remains the same with that large amount of white space.

P.S. I copy pasted the code snippet you shared in VS Code.

NuggetOfficial commented 21 hours ago

Can you also give me your matplotlib version? Ill try to run your versions on my machine to see if I can recreate the behaviour.

syhaffert commented 20 hours ago

I use Windows 10, python 3.10.14, matplotlib 3.8.4 and the latest hcipy master branch version. On my machine everything works as it should. Are you running this in a Jupyter notebook? I know that the plotting inside the notebook cells does not work well with the xlim and ylim settings. Do you have the same problem if you run the code either in an iPython terminal or through a python script?

syhaffert commented 20 hours ago

I can reproduce the error in a Jupyter notebook on my side. This does not happen if I run the code in an iPython terminal or through a python script.

AyanS13 commented 20 hours ago

Can you also give me your matplotlib version? Ill try to run your versions on my machine to see if I can recreate the behaviour.

I use matplotlib 3.9.2

AyanS13 commented 20 hours ago

I can reproduce the error in a Jupyter notebook on my side. This does not happen if I run the code in an iPython terminal or through a python script.

I see, I tried to run it as a .py file rather than .ipynb in vs code itself, but the problem persisted. I will try with Python terminal and see. Do you have any guess, why xlim and ylim creates problem in jupyter notebook for imshow_field ?

syhaffert commented 19 hours ago

I am not sure. I think what happens with the particular plotting code is that it creates the full figure in full size, and then only plots the area corresponding to the limits that you give to xlim and ylim. And then it add the axes around it. If you move the limits you can actually also move the figure around. It's a bit of a weird bug.

ehpor commented 5 hours ago

Yes, I'm aware of this bug. It has to do with Jupyter notebooks switching the bounding box of figures to "tight" by default. You can revert to the original matplotlib behavior by putting the following as the first cell of your notebook.

%matplotlib inline
%config InlineBackend.print_figure_kwargs = {'bbox_inches': None}