hoangKnLai / vscode-ipython

VSCode Extension integrating Editor with IPython console.
MIT License
18 stars 5 forks source link

More smooth plotting #50

Closed raphaelchinchilla closed 7 months ago

raphaelchinchilla commented 7 months ago

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?

hoangKnLai commented 7 months ago

Hi, this is due to matplotlib changes to their default behavior. See their doc on show(). More on this with turning interactive mode on, ion().

There is probably a way to set IPython startup to have Jupyter like default behaviors. Maybe adding %matplotlib inline to ipython.StartupCommands?

raphaelchinchilla commented 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.

raphaelchinchilla commented 7 months ago

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"

hoangKnLai commented 7 months ago

Here is something that may help. This is what I got on Window:

pip freeze ```text $ pip freeze asttokens==2.4.1 colorama==0.4.6 contourpy==1.2.0 cycler==0.12.1 decorator==5.1.1 executing==2.0.1 fonttools==4.48.1 ipython==8.21.0 jedi==0.19.1 kiwisolver==1.4.5 matplotlib==3.8.2 matplotlib-inline==0.1.6 numpy==1.26.4 packaging==23.2 parso==0.8.3 pillow==10.2.0 prompt-toolkit==3.0.43 pure-eval==0.2.2 Pygments==2.17.2 pyparsing==3.1.1 PyQt5==5.15.10 PyQt5-Qt5==5.15.2 PyQt5-sip==12.13.0 python-dateutil==2.8.2 six==1.16.0 stack-data==0.6.3 traitlets==5.14.1 wcwidth==0.2.13 ```

"ipython.StartupCommands": [
        "%matplotlib qt5",
        "%load_ext autoreload",
        "%autoreload 2"
]
$ 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]: 

image

raphaelchinchilla commented 7 months ago

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

hoangKnLai commented 7 months ago

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.

linuswalter commented 6 months ago

I am having the same problem as @raphaelchinchilla. Would be nice if we can figure out a workaround. :pray:

raphaelchinchilla commented 6 months ago

Another solution is to do plt.show(block=False). It is a bit annoying but works...