Using legend with a stem plot

Hi:

I want to add a legend to a stem plot with two plots. The basic code is:

···

###########################
from pylab import *

x = [1,2,3,4,5]
y1 = [1, 2, 3, 4, 5]
y2 = [5, 4, 3, 2, 1]

subplot(211)
plot(x, y1, 'rx-')
plot(x, y2, 'bx-')
legend(('a', 'b'))

subplot(212)
stem(x, y1, 'r')
stem(x,y2, 'b')
legend(('a', 'b'))

show()
###########################

The left subplot is the same plot using plot instead of stem. You can
see the result here: http://imagebin.org/128376 .

I expect the legend for the stem case to look similar to the plot
case, however, for the stem, I get the two labels of the legend
associated with the first stem command: 'a' linked to the marker and
'b' linked to the stem line.

Is there any way to fix this? Is this a bug?

Alejandro.

Alejandro,

I apologize for how long it has taken to get back to you. I can confirm your bug, and it is indeed a bug. However, I am not sure how exactly it should be dealt with. To prevent it from getting lost, could you please file a report on our bug tracker?

http://sourceforge.net/tracker/?group_id=80706

That would be greatly appreciated.

Ben Root

P.S. - On a unrelated note, it is not good coding style to do a “from pylab import *” as pylab brings in many functions and variables into the main namespace and will likely collide with one of your own variables and/or functions.

···

On Fri, Dec 17, 2010 at 4:03 PM, Alejandro Weinstein <alejandro.weinstein@…287…> wrote:

Hi:

I want to add a legend to a stem plot with two plots. The basic code is:

###########################

from pylab import *

x = [1,2,3,4,5]

y1 = [1, 2, 3, 4, 5]

y2 = [5, 4, 3, 2, 1]

subplot(211)

plot(x, y1, ‘rx-’)

plot(x, y2, ‘bx-’)

legend((‘a’, ‘b’))

subplot(212)

stem(x, y1, ‘r’)

stem(x,y2, ‘b’)

legend((‘a’, ‘b’))

show()

###########################

The left subplot is the same plot using plot instead of stem. You can

see the result here: http://imagebin.org/128376 .

I expect the legend for the stem case to look similar to the plot

case, however, for the stem, I get the two labels of the legend

associated with the first stem command: ‘a’ linked to the marker and

‘b’ linked to the stem line.

Is there any way to fix this? Is this a bug?

Alejandro.

I think this should be more like a feature request (rather than a
bug). Legend only support simple artists such as
lines/patches/collections. This is mentioned in the documentation
(http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.legend)
but may be not explicit enough.

Implementing legend for stem command is not very straight forward as
command like stem create multiple artists. I prefer commands like stem
to return a single container artist, but this has potential to break
the old code.

For the original question, you may use proxy artist
(http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist).
However, I'm afraid none of the currently supported artists is close
to what you want. And it is not easy (for a normal user) to create a
customized legend unfortunately.

Regards,

-JJ

···

On Thu, Jan 6, 2011 at 1:24 PM, Benjamin Root <ben.root@...1304...> wrote:

I apologize for how long it has taken to get back to you. I can confirm
your bug, and it is indeed a bug. However, I am not sure how exactly it
should be dealt with. To prevent it from getting lost, could you please
file a report on our bug tracker?

Done: https://sourceforge.net/tracker/?func=detail&aid=3152447&group_id=80706&atid=560720

I think the right behavior should mimic what Matlab does. I added the
plot generated by Matlab in the bug report.

Alejandro.

···

On Wed, Jan 5, 2011 at 9:24 PM, Benjamin Root <ben.root@...1304...> wrote:

To prevent it from getting lost, could you please
file a report on our bug tracker?

I think this should be more like a feature request (rather than a
bug). Legend only support simple artists such as
lines/patches/collections.

I disagree about this not being a bug. I understand that it can be
difficult to implement, however legend produce the wrong result.

This is mentioned in the documentation
(http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.legend)
but may be not explicit enough.

I couldn't find any reference about this in the documentation.

Alejandro.

···

On Thu, Jan 6, 2011 at 7:20 AM, Jae-Joon Lee <lee.j.joon@...287...> wrote: