Ipython : hangs when I try to display a plot in matplotlib.pyplot

I have a conda environment in my recent Intel(R) Celeron(R) Windows 11 desktop, with the following problematic packages:

ipython                   8.15.0           
python                    3.9.19               
...
pyqt                      5.15.10          
pyqt5-sip                 12.13.0          
qt-main                   5.15.2               
qt-webengine              5.15.9               
qtconsole                 5.5.1            
qtpy                      2.4.1            
qtwebkit                  5.212                
...
matplotlib                3.7.2            
matplotlib-base           3.7.2            
matplotlib-inline         0.1.6 
...           

Thus, when I run ipython and import matplotlib, when I try to do a plot, the plot window opens, but hangs. The following sample is enough to hang:

$ ipython

Python 3.9.19 (main, Mar 21 2024, 17:21:27) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.15.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import matplotlib.pyplot as plt

In [2]: %matplotlib
Installed qt5 event loop hook.
Shell is already running a gui event loop for qt5. Call with no arguments to disable the current loop.
Using matplotlib backend: QtAgg

In [3]: plt.plot([1,2,3],'o--')
Out[3]: [<matplotlib.lines.Line2D at 0x12caa994dc0>]

However, the figure (plot) window is the only to hang: says “Not Responding…”. The console doesn´t hang: I can then issue a plt.close().

Note: If I use python instead of ipython, the figure appears after plt.show(), and shows as normal. jupyter notebooks also behave correctly, as far as I tested.

Is there any problem with my configuration? Which corrections to do?

Instead of using

%matplotlib

try

%matplotlib qt5

If you don’t specify a GUI framework then IPython and Matplotlib try to do the correct thing, but it is better to be explicit.

I tried to use both magics, and the result was the same, as you may see from below.

Python 3.9.19 (main, Mar 21 2024, 17:21:27) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.15.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import matplotlib.pyplot as plt

In [2]: %matplotlib qt5
Installed qt5 event loop hook.
Shell is already running a gui event loop for qt5. Call with no arguments to disable the current loop.

In [3]: plt.plot([1,2],[45,6])

hangs showing the plot window after a while: Figure 1 (Not Responding…)

...
In [2]: %matplotlib
Installed qt5 event loop hook.
Shell is already running a gui event loop for qt5. Call with no arguments to disable the current loop.
Using matplotlib backend: QtAgg

In [3]: plt.plot([1,2],[45,6])
Out[3]: [<matplotlib.lines.Line2D at 0x2558e70a700>]

hangs also with the same result.

The only other advice I can give is to always use the %matplotlib magic before importing anything from matplotlib. The import sets the (OS-dependent) default backend and you don’t want this to happen if you are about to set a backend yourself.

Apart from this, the versions of packages that you are using are quite old and I cannot reproduce the problem with recent-ish packages. Perhaps you could try to create a new conda environment with more recent packages? Something like this:

conda create -n temp python=3.10
conda activate temp
pip install matplotlib ipython pyqt5

and then retry.