简体   繁体   English

通过Eclipse PyDev进行交互式matplotlib

[英]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. 这是这个交互式matplolib通过eclipse线程的一个跟进,大约2岁,我想知道在此期间是否有任何进展。

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. 我在Eclipse PyDev的控制台窗口中运行IPython控制台,但我无法使用matplotlib获得相同的交互式绘图功能,就像我 Eclipse PyDev 的(Windows)命令提示符中运行IPython一样。 Here is how the two compare: 以下是两者的比较方式:

A) Running IPython in a shell outside Eclipse PyDev A)在Eclipse PyDev外部的shell中运行IPython

  1. Run IPython in a Windows command prompt with "ipython --pylab" 使用“ipython --pylab”在Windows命令提示符下运行IPython
  2. Within the IPython console enter "plot([1,2,3])". 在IPython控制台中输入“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). 这将打开一个图形图窗口,IPython控制台已准备好进一步命令(无需关闭图形图窗口)。
  3. For example, I can enter "xlabel('years')" and this will update my figure plot window. 例如,我可以输入“xlabel('years')”这将更新我的图形图窗口。

B) Running IPython in an interactive console within Eclipse PyDev Enter the following in the IPython interactive console within Eclipse PyDev: B)在Eclipse PyDev中的交互式控制台中运行IPython在Eclipse PyDev中的IPython交互式控制台中输入以下内容:

  1. "from pylab import *" “来自pylab import *”
  2. "plot([1,2,3])" --> Figure plot window does not show up. “plot([1,2,3])” - >图形图窗口不显示。
  3. I have to enter "show()" to open the figure plot window. 我必须输入“show()”来打开图形图窗口。 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. 但现在的问题是,只要我保持图形图窗口打开,IPython控制台就不接受任何新命令。
  4. So I close the figure plot window, enter "xlabel('years')", followed by "show()" again. 所以我关闭图形图窗口,输入“xlabel('years')”,然后再输入“show()”。 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. 这将重新打开图形图窗口,其中“years”作为我的轴标签,但是图表本身是空的,不再显示[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). 有了这种行为,A)显然优于B),但我想继续在Eclipse PyDev中工作,因为我喜欢在我的屏幕上总是有变量列表(无需运行命令来显示所有变量,比如在运行IPython表单时)一个Windows shell)。 Using Wicked Shell, as suggested in the other thread, does not work (IPython does not work properly in Wicked Shell). 使用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命令提示符下运行它一样?

You can solve this problem by selecting a GUI for the Interactive Console in PyDev Preference. 您可以通过在PyDev首选项中为交互式控制台选择GUI来解决此问题。

Eclipse -> Window -> Preferences -> Pydev -> Interactive Console -> Enable GUI event loop integration. Eclipse - > Window - > Preferences - > Pydev - > Interactive Console - >启用GUI事件循环集成。

In my case, I chose PyQt (qt/qt4) 就我而言,我选择了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. 我认为OP所描述的是正常行为。 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. 实际上,从命令行ipython开始,导入pylab并发出plot命令会产生所描述的阻塞行为,因此这与pydev或eclipse无关。 The fact is that show in matplotlib is blocking in interactive mode; 事实是matplotlib中的show在交互模式下是阻塞的; 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. 当您在ipython会话中使用matplotlib作为“ipython --pylab”启动时,您正在利用ipython开发人员在matplotlib周围为您做的一些“黑客”,允许同时具有交互模式和非阻塞调用。 However, importing pylab is not enough to apply these "hacks". 但是,导入pylab不足以应用这些“黑客”。 PyDev does not seems to allow flags to the interpreter call, so one can't directly invoke "ipython --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. 幸运的是,ipython有一个特殊的命令“pylab”,即使没有使用pylab标志启动解释器,也会应用hack并导入pylab。 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. 因此,您可以尝试在控制台中键入“pylab”(实际上,您甚至可以自定义pydev控制台以便自动完成),您应该获得所需的行为。 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. 但是,我必须报告,虽然从命令行启动的ipython会话对我来说这很好,但是当我尝试从Eclipse内部执行相同操作时出现问题。 The command doesn't block, I get the python icon but the matplotlib window doesn't show up. 命令没有阻塞,我得到了python图标,但是matplotlib窗口没有出现。 For the records, I am on a Mac running Snow Leopard. 为了记录,我在运行Snow Leopard的Mac上。 I am not able to tell if the same problem happens also in Windows, that the OP seems to be using. 我无法判断在Windows中是否也出现了同样的问题,OP似乎正在使用。

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进行测试

Just use the %matplotlib magic-command to activate interactive plotting (exactly what you described). 只需使用%matplotlib magic-command激活交互式绘图(正是您所描述的)。

The pylab command imports numpy.* and pylab.* , seriously polluting your global namespace. pylab命令导入numpy.*pylab.* ,严重污染全局命名空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM