clearing a figure

Hello,

I'm a new matplotlib user, coming from the Matlab end.

Is there a standard way to create a figure (here I'd like the equivalent of matlab's hold on, so I can draw multiple things) and then clear the figure (so the drawing goes away) so I can repeat the process again? The commands to plot that I'll be using are fairly simple line commands.

One of the reasons I'm confused is b/c when I poked around, I found a clear method:

>>> help(pylab.gcf().clear)
Help on method clear in module matplotlib.figure:

clear(self) method of matplotlib.figure.Figure instance
     Clear the figure

but when I execute this on my open figure:

>>>pylab.gcf().clear()

nothing happens; the figure's still displaying whatever was already on it.

So far, the only thing I've found that works is to call plot differently when its time to clear the figure (pass hold=False). Subsequent calls to plot (w/o this option) keep adding to, which is great.

Is passing a hold=False arg to a drawing command the preferred way to clear a figure, or is clear() not working properly?

Many thanks,

--b

···

On Jan 9, 2007, at 7:50 PM, belinda thom wrote:

Hello,

I'm a new matplotlib user, coming from the Matlab end.

Is there a standard way to create a figure (here I'd like the equivalent of matlab's hold on, so I can draw multiple things) and then clear the figure (so the drawing goes away) so I can repeat the process again? The commands to plot that I'll be using are fairly simple line commands.

Hi,

I've been playing w/both IDLE and IPython, using TkAgg in both cases as the back end. Also, I've got the latest matplotlib and ipython versions and am using MacPython's 2.4.4 IDLE.

It seems that if IDLE is not invoked w/the -n flag, the figures that are drawn can often get the "whirling swirl of death" (i.e. they hang). Has it been other users' experience that the "-n" removes that problem (it mentioned this flag in the manual, but I didn't catch he motivation)? And if so, is there no other way to use IDLE when using matplotlib interactively? (The nice thing about IDLE is its "fresh" state each time you run a file; this goes away when -n is used).

I'm pleased to report no whirling wheels when using matplotlib via IPython.

By being able to compare behavior on both I might have found a bug:

I can create plots to my hearts content in both, but when I ask to rescale the plots, e.g. axis([-.2,2.4,-2,2.4]), NOTHING happens to the figure drawn via IDLE, whereas the command works as expected in IPython.

I hope posting this is useful. Feedback welcome.

--b

belinda thom wrote:

One of the reasons I'm confused is b/c when I poked around, I found a clear method:

>>> help(pylab.gcf().clear)
Help on method clear in module matplotlib.figure:

clear(self) method of matplotlib.figure.Figure instance
     Clear the figure

but when I execute this on my open figure:

>>>pylab.gcf().clear()

nothing happens; the figure's still displaying whatever was already on it.

The reason is that in interactive mode (as with ipython -pylab) the figure is not redrawn after you execute this method. What you want instead is

pylab.clf()

which will call the clear and then call draw_if_interactive().

This is the big difference between most pylab functions and the corresponding axes or figure methods that they wrap: the pylab functions automatically take care of redrawing the figure if you are in an interactive mode.

So far, the only thing I've found that works is to call plot differently when its time to clear the figure (pass hold=False). Subsequent calls to plot (w/o this option) keep adding to, which is great.

pylab also has a hold() function similar to the Matlab command, as an alternative to passing the hold state in the plotting command call:

def hold(b=None):
     """
     Set the hold state. If hold is None (default), toggle the
     hold state. Else set the hold state to boolean value b.

     Eg
     hold() # toggle hold
     hold(True) # hold is on
     hold(False) # hold is off

     When hold is True, subsequent plot commands will be added to the
     current axes. When hold is False, the current axes and figure
     will be cleared on the next plot command

Eric

···

Is passing a hold=False arg to a drawing command the preferred way to clear a figure, or is clear() not working properly?

Many thanks,

--b

On Jan 9, 2007, at 7:50 PM, belinda thom wrote:

Hello,

I'm a new matplotlib user, coming from the Matlab end.

Is there a standard way to create a figure (here I'd like the equivalent of matlab's hold on, so I can draw multiple things) and then clear the figure (so the drawing goes away) so I can repeat the process again? The commands to plot that I'll be using are fairly simple line commands.

I had the same problem about figures that freeze using IDLE and matplotlib but finally I manage to solve it with the help of Eric and also a bit of luck.
Instead of using TkAgg I use WxAgg and it never hangs.
I use Idle -n and in the matplotlibrc set Interactive
I use all latest version of Numpy, scipy, matplotlib and wxPython2.8-win32-unicode-2.8.0.1-py25

Hope this will help

Giorgio

Eric Firing wrote:

This is the big difference between most pylab functions and the corresponding axes or figure methods that they wrap: the pylab functions automatically take care of redrawing the figure if you are in an interactive mode.

Now I feel bad -- I think I encouraged Belinda to work with the OO interface, because I think it's the better way to go, and, in particular, translates better to putting MPL code in larger programs.

However, it is the case that there is a lot of stuff in pylab that makes it easier to use MPL in interactive mode. I kind of think that's a shame. I don't think that there is any reason that an OO interface is less suited to interactive mode.

I've thought for a while that I'd love to write a "OOlab" module -- that is, an object oriented interface to matplotlib that is well suited to interactive use.

However,

1) who know when I'll get around to it, and I haven't yet because I hardly ever do much interactively anyway (I didn't with Matlab, either).

2) this is an example of how it's hard to do -- a method like Figure.Clear() clearly belongs just where it is in an OO framework. Would it be possible for all those OO drawing methods to be able to query an "interactive" property somewhere? Does it live only in pylab now?

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@...259...

Christopher Barker wrote:

Eric Firing wrote:

This is the big difference between most pylab functions and the corresponding axes or figure methods that they wrap: the pylab functions automatically take care of redrawing the figure if you are in an interactive mode.

Now I feel bad -- I think I encouraged Belinda to work with the OO interface, because I think it's the better way to go, and, in particular, translates better to putting MPL code in larger programs.

However, it is the case that there is a lot of stuff in pylab that makes it easier to use MPL in interactive mode. I kind of think that's a shame. I don't think that there is any reason that an OO interface is less suited to interactive mode.

Even without the automatic-redraw difference, the OO interface requires more typing, and more mental record-keeping, than the pylab interface.
Typing "plot(x,y)" is easier to do and remember than creating a figure, adding axes, and then typing "ax1.plot(x,y)". For interactive use, I really don't see any advantage to an OO interface. What advantage do you see?

Eric

Christopher Barker wrote:

Eric Firing wrote:

This is the big difference between most pylab functions and the
corresponding axes or figure methods that they wrap: the pylab
functions automatically take care of redrawing the figure if you are
in an interactive mode.

Now I feel bad -- I think I encouraged Belinda to work with the OO
interface, because I think it's the better way to go, and, in
particular, translates better to putting MPL code in larger programs.

Don't feel bad. The online community is great, and I've appreciated all the advise I've gotten.

However, it is the case that there is a lot of stuff in pylab that makes
it easier to use MPL in interactive mode. I kind of think that's a
shame. I don't think that there is any reason that an OO interface is
less suited to interactive mode.

Even without the automatic-redraw difference, the OO interface requires
more typing, and more mental record-keeping, than the pylab interface.
Typing "plot(x,y)" is easier to do and remember than creating a figure,
adding axes, and then typing "ax1.plot(x,y)". For interactive use, I
really don't see any advantage to an OO interface. What advantage do
you see?

In my case, the advantage of pylab over OO is its simplicity. If this simpler usage wasn't available, it would be much harder to teach Matlab-like stuff in introductory programming classes via Python.

At the same time, it is very easy to confuse the difference between the Matlab-like stuff, the OO-like stuff, and then there's also numpy.

I vote for better documentation regarding these pieces, their interactions and their differences, somewhere easy-to-find online.

--b

···

On Jan 10, 2007, at 4:57 PM, Eric Firing wrote:

Eric Firing wrote:

Even without the automatic-redraw difference, the OO interface requires more typing, and more mental record-keeping, than the pylab interface.

Yes, but I don't think that's inherent in an OO interface, it's just that the quickie utilities are missing from the current OO interface.

oh, and it's not mental record keeping -- it's explicit record keeping, which is why I like it:

F1 = OOlab.Figure()
F2 = OOlab.Figure()

Isn't that better than trying to remember which is the current figure?

Typing "plot(x,y)" is easier to do and remember than creating a figure, adding axes, and then typing "ax1.plot(x,y)".

Why couldn't plot(x,y) create and return a figure object? Or an axis object? -- I haven't thought it out too much yet.

> For interactive use, I really don't see any advantage to an OO interface.

Well, for *just* interactive use, I agree, but I see some very large advantages to an OO style for embedding in programs and larger projects. However, I don't want to use one style for one use, and a different style for another.

Part of this might be from my style -- I actually found that I didn't make much use of interactive mode in Matlab -- and I was a heavy Matlab user -- six years of grad school and a dissertation's worth.

As handy as it is to have a command line to play with, if I'm writing more than four or five lines (and I usually am!), I'm happier putting them in a file and running them as a script. Even in that case, I don't mind a little extra typing.

What I'm envisioning for "OOlab" is a set of utility functions that do make some of the pylab stuff easy -- not well thought out yet, but something like:

F = ooLab.figure(1) # I often need to plot more than one figure anyway, so I don't mind having to type that.

ax = F.plot(x,y) # there could be this and subplot

ax.set_title = "A title for the plot"
# or better yet: ax.title = "A title for the plot"
# I'd like to see more properties in MPL too!

ax.grid(True)
.

Whatever.

Note that some of this comes from my love of namespaces -- I really don't like "import*" -- the way that can work is using more OO, so you don't need to type the module name everywhere.

I don't see much advantage to keeping the idea of a "current figure" or "current axis" -- it's just not that hard to keep a reference. Maybe it does help for quickie command line stuff, but I think for even quickie scripts, it's clearer to name your axes, etc.

However, the proof is in the pudding -- what needs to be done is for someone to sit down and start using MPL in interactive/quickie script use without pylab, and write something for OOlab whenever something is harder to do than it should be. Then we'll see how it works out.

I'd like that person to be me, but I actually haven't been using MPL for much lately, and the little I am has been embedded in a wxPython app.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@...259...

Christopher Barker wrote:

Eric Firing wrote:
  

Even without the automatic-redraw difference, the OO interface requires more typing, and more mental record-keeping, than the pylab interface.
    
Yes, but I don't think that's inherent in an OO interface, it's just that the quickie utilities are missing from the current OO interface.

oh, and it's not mental record keeping -- it's explicit record keeping, which is why I like it:

F1 = OOlab.Figure()
F2 = OOlab.Figure()

Isn't that better than trying to remember which is the current figure?

Typing "plot(x,y)" is easier to do and remember than creating a figure, adding axes, and then typing "ax1.plot(x,y)".
    
Why couldn't plot(x,y) create and return a figure object? Or an axis object? -- I haven't thought it out too much yet.
  
Chris: In the pylab interface, figure() returns a figure instance and plot(x,y) returns a list of Line2d instances.

>>> from pylab import *
>>> l = plot([1,2,3])
>>> l
[<matplotlib.lines.Line2D instance at 0x3dcf738>]
>>> f = figure()
>>> f
<matplotlib.figure.Figure instance at 0x3dcf710>
>>>

I guess I agree with Eric, I don't really see much benefit for the OO interface for interactive use.

-Jeff

···

--
Jeffrey S. Whitaker Phone : (303)497-6313
Meteorologist FAX : (303)497-6449
NOAA/OAR/PSD R/PSD1 Email : Jeffrey.S.Whitaker@...259...
325 Broadway Office : Skaggs Research Cntr 1D-124
Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg

Jeff Whitaker wrote:

Chris: In the pylab interface, figure() returns a figure instance and plot(x,y) returns a list of Line2d instances.

yes, but it's the axis instance that you are most likely to need!

- Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@...259...

so use pylab.gca() ?

···

On Jan 11, 2007, at 3:31 PM, Christopher Barker wrote:

Jeff Whitaker wrote:

Chris: In the pylab interface, figure() returns a figure instance and
plot(x,y) returns a list of Line2d instances.

yes, but it's the axis instance that you are most likely to need!

- Chris

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@...259...

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users