Matplotlib is hanging when using python3 - python

Matplotlib hangs now that I've upgraded to Sierra.
The behavior is different depending on how I run it.
In a file (running with %run <filename> inside of ipython or python <file name> from the terminal):
The terminal will hang on import (import matplotlib.pyplot as plt) and all I get is the little rocket icon bouncing in my dock.
Directly from ipython:
I can import it and show a plot, but it will hang once I try to close the figure window. Doing this from inside of the basic python shell does not have this problem. So it seems running in interactive mode also causes hanging.
Has anyone experienced something similar?
python version: 3.5.2
matplotlib version: 1.5.1
backend: MacOSX
OSX: 10.12.2
Switching to a TkAgg backend will display the figure window, but it will hang on plotting (in interactive mode).
Note
Setting up a virtual environment to run python 2.7 with a TkAgg backend works as expected

Seems to be a caching issue:
running:
cd ~/.matplotlib
fc-list
fixed the problem (same issue discussed here: import matplotlib.pyplot hangs but the symptoms were a little different)

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

Configuring macOS PyCharm for X11 Forwarding

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
x = np.linspace(1,1000)
plt.plot(np.linspace(1, 1000))
print("Works")
plt.show()
I am trying to run the simple code above within PyCharm on a remote machine, but showing the plots on my local machine (mac). The plot does not appear. I do have xQuartz X11 Server running.
Pycharm runs the remote interpreter fine.
If I run it from macOS terminal, using
ssh -X pier#129.168.0.181
python test.py
plt.show() works.
I reckon that the missing piece is the -X which enables the X11 to be forwarded to my local machine.
Where do I include this with PyCharm's command to ssh? I'm spending too much time trying to figure this out...
Note: I'm also not able to use PyCharm's Python Console to do plotting. No errors are shown but the plot is not forwarded to my local machine.
Ok, I found I needed to do two things to get it working well enough for me :
(1) Set DISPLAY = localhost:10.0 in the Environment Variables under Build, Execution, Deployment -> Python Console
(2) Right after
import matplotlib
matplotlib.use('Qt5Agg')
With this, I can use the remote interpreter as if it were local.
Building of #Ippiers answer, on windows this works via:
install xming (and have it running)
run a putty session with X11 forwarding enabled
env on the putty session and check the DISPLAY variable, it will probably be localhost:10.0
Set this display variable in pycharms run configuration DISPLAY=localhost:10.0
matplotlib.use('TkAgg') as Qt5 gave me errors

Plot cannot opened in python laucher in Mac

I'm following the Pyplot tutorial and I wanted to create a plot in Python IDLE 2.7.10 Shell. During the code execution, the python launcher has been called. However, the launcher just 'jump' and 'jump' again, but it connot open to show the plot.
Code
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
Environment
Mac OSX version : 10.10.5
Python version : 2.7.10
If you want to display it, add plt.show()

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.

Emacs Python-inferior shell not showing prompt after matplotlib show() command

So I've been experimenting with numpy and matplotlib and have stumbled across some bug when running python from the emacs inferior shell.
When I send the py file to the shell interpreter I can run commands after the code executed. The command prompt ">>>" appears fine. However, after I invoke a matplotlib show command on a plot the shell just hangs with the command prompt not showing.
>>> plt.plot(x,u_k[1,:]);
[<matplotlib.lines.Line2D object at 0x0000000004A9A358>]
>>> plt.show();
I am running the traditional C-python implementation. under emacs 23.3 with Fabian Gallina's Python python.el v. 0.23.1 on Win7.
A similar question has been raised here under the i-python platform: running matplotlib or enthought.mayavi.mlab from a py-shell inside emacs on windows
UPDATE: I have duplicated the problem on a fresh instalation of Win 7 x64 with the typical python 2.7.2 binaries available from the python website and with numpy 1.6.1 and matplotlib 1.1.0 on emacs 23.3 and 23.4 for Windows.
There must be a bug somewhere in the emacs shell.
I think there are two ways to do it.
Use ipython. Then you can use -pylab option.
I don't use Fabian Gallina's python.el, but I guess you will need something like this:
(setq python-shell-interpreter-args "-pylab")
Please read the documentation of python.el.
You can manually activate interactive mode by ion
>>> from matplotlib import pyplot as plt
>>> plt.ion()
>>> plt.plot([1,2,3])
[<matplotlib.lines.Line2D object at 0x20711d0>]
>>>
You can use different back-end:
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
Other GUI backends:
TkAgg
WX
QTAgg
QT4Agg
If you are using Elpy run your code using C-u C-c C-c
I think that this might have something to do with the behavior of the show function:
matplotlib.pyplot.show(*args, **kw)
When running in ipython with its pylab mode, display all figures and
return to the ipython prompt.
In non-interactive mode, display all figures and block until the
figures have been closed; in interactive mode it has no effect unless
figures were created prior to a change from non-interactive to
interactive mode (not recommended). In that case it displays the
figures but does not block.
A single experimental keyword argument, block, may be set to True or
False to override the blocking behavior described above.
I think your running into the blocking behavior mentioned above which would result in the shell hanging. Perhaps try running the function as: plt.show(block = False) and see if it produces the output you expect. If this is still giving you trouble let me know and I will try and reproduce your setup locally.
I think I have found an even simpler way to hang the inferior shell but only when pdb is invoked. Start pdb by supplying 'python' as the program to run.
Try this code:
print "> {<console>(1)<module>() }"
Well after a tremendous amount of time and posting the bug on the matplotlib project page and the python-mode page I found out that supplying the arguments console --matplotlib in ipython.bat will do the trick with matplotlib 1.3.1 and ipython 1.2.0
This is what I have in my iphython.bat
#python.exe -i D:\devel\Python27\Scripts\ipython-script.py console --matplotlib %*

Categories

Resources