Manually closing matplotlib plots doesn't release the program - python

When I call pyplot.show(block=True) a window with the correct plot appears. When trying to manually close it, the window closes but the program won't continue to run (like it's still blocked).
For instance, the following program will display the plot window and print Before, but after manually closing the window, the program still seems blocked, and After is not printed.:
plt.plot(x,y)
print("Before")
plt.show(block=True)
print("After")
I work with the latest matplotlib version (2.1.2) using the interactive TkAgg backend on Python 3.5.2.
Update - Additional info: I work with Pycharm 2017.2 on Ubuntu (VM on Windows host)

Solved by unselecting the "Use IPython if available" checkbox in PyCharm's settings.

Related

Spyder IDE with python 3.10 seems freezing when click run button, but it works fine if run a single line beforehand running the entire script

I have truble with last version of Spyder 5.4.0 with last version of Python 3.10.6.
Spyder version: 5.4.0 (conda)
Python version: 3.10.6 64-bit
Qt version: 5.15.2
PyQt5 version: 5.15.7
Operating System: Windows 10
Even if running a script like
print('Hello world')
when I click on the play green button, the IPython console seems freezing on this script, and it does not run for hours.
If I select this line of code and I run current selection or run the current cell it works fine. From this moment, it seems that spyder works fine, until at a certain point when I run it seems again freezing. I have to restart a new console and before running the script I have to run a single line or a single cell.
It seems that I have to 'activate' in someway the Python console in order to Spyder will run the script.
Does anyone have the same issue? How can I solve it?
I have tried to uninstall and reinstall both spyder and python many times, but it is useless.
After many trials, I noticed that there is something strange with IPython console. I noticed that when it hangs after running a code, if I delete all user variables, it worked fine.
Then I tryed to delete all variables before execution, and it work fine.
Therefore I discovered that a solution that worked for me is to go to preferences -> Run -> and untick the option 'Remove all variables before execution'
It is quite annoying because I have to do it manually every time before running, but in this way the spyder does not appear to hang anymore! I hope that the Spyder developers will solve it soon.
-
I automatically solved by typing at the beginning of any script these lines, inspired from the question Code to clear console and variables in Spyder :
try:
from IPython import get_ipython
get_ipython().magic('clear')
get_ipython().magic('reset -f')
import matplotlib.pyplot as plt
plt.close('all')
except:
pass
similar to Matlab in which you normally start your code with
clc
close all
clear all

Attach external system terminal into Spyder

I have been running into a trouble whereby Spyder IPython console is not producing Matplotlib figures as desired. I thought initially that there is something wrong in my code since jupyter notebook gives me the same wrong figures. However, when running the script in Spyder using external terminal the figures are produced as desired. Also, when I run the code in VSC the correct figures are displayed.
So the only option I am left with in Spyder is to use the external terminal to execute the code. However, it is quite a pain every time to run some codes and then manually close the terminal.
I would like to know if there is a way to permanently attach the external terminal inside Spyder? I hate the IPython console when it comes to plotting matplotlib figures!!
(Spyder maintainer here) Sorry but there's no way to dock an external Python terminal inside Spyder.

How to have Spyder act as command line Python interpreter

I am used to code in Vim and run my scripts on the command line. My coworker uses Spyder, with is, I admit, a very good tool.
The problem comes in scripts that use matplotlib, where Spyder (or IPython) interferes with at least pyplot.show(), which is typically not required in Spyder, and pyplot.savefig(), which causes an unwanted pyplot.show() in Spyder.
I have tried so far, without success:
ticking 'Execute in a new dedicated Python interpreter' in run settings dialog box
specifying in Spyder the Python interpreter to use when running scripts
disabling the PYTHONSTARTUP script in Spyder, by pointing to a noop script
Any suggestion?
did you try this:
plot window pane -> Mute inline plotting
Toggle to enable / disable plotting within spyder plot window pane

Cannot manually close matplotlib plot window

I'm running Python v2.7 and matplotlib v1.5.0 on Mac OS X Yosemite. Up to recently, I was able to run my script in the interactive interpreter, show a plot, and then manually kill the window. For example
import numpy as np
x = np.arange(1,10)
y = np.arange(1,10)
plt.plot(x,y)
plt.show()
This used to work fine. The window would close and I'd return to the prompt. However, now when I hit the red X to close the window, the window remains open. My command prompt returns and works fine, but the window is stuck and I see the spinning beach ball over it.
(Note that the cursor doesn't appear in the image, but you can see the red X is greyed out because I've hit close but it is stuck open).
It's not until I run the script a second time that the first window closes, but then I'm stuck with a second open plot with the same problem. I'm a bit confused because this only happened recently. Up till now, the window would close fine.
It's not a huge issue, because Python still runs and I can still make new plots, but I'm curious as to why the window would all of a sudden stick open. Any advice?
UPDATE
I solved the problem by switching the interactive backend. Either Qt4Agg or TkAgg as an interactive backend resolves the issue. But the question remains why the macosx and CocoaAgg backends show this behavior.
For a permanent solution (I'd rather not have to switch backends every time I open ipython) you can modify matplotlibrc.
There are various matplotlibrc files that can be changed (i.e. for just a local directory, or globally, etc.). To find the configuration file that's been loaded for your ipython session, use matplotlib.matplotlib_fname(). Here's what I got:
In [1]: import matplotlib
In [2]: matplotlib.matplotlib_fname()
Out[2]: u'/usr/local/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc'
Opening the rc file and changing the line:
backend : macosx
to read:
backend : Qt4Agg
did the trick for me.
I solved the problem by switching the interactive backend. Either Qt4Agg or TkAgg as an interactive backend resolves the issue.

Interactive matplotlib through Eclipse PyDev

This is a follow up to this interactive matplolib through eclipse thread which is about 2 years old, I was wondering if there has been any progress in the meantime.
I am running the IPython console in a console window in Eclipse PyDev, but I am unable to get the same interactive plotting features with matplotlib as if I were to run IPython in a (Windows) command prompt outside Eclipse PyDev. Here is how the two compare:
A) Running IPython in a shell outside Eclipse PyDev
Run IPython in a Windows command prompt with "ipython --pylab"
Within the IPython console enter "plot([1,2,3])". This will open a figure plot window and the IPython console is ready for further commands (without having to close the figure plot window).
For example, I can enter "xlabel('years')" and this will update my figure plot window.
B) Running IPython in an interactive console within Eclipse PyDev
Enter the following in the IPython interactive console within Eclipse PyDev:
"from pylab import *"
"plot([1,2,3])" --> Figure plot window does not show up.
I have to enter "show()" to open the figure plot window. But now the problem is that as long as I keep the figure plot window open, the IPython console does not accept any new commands.
So I close the figure plot window, enter "xlabel('years')", followed by "show()" again. This will re-open the figure plot window with "years" as my axis label, but the plot itself is empty and does not show the [1,2,3] data anymore.
With this behaviour, A) is clearly superior to B), but I would like to keep working in Eclipse PyDev because I like always having the variables list on my screen (without having to run a command to show all variables like when running IPython form a windows shell). Using Wicked Shell, as suggested in the other thread, does not work (IPython does not work properly in Wicked Shell).
How can I configure IPython in Eclipse PyDev so that it shows the same interactive behavior as if I would run it in a windows command prompt?
You can solve this problem by selecting a GUI for the Interactive Console in PyDev Preference.
Eclipse -> Window -> Preferences -> Pydev -> Interactive Console -> Enable GUI event loop integration.
In my case, I chose PyQt (qt/qt4)
Apologies for the potentially incomplete answer, but hopefully I will be able to shed some light on the problem.
I believe that the one that the OP describes is normal behaviour. In fact, starting from the command line ipython, importing pylab and issuing a plot command produces exactly the blocking behaviour described, so this is not related to pydev or eclipse. The fact is that show in matplotlib is blocking in interactive mode; when you use matplotlib in a ipython session started as "ipython --pylab", you are taking advantage of some "hacks" that the ipython developers did for you around matplotlib, allowing to have both an interactive mode and non blocking calls. However, importing pylab is not enough to apply these "hacks". PyDev does not seems to allow flags to the interpreter call, so one can't directly invoke "ipython --pylab".
Luckily, ipython has a special command "pylab" that applies the hacks and imports pylab even if the interpreter was not started with the pylab flag. So you can just try to type "pylab" inside the console (actually, you can even customize your pydev console so that it is done automatically) and you should get the desired behaviour. However, I must report that while this works fine for me from a ipython session started from the command line, something goes wrong when I try to do the same from inside Eclipse. The command doesn't block, I get the python icon but the matplotlib window doesn't show up. For the records, I am on a Mac running Snow Leopard. I am not able to tell if the same problem happens also in Windows, that the OP seems to be using.
I achieve similar behave in Eclipse PyDev by executing plotting function in another thread:
import threading
from pylab import *
import matplotlib.animation as animation
import time
x = array(range(0,1000))/100
y = sin(x)
def updateData(self):
ax.set_data(x,y)
def MyThread():
global ax
fig, axarr = subplots(1)
ax, = axarr.plot(x,y)
simulation = animation.FuncAnimation(fig, updateData)
show()
t = threading.Thread(target=MyThread)
t.start()
# console stay active, user can interactively control figure
time.sleep(1)
y = sin(2*x)
time.sleep(2)
ax.get_axes().grid()
ax.get_axes().set_xlabel("time")
Tested with toolchain Eclipse 4.3, PyDev 2.7.1, Python 3.2, IPython 0.13
Just use the %matplotlibmagic-command to activate interactive plotting (exactly what you described).
The pylab command imports numpy.* and pylab.*, seriously polluting your global namespace.

Categories

Resources