在IPython中运行带有内联/嵌入绘图的python脚本

2024-03-28 21:47:11 发布

您现在位置:Python中文网/ 问答频道 /正文

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

  • 如何使用IPython运行python脚本,显示嵌入的绘图?
  • 我需要使用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.

Tags: py脚本backendmatplotlibexampleasipythonnot
2条回答

格雷格在这里回答:
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之间的区别是什么。根据hereI图,我宁愿使用--matplotlib inline

即使添加文件以结束它也可以做我想做的事情,但它会在执行后抱怨unknown failure

pyplot.show(block=True)也会给予和unknown failure

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

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

我需要使用pylab还是只需在脚本中导入matplotlib.pyplot?

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

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

  • 是的。但皮拉布已经贬值了。使用%matplotlib inline

相关问题 更多 >