Closed raphaelchinchilla closed 7 months ago
As long as I include plt.ion() before running the code, it does work, but I have to select it each time I run the code. Otherwise it continues to get stuck after each plot...
Including "%matplotlib inline" only makes it such that no plot appears.
Consider the MWE:
I run the file with the %matplotlib magic command in ipython.StartupCommands
import numpy as np
from matplotlib import pyplot as plt
print(plt.isinteractive())
plt.plot(np.arange(10))
plt.show()
print(1 + 1)
If I run this file by pressing F5 (which is the command "IPython: run fil with command line arguments")
I receive that plt.isinteractive() is False. In MacOS the file get stuck in plt.show(), while in Linux it continues to the end of the file.
If I run the code with F9 (IPython: Run Code Selection or Line at each cursor),
if I use the "%run -i" option, the return is the same as above. However, if I do it with the "%load" option, the return of plt.isinteractive()
is True and the cursor continues until the end of the selection and does not stop at plt.show()
Finally, it is always the case that, when I do plt.isinteractive()
in the IPython terminal, I get "True"
Here is something that may help. This is what I got on Window:
pip install
numpy, matplotlib, ipython, PyQt5
to virtual environment"%matplotlib qt5"
to configuration:
"ipython.StartupCommands": [
"%matplotlib qt5",
"%load_ext autoreload",
"%autoreload 2"
]
plt.plot()
called:$ ipython --no-autoindent --InteractiveShellApp.exec_lines="%matplotlib qt5" --InteractiveShellApp.exec_lines="%load_ext autoreload" --InteractiveShellApp.exec_lines="%autoreload 2"
Python 3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.21.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import numpy as np
In [2]: import matplotlib.pyplot as plt
In [3]: x = np.linspace(0, 2 * np.pi, 128)
In [4]: y = np.sin(x)
In [5]: plt.plot(x, y)
Out[5]: [<matplotlib.lines.Line2D at 0x1a744c06d10>]
In [6]:
With the same configuration I getthe behavior I mentioned above.
What is weird for me is the difference in the output of plt.isinteractive(). It kind of suggests that the expected behavior is not taking place
What is weird for me is the difference in the output of plt.isinteractive(). It kind of suggests that the expected behavior is not taking place
Agree. matplotlib
behaviors with IPython terminal need a bit of trial and error lately. My guess is that the behaviors are dependent on the what the backend renderer can do. I wish they follows the zen of Python "Preferably one way to do thing" and support one renderer like Qt
.
I am having the same problem as @raphaelchinchilla. Would be nice if we can figure out a workaround. :pray:
Another solution is to do plt.show(block=False)
. It is a bit annoying but works...
I don't know if I am doing something wrong, but when I plot something, the ipython gets stuck and will not run the next line until I close the plot.
This happens both on a linux and a macos machines.
Is there a way to change this behavior?