使用内联/嵌入图在IPython中运行python脚本 [英] Run python script in IPython with inline / embedded plots

查看:509
本文介绍了使用内联/嵌入图在IPython中运行python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个带有 python 脚本的文件夹中有一个批处理文件。批处理文件应该在 IPython 中调用脚本,并绘制内联/嵌入的数字。虽然有很多关于这方面的信息,但我没有让它工作。

I'd like to have a batch file in one folder with a python script. The batch file should call the script in IPython and plot the figures inline / embedded. Though there is lots of info on this around, I failed getting this to work.


  • 如何运行 python 脚本与 IPython ,显示嵌入的图?

  • 我需要使用 pylab 或者我可以在脚本中导入 matplotlib.pyplot 吗?

  • 我是否必须调整脚本中的其他内容?

  • %pylab inline / %matplotlib inline 是否使用?

  • How to run a python script with IPython, showing plots embedded?
  • Do I need to use pylab or can I just import matplotlib.pyplot in the script?
  • Do I have to adapt anything else in the script?
  • Is %pylab inline / %matplotlib inline to be used or not?

后面的命令给出

In [1]: %pylab inline
UsageError: Invalid GUI request u'inline', valid ones are:[None, 'osx', 'qt4', 
'glut',   'gtk3', 'pyglet', 'wx', 'none', 'qt', 'gtk', 'tk']
In [2]: %matplotlib inline 
UsageError: Invalid GUI request u'inline', valid ones are:  [None, 'osx', 'qt4',
'glut', 'gtk3', 'pyglet', 'wx', 'none', 'qt', 'gtk', 'tk']`

到目前为止,我尝试了以下(不幸的)

Up to now I tried the following (luckless)

ipython --pylab=inline example_plots.py

给我以下内容并退出

E:\CD\package\bin>ipython --pylab=inline example_plots.py
WARNING: 'inline' not available as pylab backend, using 'auto' instead.
WARNING: 'inline' not available as pylab backend, using 'auto' instead.

或从控制台运行,但像往常一样弹出数字(并立即关闭):

or from console it runs but as usual with figures popping up (and closing right away):

E:\CD\package\bin>ipython --pylab=inline example_plots.py
WARNING: 'inline' not available as pylab backend, using 'auto' instead.
WARNING: 'inline' not available as pylab backend, using 'auto' instead.
Using matplotlib backend: Qt4Agg
# runs python script as usual (not inline)

关注如何使IPython笔记本matplotlib内联插图 < br>
使用批处理文件(和控制台一样):

Following How to make IPython notebook matplotlib plot inline
With the batch file (and the same with the console):

ipython notebook --pylab inline example_plots.py
2014-01-26 17:52:10.101 [NotebookApp] Using existing profile dir: u'C:\\Users\\Robert\\.ipython\\profile_default'
2014-01-26 17:52:10.111 [NotebookApp] Using MathJax from CDN: http://cdn.mathjax.org/mathjax/latest/MathJax.js
2014-01-26 17:52:10.127 [NotebookApp] Serving notebooks from local directory: E:\CD\package\bin
2014-01-26 17:52:10.128 [NotebookApp] The IPython Notebook is running at: http://127.0.0.1:8888/
2014-01-26 17:52:10.128 [NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

打开一个空的笔记本就可以了。

Opens an empty notebook and thats it.

我还应该尝试什么?

Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]
IPython 1.1.0 -- An enhanced Interactive Python.


推荐答案

Greg在这里回答:

在脚本执行后启动ipython qtconsole作为交互式解释器

Greg answered it here:
Start ipython qtconsole as interactive interpreter after script execution

它使用 -m 标志

ipython qtconsole --matplotlib inline -m example_plots

ipython qtconsole --pylab inline -m example_plots

但我不知道,这两个选项的区别是什么 pylab inline matplotlib inline 。从这里我想,我宁愿使用 - matplotlib inline

But I do not know, what makes the difference between those two options pylab inline and matplotlib inline. From here I figure, that I'd rather use --matplotlib inline

即使添加结束它的文件也能做到我想要的,但它在执行后抱怨未知失败

Even adding the file ending it does what I want to, but it complains about unknown failure after execution.

pyplot.show(block = True)将给出未知失败

这篇关于使用内联/嵌入图在IPython中运行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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