remove text from a plot

Matplotlib is great. Between numpy, scipy, and matplotlib, I'm almost completely weaned myself off of Matlab (just need to rewrite a bunch of m-files in Python).

My question is, is there an easy way to remove text that's been added with the text() function from a plot (like if I make a mistake in the text or its position)? I've thought about adding a long string of spaces in the same location, but haven't tried it yet. Is there an easier way like a clear() function or some other method I may have missed?

Thanks,
Aaron

Aaron,

Each text object that you add with the text() function is appended to a list called "texts" that is an attribute of the axes object. So, to remove the last text object you added, you can do:

del gca().texts[-1]
draw()

If what you want to do is change the contents but not the position, you can do:

gca().texts[-1].set_text('Big Mistake')
draw()

If you want to change the position, you can do:

gca().texts[-1].set_position((3,4))
draw()

Note that in the set_position method, unlike the text() function, the position is specified as an (x,y) tuple; the parentheses are required.

Or you can just shift one coordinate:
gca().texts[-1].set_x(5)
draw()

If you want to change the first text object, then of course you would use texts[0] instead of texts[-1], etc.

There is an alternative pylab function for setting properties that takes care of redrawing automatically. Here is an example:

setp(gca().texts[-1], 'fontsize', 15) # Matlab-style
or
setp(gca().texts[-1], fontsize=17) # nicer style

If you are going to want to experiment with the properties of your text object, then grab a reference to it when you create it, like this:

tt = text(6,7,'another one')

setp(tt, fontsize=15)

That way you don't have to keep typing "gca().texts[-1]".

Eric

Aaron Hoover wrote:

ยทยทยท

Matplotlib is great. Between numpy, scipy, and matplotlib, I'm almost completely weaned myself off of Matlab (just need to rewrite a bunch of m-files in Python).

My question is, is there an easy way to remove text that's been added with the text() function from a plot (like if I make a mistake in the text or its position)? I've thought about adding a long string of spaces in the same location, but haven't tried it yet. Is there an easier way like a clear() function or some other method I may have missed?

Thanks,
Aaron

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users