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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21366672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:41:55  来源:igfitidea点击:

Run python script in IPython with inline / embedded plots

pythonmatplotlibinlineipython

提问by embert

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

我想在一个文件夹中包含一个带有python脚本的批处理文件。批处理文件应调用脚本IPython并绘制内嵌/嵌入的图形。尽管有很多关于这方面的信息,但我未能使其正常工作。

  • How to run a pythonscript with IPython, showing plots embedded?
  • Do I need to use pylabor can I just import matplotlib.pyplotin the script?
  • Do I have to adapt anything else in the script?
  • Is %pylab inline/ %matplotlib inlineto be used or not?
  • 如何运行python脚本IPython,显示嵌入的图?
  • 我需要使用pylab还是我可以只matplotlib.pyplot在脚本中导入?
  • 我是否必须修改脚本中的其他内容?
  • 是否使用%pylab inline/ %matplotlib inline

The latter commands give

后面的命令给出

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

Gives me the following and quits

给我以下内容并退出

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)

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

以下How to make IPython notebook matplotlib plot inline
with the batch file(与控制台相同):

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.

打开一个空的笔记本,就是这样。

What else should I try?

我还应该尝试什么?

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.

采纳答案by embert

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

格雷格在这里回答:
脚本执行后启动 ipython qtconsole 作为交互式解释器

It works using the -mflag

它使用-m标志工作

ipython qtconsole --matplotlib inline -m example_plots

or

或者

ipython qtconsole --pylab inline -m example_plots

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

但我不知道,这两个选项pylab inlinematplotlib inline. 从这里我想,我宁愿使用--matplotlib inline

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

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

pyplot.show(block=True)will give and unknown failure, too

pyplot.show(block=True)将给予和unknown failure,太

回答by Van

How to run a python script with IPython, showing plots embedded?

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

  • You need to use the qt console.
  • Run ipython qtconsole <yourscript.py>.
  • If you have jupyter installed, the better.
  • Run jupyter qtconsole <yourscript.py>
  • 您需要使用 qt 控制台。
  • 运行ipython qtconsole <yourscript.py>
  • 如果你安装了 jupyter,那就更好了。
  • jupyter qtconsole <yourscript.py>

Do I need to use pylab or can I just import matplotlib.pyplot in the script?

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

  • Run %matplotlib inlineprior to importing matplotlib.pyplot.
  • %matplotlib inline在导入 matplotlib.pyplot 之前运行。

Is %pylab inline / %matplotlib inline to be used or not?

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

  • Yes. But pylab has been depreciated. Use %matplotlib inline.
  • 是的。但是pylab已经折旧了。使用%matplotlib inline.