13
votes

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.

  • 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?

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

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.
2
I think you're trying to use the inline option in the terminal - that won't work, because the terminal isn't capable of displaying graphics. inline requires the Qt console or the Notebook. But I'm not sure what you're trying to embed the figures in? - Thomas K
@ThomasK How to distinguish terminal and console? - embert
What do you mean? IPython has the Qt console (ipython qtconsole), which is capable of displaying graphics. - Thomas K
@thomask so only qtconsole can do that. I thought a can run a script which has some plots in it with ipython so that the plots will not pop up in separate windows but are shown within the console instead. Is that possible?. I tried ipython qtconsole --pylab inline example_plots.py which launches the console, but thats it. - embert

2 Answers

9
votes

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

It works using the -m flag

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 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) will give and unknown failure, too

2
votes

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

  • You need to use the qt console.
  • Run ipython qtconsole <yourscript.py>.
  • If you have jupyter installed, the better.
  • Run jupyter qtconsole <yourscript.py>

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

  • Run %matplotlib inline prior to importing matplotlib.pyplot.

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

  • Yes. But pylab has been depreciated. Use %matplotlib inline.