使用内联/嵌入图在IPython中运行python脚本

时间:2014-01-26 17:21:37

标签: python matplotlib inline ipython

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

  • 如何使用python运行IPython脚本,显示嵌入的图表?
  • 我是否需要使用pylab或者我可以在脚本中导入matplotlib.pyplot吗?
  • 我是否必须改编脚本中的其他内容?
  • 是否使用%pylab inline / %matplotlib inline

后面的命令给出了

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']`

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

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.

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

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)

关注How to make IPython notebook matplotlib plot inline
使用批处理文件(和控制台一样):

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).

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

我还应该尝试什么?

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.

2 个答案:

答案 0 :(得分:7)

格雷格在这里回答:
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 inlinematplotlib inline之间的区别是什么。从here开始,我想,我宁愿使用--matplotlib inline

即使添加结束它的文件也可以执行我想要的操作,但是在执行后它会抱怨unknown failure

pyplot.show(block=True)也会提供unknown failure

答案 1 :(得分:2)

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

  • 您需要使用qt控制台。
  • 运行ipython qtconsole <yourscript.py>
  • 如果你安装了jupyter,那就更好了。
  • 运行jupyter qtconsole <yourscript.py>

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

  • 在导入matplotlib.pyplot之前运行%matplotlib inline

是否使用%pylab内联/%matplotlib内联?

  • 是。但是pylab已经被折旧了。使用%matplotlib inline