Attempting to get some very basic stuff working on my Windows 7 machine. If I run the below from the matplotlib documentation:
Script 1:
import matplotlib
matplotlib.use('qt4agg') #Just using this to test backends
import matplotlib.pyplot as plt
plt.ion()
plt.plot([1.6, 2.7])
Then I should get a line plotted and be able to change things via the terminal prompt. What actually happens is I get a blank figure and a spinning wheel, 'python.exe is not responding' and needs to be forced closed.
If I put:
Script 2:
import matplotlib
matplotlib.use('qt4agg') #Just using this to test backends
import matplotlib.pyplot as plt
plt.ioff()
plt.plot([1.6, 2.7])
plt.show()
Then I get the desired result but my terminal prompt is blocked from use.
The other thing is that the above are both run using the QT4Agg backend which is not what I want to use. Testing a couple of other backends (GTK3agg and Tkagg) I get the following results:
GTK3Agg
ION - Same error as when running with Qt4Agg, I get a blank figure and a spinning wheel, 'python.exe is not responding' and needs to be forced closed.
IOFF - Blank figure appears but no spinning wheel and I get 'python.exe has stopped working' immediately after the figure appears
Tkagg
ION - Blank figure appears but no spinning wheel and I get 'python.exe has stopped working' immediately after the figure appears
IOFF - Blank figure appears but no spinning wheel and I get 'python.exe has stopped working' immediately after the figure appears
I need to get it working with the other backends but as currently I can at least get the non-interactive one to work with qt4agg, getting the interactive one working with that seems like a good place to start. Any ideas?
Related
I'm running Python 3.5.1 with matplotlib version 1.5.1 and iPython 5.0.0. I can't seem to get matplotlib's interactive feature working. I can run a command to create a plot:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1,2,3])
This doesn't show a figure until I manually execute plt.show(), at which point iPython hangs until I close the figure window. I have interactive set to True in my matplotlibrc file.
It's been a year or so since I used matplotlib. The last time I used it, I got interactive without having to execute plt.show(). Has something changed or am I doing something wrong?
You were probably using interactive mode previously.
Start ipython with:
ipython --pylab
Then your plots will show up instantly.
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.
I am trying to run python in XCode. The following simple plotting routine,
import matplotlib.pyplot as pyplot
pyplot.plot((0,1),(1,2))
pyplot.show()
returns no errors. XCode seems pretty happy with what it accomplishes when I hit the run button. But I get no plot window whatsoever, as far as I can tell. Is it hiding somewhere or what? How do I get to see it?
xcode will allow you to see plots if you move your code into an interactive notebook.
create a new file with the .ipynb file extension
copy your code into that file
You should be able not only to see the plots but also any other output your code is trying to send to the UI.
I was practicing plotting in matplotlib, and I noticed that the following code works correctly when I run it as one big chunk, but not when I enter it line by line into the console (a blank figure window shows up, but the line does not). What gives?
import matplotlib.pylab as plt
fig,axes=plt.subplots()
axes.plot([1,2,3],[2,3,4])
plt.show()
I'm running Enthought Canopy, Python 2.7.6., matplotlib 1.3.1
Thanks!
P.S. What I find even stranger is if I subsequently type fig into the console, all of a sudden the correct plot pops up in the figure window.
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.