Update legend when lines change color?

I have an application where the user can edit line colors and other attributes after the plot is drawn. The artists update just fine but the legend doesn't change.

From what I can see in the legend code, it doesn't seem like there is any mechanism in place for doing this. Does anyone have any ideas on the best way to implement something like this?

Here is a simplified script to show the issue:

import pylab as p
p.ion()
l = p.plot( [1,2,3], 'b', label="foo" )
p.legend()

raw_input( "press return..." )
l[0].set_color( "green" )
p.draw()

Thanks,
Ted

If you keep a reference to the Legend object, then you can call its get_lines() method to get a list of Line2D objects corresponding to the objects returned by plot(). You can use the set_color() method on them.
Maybe this is enough if your application is simple enough.

Eric

···

On 2013/08/01 4:23 PM, Drain, Theodore R (392P) wrote:

I have an application where the user can edit line colors and other attributes after the plot is drawn. The artists update just fine but the legend doesn't change.

From what I can see in the legend code, it doesn't seem like there is any mechanism in place for doing this. Does anyone have any ideas on the best way to implement something like this?

Here is a simplified script to show the issue:

import pylab as p
p.ion()
l = p.plot( [1,2,3], 'b', label="foo" )
p.legend()

raw_input( "press return..." )
l[0].set_color( "green" )
p.draw()

Thanks,
Ted

Thanks - we'll look into that. We might also see how hard it would be to implement an update or refresh method on the legend that could be called when the lines change to keep the legend in sync. Seems like the legend should own that functionality since it set up the mapping between the lines and what it's displaying...

···

________________________________________
From: Eric Firing [efiring@...202...]
Sent: Thursday, August 01, 2013 8:37 PM
To: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] Update legend when lines change color?

On 2013/08/01 4:23 PM, Drain, Theodore R (392P) wrote:

I have an application where the user can edit line colors and other attributes after the plot is drawn. The artists update just fine but the legend doesn't change.

From what I can see in the legend code, it doesn't seem like there is any mechanism in place for doing this. Does anyone have any ideas on the best way to implement something like this?

Here is a simplified script to show the issue:

import pylab as p
p.ion()
l = p.plot( [1,2,3], 'b', label="foo" )
p.legend()

raw_input( "press return..." )
l[0].set_color( "green" )
p.draw()

Thanks,
Ted

If you keep a reference to the Legend object, then you can call its
get_lines() method to get a list of Line2D objects corresponding to the
objects returned by plot(). You can use the set_color() method on them.
Maybe this is enough if your application is simple enough.

Eric

------------------------------------------------------------------------------
Get your SQL database under version control now!
Version control is standard for application code, but databases havent
caught up. So what steps can you take to put your SQL databases under
version control? Why should you start doing it? Read more to find out.
http://pubads.g.doubleclick.net/gampad/clk?id=49501711&iu=/4140/ostg.clktrk
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

I agree entirely. It would be logical for the legend to either have a manual refresh method, or perhaps to be coupled to its targets the way a colorbar is coupled to its mappable, tracking it automatically. The Legend is a very complex beast, however, so I suspect this is a real project.

···

On 2013/08/02 8:55 AM, Drain, Theodore R (392P) wrote:

Thanks - we'll look into that. We might also see how hard it would
be to implement an update or refresh method on the legend that could
be called when the lines change to keep the legend in sync. Seems
like the legend should own that functionality since it set up the
mapping between the lines and what it's displaying...

Mike and I discussed this at the recent conference. What makes
ScalarMappable different from other artists is that it has attribute
caching and it has callback mechanisms for changes to certain attributes.
This is why colorbar can change with its image. These features needs to be
better generalized and cleaned up, and then applied to *all* attributes.

Another issue is that the artist objects contained in the legend are
created from the artist objects that it represents -- at the time of legend
creation. All color, linestyle marker, etc. attributes are copied rather
than referenced in the artists in the legend. Therefore, any changes to
either doesn't impact the other, unfortunately.

So, there are two approaches to solve this. 1) implement a generic,
efficient callback mechanism for attributes (and proper caching to reduce
unneeded dispatches) and have the legend object register callbacks for any
changes in the legend entries. 2) have a mechanism for some sort of shared
attributes.

Just thinking out loud... wishing I had the time to actually implement my
ideas...

Cheers!
Ben Root

···

On Fri, Aug 2, 2013 at 3:32 PM, Eric Firing <efiring@...202...> wrote:

On 2013/08/02 8:55 AM, Drain, Theodore R (392P) wrote:
> Thanks - we'll look into that. We might also see how hard it would
> be to implement an update or refresh method on the legend that could
> be called when the lines change to keep the legend in sync. Seems
> like the legend should own that functionality since it set up the
> mapping between the lines and what it's displaying...

I agree entirely. It would be logical for the legend to either have a
manual refresh method, or perhaps to be coupled to its targets the way a
colorbar is coupled to its mappable, tracking it automatically. The
Legend is a very complex beast, however, so I suspect this is a real
project.