简体   繁体   English

Linux上的ipython笔记本运行matplotlib与nbagg交互的VM

[英]ipython notebook on linux VM running matplotlib interactive with nbagg

I want buttons and other interactive matplotlib objects to appear from within my ipython notebook. 我希望按钮和其他交互式matplotlib对象出现在我的ipython笔记本中。 截图

Here is what I've done: 这就是我所做的:

  1. Installed http://datasciencetoolbox.org , it is a vagrant box with ipython installed and version 1.3.1 of matplotlib. 安装了http://datasciencetoolbox.org ,它是一个安装了ipython和matplotlib版本1.3.1的流浪盒。

  2. I needed to upgrade matplotlib to the latest version, because it has this capability to do inline interactive plots. 我需要将matplotlib升级到最新版本,因为它具有执行内联交互式绘图的功能。 What's new in Matplotlib 1.4.1 Matplotlib 1.4.1中的新功能
    I needed to run sudo apt-get install pkg-config and 我需要运行sudo apt-get install pkg-config
    sudo pip install matplotlib --upgrade in order to get that going. sudo pip install matplotlib --upgrade以便实现这一目标。

  3. Then, in order to produce the nice (ie error-free) screenshot below, I went into the .ipython/dst-profile/ipython_notebook_config.py file and erased the line about IPKernelApp.pylab='inline' to be able to run the matplotlib.use('nbagg') command. 然后,为了生成下面的好(即无错误)屏幕截图,我进入了.ipython/dst-profile/ipython_notebook_config.py文件,删除了关于IPKernelApp.pylab='inline' ,以便能够运行matplotlib.use('nbagg')命令。

  4. Then I was able to create the screenshot below. 然后我就可以在下面创建截图了。 However, things still look poor. 然而,事情看起来仍然很糟糕。 Those buttons are not buttons. 那些按钮不是按钮。 That is an image of buttons. 那是按钮的图像。 Please advise on how to make those buttons come to life! 请告知如何使这些按钮变得生动!

Oh... and check this out if this helps you help me. 呵呵......,并检查出如果这能帮助你帮助我。

Thanks! 谢谢!

截图

Basically you are facing two issues 基本上你面临着两个问题

  • the %pylab inline call overrides the matplotlib.use('nbagg') call, to use the inline backend instead of the nbagg backend which you are actually wanting. %pylab inline调用会覆盖matplotlib.use('nbagg')调用,以使用内联后端而不是你真正想要的nbagg后端。 If you use a recent version of IPython (2.3) you can directly use %matplotlib nbagg (or %matplotlib notebook ) to load the nbagg backend instead of your %pylab call. 如果您使用最新版本的IPython(2.3),您可以直接使用%matplotlib nbagg (或%matplotlib notebook )来加载nbagg后端而不是%pylab调用。

  • once you enabled the nbagg backend you will need to explicitly show it, ie. 一旦你启用了nbagg后端,你需要明确地显示它,即。 add a plt.show() call at the end of your script -> Update : with IPython 2.3.1 this is no longer needed (thanks @tcaswell for the hint) 在脚本末尾添加一个plt.show()调用 - > 更新 :使用IPython 2.3.1不再需要它(感谢@tcaswell提示)

With this you get the interactive matplotlib experience embedded in the IPython notebook. 通过这种方式,您可以获得嵌入在IPython笔记本中的交互式matplotlib体验。 However, a quick try of your code does't yield to the desired result. 但是,快速尝试代码并不能达到预期的效果。 The Button reacts and the callback is executed but the print call doesn't show anything. Button做出反应并执行回调,但print调用没有显示任何内容。 Anyway, to see that it's working try the following simple example (requires IPython 2.3): 无论如何,要看到它正在运行,请尝试以下简单示例(需要IPython 2.3):

%matplotlib nbagg
from matplotlib.widgets import Button
import matplotlib.pyplot as plt
def callback(event):
    plt.text(event.xdata, event.ydata, 'clicked')

f,a = plt.subplots(1)
b1 = Button(a,'Button1')
b1.on_clicked(callback)
plt.show()

Btw. 顺便说一句。 it is highly recommended to use %matplotlib instead of %pylab as later leads to some side effects, see here . 强烈建议使用%matplotlib而不是%pylab,因为后来会产生一些副作用,请参见此处

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

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