How Clear Points Without Wiping Everything?

Hello-

Scenario:

Basemap used to display the East Coast of the US and the Atlantic Ocean.
Shapelib is used read a shapefile the contents of is pumped into a PyPlot subplot hosted Line Collection which overlays the ocean with a grid
PyPlot text is used to label each grid with it's designator.

What I want to do is plot a collection of points, save the result as a PNG, clear the first set of points, plot another collection of points, save the result, and so on. The problem is the if I use the Pyplot clf function it wipes everything previously built.

What do I need to do in order to clear just the points without clearing everything?

Thank you.

Jim

Hello-

Scenario:

Basemap used to display the East Coast of the US and the Atlantic Ocean.

Shapelib is used read a shapefile the contents of is pumped into a PyPlot

subplot hosted Line Collection which overlays the ocean with a grid

PyPlot text is used to label each grid with it’s designator.

What I want to do is plot a collection of points, save the result as a PNG,

clear the first set of points, plot another collection of points, save the

result, and so on. The problem is the if I use the Pyplot clf function it wipes

everything previously built.

What do I need to do in order to clear just the points without clearing everything?

Thank you.

Jim

Jim,

Sorry for the delay. Most plotting functions in matplotlib returns a Collection object. These objects have a member function “remove()”.

import matplotlib.pyplot as plt

plt.ion()
plt.plot([0, 1, 2, 3, 4], [4, 3, 2, 1, 0])
pts = plt.scatter([1, 2, 3], [1, 2, 3])
plt.show() # You see three points and a line
res.remove()

plt.show() # Now you see only the line

I hope that helps!
Ben Root

···

On Wed, Feb 1, 2012 at 4:07 PM, Jim St.Cyr <jim.stcyr@…287…> wrote:

`Ben-

  Very helpful.  A question, how do you determine the object

designator? In your example above, res.remove(), where did the
‘res’ come from?

  Jim

`
···

On 2/2/2012 3:41 PM, Benjamin Root wrote:

    On Wed, Feb 1, 2012 at 4:07 PM, Jim > St.Cyr <jim.stcyr@...287...> >         wrote:

Hello-

      Scenario:



      Basemap used to display the East Coast of the US and the

Atlantic Ocean.

      Shapelib is used read a shapefile the contents of is pumped

into a PyPlot

      subplot hosted Line Collection which overlays the ocean with a

grid

      PyPlot text is used to label each grid with it's designator.



      What I want to do is plot a collection of points, save the

result as a PNG,

      clear the first set of points, plot another collection of

points, save the

      result, and so on.  The problem is the if I use the Pyplot clf

function it wipes

      everything previously built.



      What do I need to do in order to clear just the points without

clearing everything?

      Thank you.



      Jim
      Jim,



      Sorry for the delay.  Most plotting functions in matplotlib

returns a Collection object. These objects have a member
function “remove()”.

      >>> import matplotlib.pyplot as plt

      >>> plt.ion()

      >>> plt.plot([0, 1, 2, 3, 4], [4, 3, 2, 1, 0])

      >>> pts = plt.scatter([1, 2, 3], [1, 2, 3])

      >>> plt.show()    # You see three points and a line

      >>> res.remove()

      >>> plt.show()   # Now you see only the line





      I hope that helps!

      Ben Root

It was assigned when I called scatter(). Just about any mpl plotting function (plot(), scatter(), hist(), etc.) returns an object. Most of the time, users do not save the result into a variable, but if you want to do advanced tricks, you will need to save those returns.

Ben Root

···

On Thu, Feb 2, 2012 at 2:54 PM, Jim St.Cyr <jim.stcyr@…287…> wrote:

On 2/2/2012 3:41 PM, Benjamin Root wrote:

    On Wed, Feb 1, 2012 at 4:07 PM, Jim > > St.Cyr <jim.stcyr@...287...> > >         wrote:

Hello-

      Scenario:



      Basemap used to display the East Coast of the US and the

Atlantic Ocean.

      Shapelib is used read a shapefile the contents of is pumped

into a PyPlot

      subplot hosted Line Collection which overlays the ocean with a

grid

      PyPlot text is used to label each grid with it's designator.



      What I want to do is plot a collection of points, save the

result as a PNG,

      clear the first set of points, plot another collection of

points, save the

      result, and so on.  The problem is the if I use the Pyplot clf

function it wipes

      everything previously built.



      What do I need to do in order to clear just the points without

clearing everything?

      Thank you.



      Jim
      Jim,



      Sorry for the delay.  Most plotting functions in matplotlib

returns a Collection object. These objects have a member
function “remove()”.

      >>> import matplotlib.pyplot as plt

      >>> plt.ion()

      >>> plt.plot([0, 1, 2, 3, 4], [4, 3, 2, 1, 0])

      >>> pts = plt.scatter([1, 2, 3], [1, 2, 3])

      >>> plt.show()    # You see three points and a line

      >>> res.remove()

      >>> plt.show()   # Now you see only the line





      I hope that helps!

      Ben Root

`Ben-

  Very helpful.  A question, how do you determine the object

designator? In your example above, res.remove(), where did the
‘res’ come from?

  Jim

`

Note there is a typo. Ben assigned the output to “pts” but then referenced “res”.

···

On Thu, Feb 2, 2012 at 12:59 PM, Benjamin Root <ben.root@…1304…> wrote:

On Thu, Feb 2, 2012 at 2:54 PM, Jim St.Cyr <jim.stcyr@…287…> wrote:

On 2/2/2012 3:41 PM, Benjamin Root wrote:

    On Wed, Feb 1, 2012 at 4:07 PM, Jim > > > St.Cyr <jim.stcyr@...287...> > > >         wrote:

Hello-

      Scenario:



      Basemap used to display the East Coast of the US and the

Atlantic Ocean.

      Shapelib is used read a shapefile the contents of is pumped

into a PyPlot

      subplot hosted Line Collection which overlays the ocean with a

grid

      PyPlot text is used to label each grid with it's designator.



      What I want to do is plot a collection of points, save the

result as a PNG,

      clear the first set of points, plot another collection of

points, save the

      result, and so on.  The problem is the if I use the Pyplot clf

function it wipes

      everything previously built.



      What do I need to do in order to clear just the points without

clearing everything?

      Thank you.



      Jim
      Jim,



      Sorry for the delay.  Most plotting functions in matplotlib

returns a Collection object. These objects have a member
function “remove()”.

      >>> import matplotlib.pyplot as plt

      >>> plt.ion()

      >>> plt.plot([0, 1, 2, 3, 4], [4, 3, 2, 1, 0])

      >>> pts = plt.scatter([1, 2, 3], [1, 2, 3])

      >>> plt.show()    # You see three points and a line

      >>> res.remove()

      >>> plt.show()   # Now you see only the line





      I hope that helps!

      Ben Root

`Ben-

  Very helpful.  A question, how do you determine the object

designator? In your example above, res.remove(), where did the
‘res’ come from?

  Jim

`

It was assigned when I called scatter(). Just about any mpl plotting function (plot(), scatter(), hist(), etc.) returns an object. Most of the time, users do not save the result into a variable, but if you want to do advanced tricks, you will need to save those returns.

Ben Root


Keep Your Developer Skills Current with LearnDevNow!

The most comprehensive online learning library for Microsoft developers

is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,

Metro Style Apps, more. Free future releases when you subscribe now!

http://p.sf.net/sfu/learndevnow-d2d


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Good catch!

Ben Root

···

On Thu, Feb 2, 2012 at 3:04 PM, G Jones <glenn.caltech@…2015…87…> wrote:

Note there is a typo. Ben assigned the output to “pts” but then referenced “res”.

Benjamin Root :

Just about any mpl plotting function (plot(), scatter(), hist(), etc.) returns an object. Most of the time, users do not save the result into a variable, but if you want to do advanced tricks, you will need to save those returns.

Sorry for a shameless attempt to add something to this, but actually here you don't need it, these collections are accessible through the current axes:

plt.plot([0, 1, 2, 3, 4], [4, 3, 2, 1, 0])
pts = plt.scatter([1, 2, 3], [1, 2, 3])

ax=plt.gca()
...
del ax.collections[:]

(Or, say, del ax.lines[:] to remove the first line ; I do it often when I plot a solution of a differential equation, a trajectory, keeping just a few last segments).

···

==

This reminds me a nuisance... Under Windows XP, ion() is not too compatible with show().
TKAgg (by default), WXAgg and GTKAgg bomb Bens program (and without draw() nothing is plotted).

Jerzy Karczmarczuk

Benjamin Root :

Just about any mpl plotting function (plot(), scatter(), hist(), etc.)

returns an object. Most of the time, users do not save the result

into a variable, but if you want to do advanced tricks, you will need

to save those returns.

Sorry for a shameless attempt to add something to this, but actually

here you don’t need it, these collections are accessible through the

current axes:

plt.plot([0, 1, 2, 3, 4], [4, 3, 2, 1, 0])

pts = plt.scatter([1, 2, 3], [1, 2, 3])

ax=plt.gca()

del ax.collections[:]

I forget if this approach is recommended or not. There are methods for ax that properly handle removal of types of artists that have been attached to an axes. The above approach assumes that no other collections have been plotted that you wanted to keep. The approach I gave is a very surgical method that makes sure that only what is supposed to be removed gets removed. Both are valid, and their usefulness depends upon which view of the data you need (remove types of artists versus removing particular artists).

This reminds me a nuisance… Under Windows XP, ion() is not too

compatible with show().

TKAgg (by default), WXAgg and GTKAgg bomb Bens program (and without

draw() nothing is plotted).

That would be a bug and should be reported (assuming that it is in the latest version). Make sure that you are using at least v1.0.1 (preferably v1.1.0) to make sure that show() should do what you want. Any version earlier than v1.0.1 is very unpredictable with respect to multiple show() calls.

Ben Root

···

On Thu, Feb 2, 2012 at 3:52 PM, Jerzy Karczmarczuk <jerzy.karczmarczuk@…3937…> wrote:

Benjamin Root on my suggestion that one can use del instead of
remove :

    There are methods for ax that properly handle removal of

types of artists that have been attached to an axes. The above
approach assumes that no other collections have been plotted
that you wanted to keep. The approach I gave is a very surgical
method that makes sure that only what is supposed to be removed
gets removed. Both are valid, and their usefulness depends upon
which view of the data you need (remove types of artists versus
removing particular artists).

Yes, absolutely.

With just a little remark: remove is a Python function which

searches the list for a given value. del uses indices, so

del lst[10000]

will be most probably faster than lst.remove(value), if this value

happens to occur for the first time at index 10000. Unless
Matplotlib uses a specially tuned version of remove.

    Under Windows XP, ion() is not too

    compatible with show().

    TKAgg (by default), WXAgg and GTKAgg bomb Bens program (and

without

    draw() nothing is plotted).
    That would be a bug and should be reported (assuming that it is

in the latest version). Make sure that you are using at least
v1.0.1 (preferably v1.1.0) to make sure that show() should do
what you want. Any version earlier than v1.0.1 is very
unpredictable with respect to multiple show() calls.

I use 1.1.0. (Python 2.7.2) I didn't report any bug because I didn't

know whether this was a bug…

But there are more...

The animation module uses various timers depending on the back-end.

This is – perhaps – the result of the fact that Python standard
Timer (a subclass of Thread) is, to say it mildly, rather weak. So,
under Tk the system uses after(), wx offers its timers, etc. Timed
animation works differently under various backends.

Here:

is a program which simulates/visualizes a simple-minded 2D Ising

model (vectorized Metropolis Monte-Carlo; not exactly physical,
but only mister Nobody is perfect…). It should update the picture
dynamically, and my students can see how the ferromagnetic domains
develop.
When run with Tk, OK. GTK and wx – no. The animation runs, but the
figure is frozen for many seconds, and it is updated when the Slider
is activated. The autonomous refreshing of the image is much much
slower than under TK. I don’ understand what is happening…
Tested on Windows XP, and under Linux (Fedora). I repeat, the
animation runs, this is a problem of refreshing the display.
Thank you, and all the best.
Jerzy Karczmarczuk

···

http://users.info.unicaen.fr/~karczma/TEACH/Test/isingVZ.py

Benjamin Root on my suggestion that one can use del instead of

remove :

    There are methods for ax that properly handle removal of

types of artists that have been attached to an axes. The above
approach assumes that no other collections have been plotted
that you wanted to keep. The approach I gave is a very surgical
method that makes sure that only what is supposed to be removed
gets removed. Both are valid, and their usefulness depends upon
which view of the data you need (remove types of artists versus
removing particular artists).

Yes, absolutely.

With just a little remark: remove is a Python function which

searches the list for a given value. del uses indices, so

del lst[10000]

will be most probably faster than lst.remove(value), if this value

happens to occur for the first time at index 10000. Unless
Matplotlib uses a specially tuned version of remove.

Don’t confuse Python’s remove() method for lists with the remove() method for matplotlib Artist (and subclassed Artists) objects. Collections are a subclass of Artist, and remove() simply means “remove me from the Axes object I was connected to”. This is because an Axes object contains lists to the artists, collections and others that have been attached to it, and each Artist has a reference to the Axes object that it was connected to. So, calling remove() on an Artist will do all the appropriate house-keeping.

matplotlib Artist objects do not implement del(), so deleting an Artist before calling remove() will not properly disconnect all of the callbacks and references. I suspect that the only reason why this works at all is that there is a lot of usage of weakrefs and it so there might be enough error-checking to keep everything from crashing.

Others who are much more familiar with the underlying architecture may be able to comment better. In the meantime, the pedantic, better form of:

del ax.collections[:]

would be:

for item in ax.collections :

item.remove()

del ax.collections[:]

    Under Windows XP, ion() is not too

    compatible with show().

    TKAgg (by default), WXAgg and GTKAgg bomb Bens program (and

without

    draw() nothing is plotted).
    That would be a bug and should be reported (assuming that it is

in the latest version). Make sure that you are using at least
v1.0.1 (preferably v1.1.0) to make sure that show() should do
what you want. Any version earlier than v1.0.1 is very
unpredictable with respect to multiple show() calls.

I use 1.1.0. (Python 2.7.2) I didn't report any bug because I didn't

know whether this was a bug…

Then, please do report it.

But there are more…

The animation module uses various timers depending on the back-end.

This is – perhaps – the result of the fact that Python standard
Timer (a subclass of Thread) is, to say it mildly, rather weak. So,
under Tk the system uses after(), wx offers its timers, etc. Timed
animation works differently under various backends.

This should also be a new thread, and possibly a new bug report as well.

Ben Root

···

On Thu, Feb 2, 2012 at 6:25 PM, Jerzy Karczmarczuk <jerzy.karczmarczuk@…3937…> wrote: