运行Matplotlib与linux交互的linux VM上的ipython笔记本 [英] ipython notebook on linux VM running matplotlib interactive with nbagg

查看:83
本文介绍了运行Matplotlib与linux交互的linux VM上的ipython笔记本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望按钮和其他交互式matplotlib对象从我的ipython笔记本中出现.

I want buttons and other interactive matplotlib objects to appear from within my ipython notebook.

这是我所做的:

  1. 已安装 http://datasciencetoolbox.org ,它是一个装有ipython版本1.3的无所事事的盒子.1的matplotlib.

  1. Installed http://datasciencetoolbox.org, it is a vagrant box with ipython installed and version 1.3.1 of matplotlib.

我需要将matplotlib升级到最新版本,因为它具有执行内联交互式绘图的功能. Matplotlib 1.4.1的新功能
我需要运行sudo apt-get install pkg-config
sudo pip install matplotlib --upgrade为了实现这一目标.

I needed to upgrade matplotlib to the latest version, because it has this capability to do inline interactive plots. What's new in Matplotlib 1.4.1
I needed to run sudo apt-get install pkg-config and
sudo pip install matplotlib --upgradein order to get that going.

然后,为了生成下面的漂亮(即无错误)屏幕截图,我进入了.ipython/dst-profile/ipython_notebook_config.py文件并删除了关于IPKernelApp.pylab='inline'的行,以便能够运行matplotlib.use('nbagg')命令.

Then, in order to produce the nice (i.e. 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.

然后,我能够创建以下屏幕截图.但是,情况看起来仍然很糟糕.这些按钮不是按钮.那是按钮的图像.请提供有关如何使这些按钮栩栩如生的建议!

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.

谢谢!

推荐答案

基本上,您面临两个问题

Basically you are facing two issues

  • %pylab inline调用会覆盖matplotlib.use('nbagg')调用,以使用 inline 后端而不是您实际需要的 nbagg 后端.如果您使用的是最新版本的IPython(2.3),则可以直接使用%matplotlib nbagg(或%matplotlib notebook)来加载nbagg后端,而不用进行%pylab调用.

  • the %pylab inlinecall overrides the matplotlib.use('nbagg')call, to use the inline backend instead of the nbagg backend which you are actually wanting. 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 %pylabcall.

一旦启用了 nbagg 后端,就需要显式显示它,即.在脚本末尾添加plt.show()调用-> 更新:使用IPython 2.3.1时,不再需要此调用(感谢@tcaswell作为提示)

once you enabled the nbagg backend you will need to explicitly show it, ie. 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)

通过此操作,您将获得嵌入在IPython笔记本中的交互式matplotlib体验.但是,快速尝试您的代码并不能达到预期的结果. Button会做出反应并执行回调,但是print调用不会显示任何内容.无论如何,要查看其工作原理,请尝试以下简单示例(需要IPython 2.3):

With this you get the interactive matplotlib experience embedded in the IPython notebook. 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. Anyway, to see that it's working try the following simple example (requires 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()

顺便说一句.强烈建议使用%matplotlib代替%pylab,因为以后会产生一些副作用,请参见

Btw. it is highly recommended to use %matplotlib instead of %pylab as later leads to some side effects, see here.

这篇关于运行Matplotlib与linux交互的linux VM上的ipython笔记本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆