Saving plots without displaying them?

9,102 views
Skip to first unread message

Richard Stanton

unread,
Jan 16, 2013, 11:09:11 PM1/16/13
to pyd...@googlegroups.com

Sometimes I want to create and save a large number of plots, and it would be very convenient to be able to create and save them on disk without first displaying them on the screen. Is there a way to do this?

 

Thanks for any suggestions.

 

Richard Stanton

Adam Hughes

unread,
Jan 16, 2013, 11:11:46 PM1/16/13
to pyd...@googlegroups.com

Look at matplotlibs savefig function. You would call savefig then plt.clear or whatever the clear function is. Sorry responding from phone

--
 
 

Yaroslav Halchenko

unread,
Jan 17, 2013, 10:59:34 PM1/17/13
to pyd...@googlegroups.com
I guess should be as with any module using matplotlib -- just set
backend to non-interactive one (e.g. Agg) first and then use savefig ;)
see
http://matplotlib.org/faq/howto_faq.html#generate-images-without-having-a-window-appear
--
Yaroslav O. Halchenko
Postdoctoral Fellow, Department of Psychological and Brain Sciences
Dartmouth College, 419 Moore Hall, Hinman Box 6207, Hanover, NH 03755
Phone: +1 (603) 646-9834 Fax: +1 (603) 646-1419
WWW: http://www.linkedin.com/in/yarik

Richard Stanton

unread,
Jan 19, 2013, 3:07:25 AM1/19/13
to pyd...@googlegroups.com
Yaroslav Halchenko <yarik...@gmail.com> Jan 17 10:59PM -0500  

> I guess should be as with any module using matplotlib -- just set
> backend to non-interactive one (e.g. Agg) first and then use savefig ;)
> see
> http://matplotlib.org/faq/howto_faq.html#generate-images-without-having-a-window-appear

Thanks very much for the suggestion. The problem is that I'm trying to do this in an IPython notebook, and whenever I execute a plot command, the graph immediately appears on the screen. I can save it as well, using savefig, but it would be much faster if it wasn't displayed at all. I tried following the advice given in the link above, but when I try to execute the command

matplotlib.use('Agg'),

even right at the beginning of the notebook, I get the warning

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.

and the plot command continues to display on the screen.

Goyo

unread,
Jan 19, 2013, 7:40:42 AM1/19/13
to pyd...@googlegroups.com
That's because you are starting the notebook in pylab mode. Don't do that if you want to change the matplotlib backend in the notebook.

Goyo

Eraldo Pomponi

unread,
Jan 19, 2013, 5:03:57 AM1/19/13
to pyd...@googlegroups.com

Dear Richard,

Il giorno 19/gen/2013 09:07, "Richard Stanton" <sta...@haas.berkeley.edu> ha scritto:
>
> Yaroslav Halchenko <yarik...@gmail.com> Jan 17 10:59PM -0500  
>
> > I guess should be as with any module using matplotlib -- just set
> > backend to non-interactive one (e.g. Agg) first and then use savefig ;)
> > see
> > http://matplotlib.org/faq/howto_faq.html#generate-images-without-having-a-window-appear
>
> Thanks very much for the suggestion. The problem is that I'm trying to do this in an IPython notebook, and whenever I execute a plot command, the graph immediately appears on the screen. I can save it as well, using savefig, but it would be much faster if it wasn't displayed at all. I tried following the advice given in the link above, but when I try to execute the command
>
> matplotlib.use('Agg'),
>
> even right at the beginning of the notebook, I get the warning
>
> UserWarning:  This call to matplotlib.use() has no effect
> because the the backend has already been chosen;

Import matplotlib explicitly ... Do not use --pylab when starting ipython notebook.
Im this way you should be able to fix the matplotlib backend as you wish. 

HTH,

Eraldo

Richard Stanton

unread,
Jan 19, 2013, 1:03:55 PM1/19/13
to pyd...@googlegroups.com
Goyo <goyo...@gmail.com> Jan 19 04:40AM -0800 :

> That's because you are starting the notebook in pylab mode. Don't do that
> if you want to change the matplotlib backend in the notebook.
 
Eraldo Pomponi <eraldo....@gmail.com> Jan 19 11:03AM +0100  
 
> Import matplotlib explicitly ... Do not use --pylab when starting ipython
> notebook.

Thanks! That helped. Now I can switch on-screen plotting off completely.

Now the bonus question...

Is there any way to toggle the plotting behavior *within a single notebook*? Here's an example of what I'd like to do:

1) In one cell, plot a graph and have it displayed on the screen (within the notebook - not in a separate window)

2) In a later cell in the same notebook, have a loop where I generate and save multiple graphs. For this cell, to save time I don't want the graphs displayed on the screen as they're generated.

Paul Hobson

unread,
Jan 22, 2013, 8:24:40 PM1/22/13
to pyd...@googlegroups.com
The cell-level magic "%pylab" might do what you want.
-p

Richard Stanton

unread,
Jan 24, 2013, 12:44:43 AM1/24/13
to pyd...@googlegroups.com
Paul Hobson <pmho...@gmail.com> Jan 22 05:24PM -0800  

On Sat, Jan 19, 2013 at 10:03 AM, Richard Stanton <sta...@haas.berkeley.edu
 
> The cell-level magic "%pylab" might do what you want.

Thanks, Paul. I'll play around with this and see what I can work out.
 
Reply all
Reply to author
Forward
0 new messages