Changing backend with ipython

Hi all,

All the usual apologies if this is a FAQ, but I can't find it.

Is there any way to change the backend interactively when using ipython?

Best of all would a way to make changes in the middle of a session, but even being able to do it at the beginning without editing matplotlibrc would be great.

For what I actually need to do, I don't really need to change the backend, but what I'd like to be able to do is just interactively stop all interactive (X11) output for my chosen backend -- I'm working over the network and it's very slow, but I want my savefig commandds to produce something.

Without ipython, I know you can do -dAgg (for example) but I don't see how /if that works with ipython.

Andrew

Hi all,

All the usual apologies if this is a FAQ, but I can't find it.

Is there any way to change the backend interactively when using ipython?

Take a look at the pylab/pyplot switch_backends function

switch_backend(newbackend)
    Switch the default backend to newbackend. This feature is
    **experimental**, and is only expected to work switching to an
    image backend. Eg, if you have a bunch of PostScript scripts that
    you want to run from an interactive ipython session, you may want
    to switch to the PS backend before running them to avoid having a
    bunch of GUI windows popup. If you try to interactively switch
    from one GUI backend to another, you will explode.

    Calling this command will close all open windows.

Eg:

switch_backends('Agg')

JDH

···

On Fri, Jun 27, 2008 at 12:52 PM, Andrew Jaffe <a.h.jaffe@...287...> wrote:

Hi John,

Thanks for the very fast response, but it doesn't quite work for my use case. I don't know if it's because my default is interactive, or ipython on general, or something else...

n21:~/work/Pointing> ipython -pylab -nobanner

In [1]: switch_backend('Agg')
/usr/local/src/python-2.5.2/lib/python2.5/site-packages/matplotlib/__init__.py:753: UserWarning: This call to matplotlib.use() has no effect
because the the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

   warnings.warn(_use_error_msg)

···

---------------------------------------------------------------------------
NameError Traceback (most recent call last)

/home/jaffe/work/Pointing/<ipython console> in <module>()

/usr/local/src/python-2.5.2/lib/python2.5/site-packages/matplotlib/pyplot.py in switch_backend(newbackend)
      52 global new_figure_manager, draw_if_interactive, show
      53 matplotlib.use(newbackend)
---> 54 reload(backends)
      55 from backends import new_figure_manager, draw_if_interactive, show
      56

NameError: global name 'backends' is not defined

In [2]:

John Hunter wrote:

On Fri, Jun 27, 2008 at 12:52 PM, Andrew Jaffe <a.h.jaffe@...287...> wrote:

Hi all,

All the usual apologies if this is a FAQ, but I can't find it.

Is there any way to change the backend interactively when using ipython?

Take a look at the pylab/pyplot switch_backends function

switch_backend(newbackend)
    Switch the default backend to newbackend. This feature is
    **experimental**, and is only expected to work switching to an
    image backend. Eg, if you have a bunch of PostScript scripts that
    you want to run from an interactive ipython session, you may want
    to switch to the PS backend before running them to avoid having a
    bunch of GUI windows popup. If you try to interactively switch
    from one GUI backend to another, you will explode.

    Calling this command will close all open windows.

Eg:

switch_backends('Agg')

JDH

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php

Andrew,

The traceback shows that you are tripping over something that has been changed since the version you are using, so it might work if you update your mpl, preferably from svn. I think the relevant change was made by John quite recently, but I haven't looked it up.

Eric

Andrew Jaffe wrote:

···

Hi John,

Thanks for the very fast response, but it doesn't quite work for my use case. I don't know if it's because my default is interactive, or ipython on general, or something else...

n21:~/work/Pointing> ipython -pylab -nobanner

In [1]: switch_backend('Agg')
/usr/local/src/python-2.5.2/lib/python2.5/site-packages/matplotlib/__init__.py:753: UserWarning: This call to matplotlib.use() has no effect
because the the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

   warnings.warn(_use_error_msg)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)

/home/jaffe/work/Pointing/<ipython console> in <module>()

/usr/local/src/python-2.5.2/lib/python2.5/site-packages/matplotlib/pyplot.py in switch_backend(newbackend)
      52 global new_figure_manager, draw_if_interactive, show
      53 matplotlib.use(newbackend)
---> 54 reload(backends)
      55 from backends import new_figure_manager, draw_if_interactive, show
      56

NameError: global name 'backends' is not defined

In [2]:

John Hunter wrote:

On Fri, Jun 27, 2008 at 12:52 PM, Andrew Jaffe <a.h.jaffe@...287...> wrote:

Hi all,

All the usual apologies if this is a FAQ, but I can't find it.

Is there any way to change the backend interactively when using ipython?

Take a look at the pylab/pyplot switch_backends function

switch_backend(newbackend)
    Switch the default backend to newbackend. This feature is
    **experimental**, and is only expected to work switching to an
    image backend. Eg, if you have a bunch of PostScript scripts that
    you want to run from an interactive ipython session, you may want
    to switch to the PS backend before running them to avoid having a
    bunch of GUI windows popup. If you try to interactively switch
    from one GUI backend to another, you will explode.

    Calling this command will close all open windows.

Eg:

switch_backends('Agg')

JDH

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Hi-

[OK, sorry for all the top-posting...]

Anyway, if I move to the current svn, I get

In [1]: switch_backend('Agg')

···

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)

/Users/jaffe/home/python/<ipython console> in <module>()

/Users/jaffe/Library/Python/2.5/site-packages/matplotlib-0.98.2-py2.5-macosx-10.3-fat.egg/matplotlib/pyplot.pyc in switch_backend(newbackend)
      53 close('all')
      54 global new_figure_manager, draw_if_interactive, show
---> 55 matplotlib.use(newbackend, warn=False)
      56 reload(matplotlib.backends)
      57 from matplotlib.backends import pylab_setup

TypeError: use() got an unexpected keyword argument 'warn'

Andrew

Eric Firing wrote:

Andrew,

The traceback shows that you are tripping over something that has been changed since the version you are using, so it might work if you update your mpl, preferably from svn. I think the relevant change was made by John quite recently, but I haven't looked it up.

Eric

Andrew Jaffe wrote:

Hi John,

Thanks for the very fast response, but it doesn't quite work for my use case. I don't know if it's because my default is interactive, or ipython on general, or something else...

n21:~/work/Pointing> ipython -pylab -nobanner

In [1]: switch_backend('Agg')
/usr/local/src/python-2.5.2/lib/python2.5/site-packages/matplotlib/__init__.py:753: UserWarning: This call to matplotlib.use() has no effect
because the the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

   warnings.warn(_use_error_msg)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)

/home/jaffe/work/Pointing/<ipython console> in <module>()

/usr/local/src/python-2.5.2/lib/python2.5/site-packages/matplotlib/pyplot.py in switch_backend(newbackend)
      52 global new_figure_manager, draw_if_interactive, show
      53 matplotlib.use(newbackend)
---> 54 reload(backends)
      55 from backends import new_figure_manager, draw_if_interactive, show
      56

NameError: global name 'backends' is not defined

In [2]:

John Hunter wrote:

On Fri, Jun 27, 2008 at 12:52 PM, Andrew Jaffe <a.h.jaffe@...287...> wrote:

Hi all,

All the usual apologies if this is a FAQ, but I can't find it.

Is there any way to change the backend interactively when using ipython?

Take a look at the pylab/pyplot switch_backends function

switch_backend(newbackend)
    Switch the default backend to newbackend. This feature is
    **experimental**, and is only expected to work switching to an
    image backend. Eg, if you have a bunch of PostScript scripts that
    you want to run from an interactive ipython session, you may want
    to switch to the PS backend before running them to avoid having a
    bunch of GUI windows popup. If you try to interactively switch
    from one GUI backend to another, you will explode.

    Calling this command will close all open windows.

Eg:

switch_backends('Agg')

JDH

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php

Hi-

[OK, sorry for all the top-posting...]

Anyway, if I move to the current svn, I get

In [1]: switch_backend('Agg')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)

/Users/jaffe/home/python/<ipython console> in <module>()

/Users/jaffe/Library/Python/2.5/site-packages/matplotlib-0.98.2-py2.5-macosx-10.3-fat.egg/matplotlib/pyplot.pyc
in switch_backend(newbackend)
     53 close('all')
     54 global new_figure_manager, draw_if_interactive, show
---> 55 matplotlib.use(newbackend, warn=False)
     56 reload(matplotlib.backends)
     57 from matplotlib.backends import pylab_setup

TypeError: use() got an unexpected keyword argument 'warn'

Try removing all the matplotlib installs, eggs, etc. Something is
getting confused, because warn is a valid keyword to use in svn.

···

On Fri, Jun 27, 2008 at 1:31 PM, Andrew Jaffe <a.h.jaffe@...287...> wrote:

rm -rf /Users/jaffe/Library/Python/2.5/site-packages/matplotlib*
rm -rf build
clean reinstall of mpl

Hi John,

John Hunter wrote:

Hi-

[OK, sorry for all the top-posting...]

Anyway, if I move to the current svn, I get

...

Try removing all the matplotlib installs, eggs, etc. Something is
getting confused, because warn is a valid keyword to use in svn.

rm -rf /Users/jaffe/Library/Python/2.5/site-packages/matplotlib*
rm -rf build
clean reinstall of mpl

In fact I deleted my whole svn directory and re-updated, but no change. I'm pretty sure I've done this correctly...

If it matters, I'm installing as an egg.
[python setupegg.py build bdist_egg;
easy_install dist/matplotlib-0.98.2-py2.5-macosx-10.3-fat.egg]

In [1]: switch_backend('Agg')

···

On Fri, Jun 27, 2008 at 1:31 PM, Andrew Jaffe <a.h.jaffe@...287...> wrote:

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)

/Users/jaffe/home/python/<ipython console> in <module>()

/Users/jaffe/Library/Python/2.5/site-packages/matplotlib-0.98.2-py2.5-macosx-10.3-fat.egg/matplotlib/pyplot.pyc in switch_backend(newbackend)
      53 close('all')
      54 global new_figure_manager, draw_if_interactive, show
---> 55 matplotlib.use(newbackend, warn=False)
      56 reload(matplotlib.backends)
      57 from matplotlib.backends import pylab_setup

TypeError: use() got an unexpected keyword argument 'warn'

In [2]: matplotlib.__version__
Out[2]: '0.98.2'

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php

Mid-session you can't swtich *GUI* backends in ipython because the
entire GUI event loop can only be initialized once. But at startup,
as of fairly recent versions you can do:

ipython -pylab -Xthread

for X in {g,w,q,q4}. This will force a specific threading/gui backend
regardless of your mplrc settings.

Cheers,

f

···

On Fri, Jun 27, 2008 at 10:52 AM, Andrew Jaffe <a.h.jaffe@...287...> wrote:

Hi all,

All the usual apologies if this is a FAQ, but I can't find it.

Is there any way to change the backend interactively when using ipython?

Best of all would a way to make changes in the middle of a session, but
even being able to do it at the beginning without editing matplotlibrc
would be great.

Hi,

Fernando Perez wrote:

Hi all,

All the usual apologies if this is a FAQ, but I can't find it.

Is there any way to change the backend interactively when using ipython?

Best of all would a way to make changes in the middle of a session, but
even being able to do it at the beginning without editing matplotlibrc
would be great.

Mid-session you can't swtich *GUI* backends in ipython because the
entire GUI event loop can only be initialized once. But at startup,
as of fairly recent versions you can do:

ipython -pylab -Xthread

for X in {g,w,q,q4}. This will force a specific threading/gui backend
regardless of your mplrc settings.

Actually, I want to be able to choose a *non*-GUI backend (speed/commmunication issues). (Or, as I said, just to turn off the GUI output...)

Yours,

Andrew

···

On Fri, Jun 27, 2008 at 10:52 AM, Andrew Jaffe <a.h.jaffe@...287...> wrote:

Cheers,

f

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php

In fact I deleted my whole svn directory and re-updated, but no change.
I'm pretty sure I've done this correctly...

You also need to delete previous installs of matplotlib.

If it matters, I'm installing as an egg.
[python setupegg.py build bdist_egg;
easy_install dist/matplotlib-0.98.2-py2.5-macosx-10.3-fat.egg]

Well, I'm looking at the signature of matplotlib.use in the source::

  def use(arg, warn=True):

and warn is a valid keyword as you can see. When you

import matplotlib
print matplotlib.__file__

are you getting the file you think you should?

If you have an old matplotlib installed somewhere on your system that
used namespace packages, you may be getting a different matplotlib
root than you think you ware. That is why I suggested you remove all
existing matplotlib installs anywhere on your path.

JDH

···

On Fri, Jun 27, 2008 at 1:49 PM, Andrew Jaffe <a.h.jaffe@...287...> wrote:

Hi John,

John Hunter wrote:>

In fact I deleted my whole svn directory and re-updated, but no change.
I'm pretty sure I've done this correctly...

You also need to delete previous installs of matplotlib.

If it matters, I'm installing as an egg.
[python setupegg.py build bdist_egg;
easy_install dist/matplotlib-0.98.2-py2.5-macosx-10.3-fat.egg]

Well, I'm looking at the signature of matplotlib.use in the source::

and warn is a valid keyword as you can see. When you

import matplotlib
print matplotlib.__file__

are you getting the file you think you should?

Aha, the problem is in ipython, which shadows user() with its own version, not yet updated for the new signature in my version.

Andrew

···

If you have an old matplotlib installed somewhere on your system that
used namespace packages, you may be getting a different matplotlib
root than you think you ware. That is why I suggested you remove all
existing matplotlib installs anywhere on your path.

JDH

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php

Hi all,

I should point out that for my actual use case, Fernando explained the right thing to do: start ipython *without* pylab, in which case use('Agg') [for example] is legal.

Andrew

Andrew Jaffe wrote:

···

Hi,

Fernando Perez wrote:

On Fri, Jun 27, 2008 at 10:52 AM, Andrew Jaffe <a.h.jaffe@...287...> wrote:

Hi all,

All the usual apologies if this is a FAQ, but I can't find it.

Is there any way to change the backend interactively when using ipython?

Best of all would a way to make changes in the middle of a session, but
even being able to do it at the beginning without editing matplotlibrc
would be great.

Mid-session you can't swtich *GUI* backends in ipython because the
entire GUI event loop can only be initialized once. But at startup,
as of fairly recent versions you can do:

ipython -pylab -Xthread

for X in {g,w,q,q4}. This will force a specific threading/gui backend
regardless of your mplrc settings.

Actually, I want to be able to choose a *non*-GUI backend (speed/commmunication issues). (Or, as I said, just to turn off the GUI output...)

Yours,

Andrew

Cheers,

f

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php