Ways of updating legends

Hi,
I am re-using a scatter plot in the same figure for interactively displaying
results without ending up with 30 windows open. The legend is relevant, and so
it must be also updated. So far the only way I found was to use the set_label()
method and then using plt.legend(). Is this the only way to get a legend updated
from a label? I am curious, since generally there is the pyplot way and a more
OO way of achieving things, but I could't find it this time.

The snippet below shows what I am using right now:

import numpy as np
import scipy as sp
import matplotlib as mpl
import matplotlib.pyplot as plt

data = np.random.randn(3,10)
fig = plt.figure()
ax = fig.add_subplot(111)
s, = ax.plot([])
ax.axis([0,10,-1,1])
ax.legend([s],[''], loc=0)
for i in range(data.shape[0]):
    s.set_data([np.arange(data.shape[1]),data[i]])
    s.set_label(str(i))
    plt.legend()
    plt.draw()
    plt.ginput(timeout=0)

You're already using "ax.legend", what kind of OO way do you want?

Instead of calling plt.legend, you may do
   ax.legend([s],[str(i)])

Or, if you know what you're doing, you can do

leg = ax.legend([s],[''], loc=0)

and in the for loop,

   leg.texts[0].set_text(str(i))

Regards,

-JJ

···

On Thu, Feb 11, 2010 at 5:56 AM, Jorge Scandaliaris <jorgesmbox-ml@...1664...> wrote:

Hi,
I am re-using a scatter plot in the same figure for interactively displaying
results without ending up with 30 windows open. The legend is relevant, and so
it must be also updated. So far the only way I found was to use the set_label()
method and then using plt.legend(). Is this the only way to get a legend updated
from a label? I am curious, since generally there is the pyplot way and a more
OO way of achieving things, but I could't find it this time.

The snippet below shows what I am using right now:

import numpy as np
import scipy as sp
import matplotlib as mpl
import matplotlib.pyplot as plt

data = np.random.randn(3,10)
fig = plt.figure()
ax = fig.add_subplot(111)
s, = ax.plot([])
ax.axis([0,10,-1,1])
ax.legend([s],[''], loc=0)
for i in range(data.shape[0]):
s.set_data([np.arange(data.shape[1]),data[i]])
s.set_label(str(i))
plt.legend()
plt.draw()
plt.ginput(timeout=0)

------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace,
Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

You're already using "ax.legend", what kind of OO way do you want?

Instead of calling plt.legend, you may do
   ax.legend([s],[str(i)])

Thanks Jae-Joon. You're right. I tried this before, but I must have done
something wrong then, because when I tried the result were new legends
stacked upon the old ones, which didn't look nice when the new legend
was shorter than previous ones.

Or, if you know what you're doing, you can do

leg = ax.legend([s],[''], loc=0)

and in the for loop,

   leg.texts[0].set_text(str(i))

This is even nicer. I tried something similar but somehow overlooked the
texts[0] part, so it didn't work. I'll have to go back to the docs...

Regards,

Jorge

···

El 11/02/10 18:28, Jae-Joon Lee escribió: