legend marker update problem

Hi,

I've recently come accross an issue when working on an interactive
marker toggling callback. The problem is illustrated below:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot(range(10), range(10))
# uncomment next line to reproduce bug
# line.set_marker('d')
legend = ax.legend([line], ['foo'])
legend_line, = legend.get_lines()
fig.show()
raw_input('press enter to clear marker')
line.set_marker('')
legend_line.set_marker('')
fig.canvas.draw()
raw_input('press enter to set marker')
line.set_marker('d')
legend_line.set_marker('d')
fig.canvas.draw()
raw_input('press enter to exit')

So when I add a line object to the legend *without marker*, the update
works fine both on data and legend line, but *with marker* it does not
refresh the legend line. I consider it as a bug, please tell me if I'm
doing something wrong.

python 2.7.4 win32
matplotlib 1.2.1

Thanks,
Gregorio

Gregorio,

I experienced a similar issue with trying to change the marker color.

See below the previous response from JJ for accessing the legend marker or using a proxy artist.

-Sterling

···

On Sep 4, 2012, at 5:33PM, Jae-Joon Lee wrote:

On Wed, Sep 5, 2012 at 6:05 AM, Sterling Smith <smithsp@...3304...> wrote:

I still do not get black markers. Furthermore, if you try to make a new legend with the result of leg.get_lines(), you will get lines without markers, which leads me to the conclusion I stated in my previous email (which you did not copy)

I suspect that this is because the legend marker is drawn separately from the legend line to accommodate the numpoints argument of the legend functions. Then the question is how to access these markers if they are separate from the line2d objects in the legend. I didn't even see them in the children of the legend [legend.get_children()].

This is correct. To support legend handle like --o-- (i.e., no markers
at the ends), lines and markers are drawn as a separate artist. You
may use something like,

line[0]._legmarker.set_markerfacecolor('black')
line[1]._legmarker.set_markerfacecolor('black')

I, personally, recommend you to use a proxy artist.

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

For example,

You may do something like

import pylab
pylab.plot(pylab.linspace(0,1,100),marker='o',ls='')
pylab.plot(pylab.linspace(0,1,100),marker='o',ls='-')

# creates artists for legend purpose only
l1, = pylab.plot(pylab.linspace(0,1,100), 'ko-')
l2, = pylab.plot(pylab.linspace(0,1,100), 'ko')
# remove them from the axes.
l1.remove()
l2.remove()

leg=pylab.legend([l1, l2], ["Test 1", "Test 2"], loc='best')

Regards,

-JJ

On May 16, 2013, at 7:25AM, Gregorio Bastardo wrote:

Hi,

I've recently come accross an issue when working on an interactive
marker toggling callback. The problem is illustrated below:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot(range(10), range(10))
# uncomment next line to reproduce bug
# line.set_marker('d')
legend = ax.legend([line], ['foo'])
legend_line, = legend.get_lines()
fig.show()
raw_input('press enter to clear marker')
line.set_marker('')
legend_line.set_marker('')
fig.canvas.draw()
raw_input('press enter to set marker')
line.set_marker('d')
legend_line.set_marker('d')
fig.canvas.draw()
raw_input('press enter to exit')

So when I add a line object to the legend *without marker*, the update
works fine both on data and legend line, but *with marker* it does not
refresh the legend line. I consider it as a bug, please tell me if I'm
doing something wrong.

python 2.7.4 win32
matplotlib 1.2.1

Thanks,
Gregorio

------------------------------------------------------------------------------
AlienVault Unified Security Management (USM) platform delivers complete
security visibility with the essential security capabilities. Easily and
efficiently configure, manage, and operate all of your security controls
from a single console and one unified framework. Download a free trial.
http://p.sf.net/sfu/alienvault_d2d
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Hi Sterling,

Thanks for the hint, using "line._legmarker" attribute solved the problem.

I see the reason behind, however I still consider this as an incorrect
behaviour, since marker toggling works in case the line is originally
added to the legend without marker (so legend line and marker do not
behave like separated). Is it worth reporting on the mpl issue tracker
(or have you done it that time)?

Gregorio

2013/5/16 Sterling Smith <smithsp@...3304...>:

···

Gregorio,

I experienced a similar issue with trying to change the marker color.

See below the previous response from JJ for accessing the legend marker or using a proxy artist.

-Sterling

Gregorio,

I'm glad that helped. I have not reported it on the issue tracker, but your case certainly has more of a bug with it (where it works sometimes, but not always), so I would recommend it.

-Sterling

···

On May 17, 2013, at 2:10AM, Gregorio Bastardo wrote:

Hi Sterling,

Thanks for the hint, using "line._legmarker" attribute solved the problem.

I see the reason behind, however I still consider this as an incorrect
behaviour, since marker toggling works in case the line is originally
added to the legend without marker (so legend line and marker do not
behave like separated). Is it worth reporting on the mpl issue tracker
(or have you done it that time)?

Gregorio

2013/5/16 Sterling Smith <smithsp@...3304...>:

Gregorio,

I experienced a similar issue with trying to change the marker color.

See below the previous response from JJ for accessing the legend marker or using a proxy artist.

-Sterling

Thanks, I opened an issue for it:

2013/5/17 Sterling Smith <smithsp@...3304...>:

···

Gregorio,

I'm glad that helped. I have not reported it on the issue tracker, but your case certainly has more of a bug with it (where it works sometimes, but not always), so I would recommend it.

-Sterling

On May 17, 2013, at 2:10AM, Gregorio Bastardo wrote:

Hi Sterling,

Thanks for the hint, using "line._legmarker" attribute solved the problem.

I see the reason behind, however I still consider this as an incorrect
behaviour, since marker toggling works in case the line is originally
added to the legend without marker (so legend line and marker do not
behave like separated). Is it worth reporting on the mpl issue tracker
(or have you done it that time)?

Gregorio

2013/5/16 Sterling Smith <smithsp@...3304...>:

Gregorio,

I experienced a similar issue with trying to change the marker color.

See below the previous response from JJ for accessing the legend marker or using a proxy artist.

-Sterling