通过 Eclipse PyDev 的交互式 matplotlib
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13440956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Interactive matplotlib through Eclipse PyDev
提问by Michael
This is a follow up to this interactive matplolib through eclipsethread which is about 2 years old, I was wondering if there has been any progress in the meantime.
这是通过大约 2 岁的eclipse线程对这个交互式 matplolib的后续行动,我想知道在此期间是否有任何进展。
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 outsideEclipse PyDev. Here is how the two compare:
我正在 Eclipse PyDev 的控制台窗口中运行 IPython 控制台,但是我无法使用 matplotlib 获得相同的交互式绘图功能,就像我要在Eclipse PyDev之外的 (Windows) 命令提示符下运行 IPython 一样。以下是两者的比较:
A) Running IPython in a shell outside Eclipse PyDev
A) 在 Eclipse PyDev 之外的 shell 中运行 IPython
- 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.
- 使用“ipython --pylab”在 Windows 命令提示符下运行 IPython
- 在 IPython 控制台中输入“plot([1,2,3])”。这将打开一个图形绘图窗口,并且 IPython 控制台已准备好接收更多命令(无需关闭图形绘图窗口)。
- 例如,我可以输入“xlabel('years')”,这将更新我的图形绘图窗口。
B) Running IPython in an interactive console within Eclipse PyDevEnter the following in the IPython interactive console within Eclipse PyDev:
B) 在 Eclipse PyDev 内的交互式控制台中运行 IPython 在 Eclipse PyDev 内的 IPython 交互式控制台中输入以下内容:
- "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.
- “从pylab导入*”
- "plot([1,2,3])" --> 图绘图窗口不显示。
- 我必须输入“show()”才能打开图形绘图窗口。但现在的问题是,只要我保持图形绘图窗口打开,IPython 控制台就不会接受任何新命令。
- 所以我关闭图形绘图窗口,输入“xlabel('years')”,然后再次输入“show()”。这将重新打开图形绘图窗口,以“年”作为我的轴标签,但绘图本身是空的,不再显示 [1,2,3] 数据。
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).
有了这种行为,A) 显然优于 B),但我想继续在 Eclipse PyDev 中工作,因为我喜欢总是在我的屏幕上显示变量列表(无需像运行 IPython 表单那样运行命令来显示所有变量一个窗户外壳)。正如另一个线程中所建议的那样,使用 Wicked Shell 不起作用(IPython 在 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?
如何在 Eclipse PyDev 中配置 IPython,使其显示与在 Windows 命令提示符下运行相同的交互行为?
回答by Andy
You can solve this problem by selecting a GUI for the Interactive Console in PyDev Preference.
您可以通过在 PyDev Preference 中为交互式控制台选择 GUI 来解决此问题。
Eclipse -> Window -> Preferences -> Pydev -> Interactive Console -> Enable GUI event loop integration.
Eclipse -> 窗口 -> 首选项 -> Pydev -> 交互式控制台 -> 启用 GUI 事件循环集成。
In my case, I chose PyQt (qt/qt4)
就我而言,我选择了 PyQt (qt/qt4)
回答by Spock
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".
我相信 OP 描述的是正常行为。实际上,从命令行 ipython 开始,导入 pylab 并发出 plot 命令会产生所描述的阻塞行为,因此这与 pydev 或 eclipse 无关。事实是 matplotlib 中的 show 在交互模式下是阻塞的;当您在以“ipython --pylab”开头的 ipython 会话中使用 matplotlib 时,您正在利用 ipython 开发人员围绕 matplotlib 为您所做的一些“技巧”,允许同时具有交互模式和非阻塞调用。但是,导入 pylab 还不足以应用这些“技巧”。PyDev 似乎不允许解释器调用的标志,因此不能直接调用“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.
幸运的是,ipython 有一个特殊的命令“pylab”,即使解释器没有以 pylab 标志启动,它也会应用黑客并导入 pylab。因此,您可以尝试在控制台中键入“pylab”(实际上,您甚至可以自定义 pydev 控制台,使其自动完成),并且您应该获得所需的行为。但是,我必须报告,虽然这对我来说从命令行启动的 ipython 会话工作正常,但当我尝试从 Eclipse 内部执行相同操作时出现问题。该命令不会阻止,我得到了 python 图标,但 matplotlib 窗口没有显示。为了记录,我使用的是运行 Snow Leopard 的 Mac。我无法判断同样的问题是否也发生在 Windows 中,OP 似乎正在使用。
回答by Liteon
I achieve similar behave in Eclipse PyDev by executing plotting function in another thread:
通过在另一个线程中执行绘图功能,我在 Eclipse PyDev 中实现了类似的行为:
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
使用工具链 Eclipse 4.3、PyDev 2.7.1、Python 3.2、IPython 0.13 进行测试
回答by ankostis
Just use the %matplotlib
magic-commandto activate interactive plotting (exactly what you described).
只需使用%matplotlib
魔术命令激活交互式绘图(正是您所描述的)。
The pylab
command imports numpy.*
and pylab.*
, seriously polluting your global namespace.
该pylab
命令会导入numpy.*
和pylab.*
,严重污染了您的全局命名空间。