-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
[Bug]: tight_layout (version 3.5+) #22673
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
Comments
|
Are you using a window tiling manager? Perhaps it is doing weird things with the window dpi. Tight layout doesn't change window size but it does do multiple draws. I suspect somehow that between the draws your window manager is telling matplotlib that the dpi or dpi ratio has changed. |
No, just regular windows. |
specifically, it sets dpi to 72 whereas the original value is 102. |
|
Another interesting example
Oops. |
I cannot reproduce either of these effects. |
Also, you probably shouldn't use this any more. |
I am using IPython on the command line, not jupyter. What was/is wrong with |
does not work.
|
|
Something is changing your dpi_ratio between the draw triggered by If I use
try
|
Other debugging steps:
|
I think you first analysis is not correct. The resolution of 100 dpi it uses first is the correct one. The 72 dpi is a default value it must be getting elsewhere. From
|
window manager is fvwm2. But there was never an issue until the current mpl version ... |
I follow you steps above and window is not rescaled. No issues. |
it I enable my matplotlibrc file, the problem appears. The file, admittedly is dated ...
matplotlibrc
|
Can you clarify both these statements? I can't tell exactly what you did. Thanks. |
I use a custom ipython config file that loads If I use the above So the bug seems to be triggered by my |
Your matplotlibrc file only uses Qt5Agg, so far as I can see. The rest of it is commented out. If you don't use Qt5Agg, you are presumably using TkAgg, which you said above is also broken, but now you say it is working? Hard for us to track down, but my guess would be you have a broken Qt5 installation. Perhaps upgrade |
I can confirm/reproduce this bug.
As a result of this, macos (intel) |
@MarkWieczorek Can you include your imports and the backend you are using? You must have something set in a configuration file somewhere to not need to execute the imports... On MacOS, master, if I do
... you can complain that the dpi "changed" but that is because I am working on a Retina screen. |
After starting with a base environment with only matplotlib-base installed I note the following:
I note that I am using the default "MacOS" backend. |
If a fresh install behaves properly, my guess would be you have a crossed install somehow. I would just rebuild my environment until it fails, perhaps starting with removing Matplotlib and re-installing it (and any dependencies ti takes with it). Don't remove the old environment, just make a fresh one. |
Note that @greglucas has made some improvements to HiDPI handling on the macOS backend that are only in |
My tests were in a fresh conda env on 3.5.1 using |
For me, it seems that just the presence of the customised Something must be called differently by |
|
What, then causes the actual change? It must be calling some functions that implicitly reset it. I suspect it needs it for calculations, but when obtaining it the function used changes it where it should not? |
I dunno, I am going to take a wild guess that someone monkey patches us in both your tool chains and breaks our renderer dance. It would be helpful to know which package, but unless we can find a Matplotlib-only reproducer, its hard of us to move forward. |
On MacOS (M1), I use homebrew with current python 3.10.2, current IPython (but irrelevant) installed from pip. plot([0,1])
tight_layout() to get the misfit window/dpi patch. And it only depends on the
config line. I do not use conda. But I do use Python 3.10.2. I would think a homebrew install of python on MacOS is fairly standard. |
Why do you think they are irrelevant? Please test without ipython. |
I have installed some 100 packages, I am happy to provide the list but it may not be helpful. |
If you can't start from a clean install and then isolate the package that breaks, we can't move forward. |
They are irrelevant because on my linux install the plain python script does the same thing. As noted in the earlier posts. The MacOS note just above was only for the MacOS to test whether uniquely that one line breaks things or fixes them when commented out. It does. This was to make sure it is not just my install or platform. |
@MarkWieczorek noted it dependent on other packages. I have no experience on that. @jklymak I think you would be able to reproduce the problem if you used a non-conda vanilla Python 3.10.x install. |
THE PROBLEM IS JUPYTER Try this
Note: In trying to isolate this problem, the following packages seem to work ok:
|
export py=3.10.2
export prev=
rm Python
mkdir Python_${py}${prev}
ln -s Python_${py}${prev} Python
mkdir install
cd install
wget https://www.python.org/ftp/python/${py}/Python-${py}${prev}.tar.xz
cd
tar -xvJf install/Python-${py}${prev}.tar.xz
cd Python-${py}${prev}
./configure --prefix=$HOME/Python --enable-optimizations --enable-shared
make install now we have to get ~/Python>pip3 install matplotlib
Collecting matplotlib
Using cached matplotlib-3.5.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.9 MB)
Collecting cycler>=0.10
Using cached cycler-0.11.0-py3-none-any.whl (6.4 kB)
Collecting fonttools>=4.22.0
Using cached fonttools-4.31.2-py3-none-any.whl (899 kB)
Collecting python-dateutil>=2.7
Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting numpy>=1.17
Using cached numpy-1.22.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.8 MB)
Collecting packaging>=20.0
Using cached packaging-21.3-py3-none-any.whl (40 kB)
Collecting pyparsing>=2.2.1
Using cached pyparsing-3.0.7-py3-none-any.whl (98 kB)
Collecting pillow>=6.2.0
Using cached Pillow-9.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.3 MB)
Collecting kiwisolver>=1.0.1
Using cached kiwisolver-1.4.2-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.6 MB)
Collecting six>=1.5
Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: six, pyparsing, python-dateutil, pillow, packaging, numpy, kiwisolver, fonttools, cycler, matplotlib
Successfully installed cycler-0.11.0 fonttools-4.31.2 kiwisolver-1.4.2 matplotlib-3.5.1 numpy-1.22.3 packaging-21.3 pillow-9.0.1 pyparsing-3.0.7 python-dateutil-2.8.2 six-1.16.0
WARNING: You are using pip version 21.2.4; however, version 22.0.4 is available.
You should consider upgrading via the '/home/alex/Python/bin/python3.10 -m pip install --upgrade pip' command. and run ~>python
Python 3.10.2 (main, Mar 31 2022, 01:04:24) [GCC 11.2.1 20211203 (Red Hat 11.2.1-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> plt.plot([0,1])
[<matplotlib.lines.Line2D object at 0x7f4764421720>]
>>> plt.pause(.1)
>>> plt.gcf().dpi
100.0
>>> plt.tight_layout()
>>> plt.pause(.1)
>>> plt.gcf().dpi
100.0 and now with
I get as before
So, even the most minimal ( Even with tkAgg (see plot). No Jupyter needed to break it. It's broken all by itself. I hope this helps. |
@2sn @MarkWieczorek if either of you are able to checkout #22732 and see if that magically fixes things, that would be appreciated. I think we were falling back too soon, and should have been checking fig.canvas.get_renderer before falling back to the cached renderer... I'm still not sure why this would be so fragile to install, but I don't think this proposed change will make things worse. (if you have |
@jklymak How would I use it (how to install from that patch)? I don't have |
https://github.com/cli/cli/blob/trunk/docs/install_linux.md
|
@jklymak Thanks ... seems to uninstall some existing things
and I have In [1]: mpl.__version__
Out[1]: '3.6.0.dev1932+gbda8790ca1' With this, the dpi issue is gone, but the plot is no longer automatically updated in pylab (ion) mode. I need to call
so, one bug fixed, one new ... :-( Maybe the old pyparsing causes issues with IPython. ... No, it is not. Must be something else. It seem I just need to call |
I did some conda updates (lidely including matplotlib) and I unfortunately just had the same problem displaying plots. I had to downgrade to matplotlib=3.4.3=py38hecd8cb5_0 to make it work again. |
@agallenne please open a new issue perhaps after creating a fresh conda environment. Be sure to include all details of your setup and steps to reproduce the problem. Thanks. |
By doing Conda update all? |
I would create a new environment: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html |
I can't install properly matplotlib apparently. I did conda create -n py39 python=3.9 matplotlib, but I now have another error: `Python 3.9.12 (main, Jun 1 2022, 06:36:29)
|
This file is run as part of IPython start up, but is controlled by you:
You need to keep installing things until you have all of the libraries you need or remove stuff from your start up file if you no longer need it. This discussion is probably better had at https://discourse.matplotlib.org rather than the issue tracker. |
yes I know, sorry I pasted everything, it was more the first lines actually, saying "[TerminalIPythonApp] WARNING | Eventloop or matplotlib integration failed. Is matplotlib installed?" Then if I want to plot I can't because if I do "import matplotlib.pyplot as plt" I have this: In [2]: import matplotlib.pyplot as pltImportError Traceback (most recent call last) File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/pyplot.py:2230, in File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/pyplot.py:267, in switch_backend(newbackend) File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/pyplot.py:268, in switch_backend..backend_mod() File ~/opt/anaconda3/envs/py39/lib/python3.9/importlib/init.py:127, in import_module(name, package) File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/backends/backend_qt5agg.py:5, in File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/backends/backend_qtagg.py:9, in File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/backends/qt_compat.py:128, in ImportError: Failed to import any qt binding In [3]: import matplotlib In [4]: import matplotlib.pyplot as pltImportError Traceback (most recent call last) File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/pyplot.py:2230, in File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/pyplot.py:267, in switch_backend(newbackend) File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/pyplot.py:268, in switch_backend..backend_mod() File ~/opt/anaconda3/envs/py39/lib/python3.9/importlib/init.py:127, in import_module(name, package) File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/backends/backend_qt5agg.py:5, in File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/backends/backend_qtagg.py:9, in File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/backends/qt_compat.py:128, in ImportError: Failed to import any qt binding |
Ok I installed PyQt5, let's see steps further |
No, same problem with a fresh env |
This is not an appropriate forum for user help. Please take to discourse.matplotlib.org and we can try and help. Thanks for your understanding! |
Not sure to understand. I am not asking for help about installing matplotlib. I just add problem to have a fresh py39 install (as you asked). But I figured it out and saw that I still have the problem with tight_layout, as the original post. |
I you have this issue, and you have a fresh install, please open a new issue with all the relevant details. The dev team cannot reproduce the original issue, so please be sure to give us the magic formula to make it happen. (Also please have a look at how to format code on GitHub: https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#quoting-code) |
Bug summary
fig.tight_layout is broken in current matplotlib.
Code for reproduction
Actual outcome
below another example from real code that may be instructive
Expected outcome
figure scaled to image size, background deleted.
Additional information
Happens always.
I do not recall this issue in version before 3.5
I have no clue why it is happening. The flaw seems to lay in mpl getting/setting wrong window size when using tight_layout. Maybe gtk unhappy with X11. When I use
mpl.use('Qt5Cairo')
then it just makes the window and fonts smaller. Obviously,tight_layout
should not change physical window size on screen! Similar for Gtk4Cairo.Gtk4Agg
has similar issues toQt5Agg
but does not retain noise in background, just shrinks figure. I could be related to DPI settings. It might get them from a different system call than when opening the window?The only fix I can find is use other plot package, e.g., plotly.
Operating system
Linux 5.16.14-200.fc35.x86_64 #1 SMP PREEMPT Fri Mar 11 20:31:18 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Matplotlib Version
3.5.1
Matplotlib Backend
Qt5Agg
Python version
Python 3.10.2
Jupyter version
N/A
Installation
pip
The text was updated successfully, but these errors were encountered: