legend() without args?

In matplotlib 0.90.1 the behavior of legend() seems to have changed.

Here's a test code fragment:

···

---------------------------
import pylab
import numpy

y=numpy.arange(-10,10)**2
print y
pylab.plot(y)
pylab.legend()
pylab.show()
---------------------------
Running on python 2.5.1, matplotlib 0.90.0 gives me a reasonable-looking
legend in upper right corner with label "line0".

matplotlib 0.90.1 gives an empty rectangular box, fairly large, right in
the middle of the plot.

Attempting to move the legend with loc=2 or whatever has no effect.
However, giving an explicit label ( pylab.legend(['line1']) ) makes it
start working the same as 0.90.0.

I started looking at the source but don't have time today to understand
how this is supposed to work. Maybe later.

Is this the intended behavior? The announcement for 0.90.1 says:
"Display only meaningful labels when calling legend() without args."
It's not clear to me what this means.

--
                        Jonathan Griffitts
AnyWare Engineering Boulder, CO, USA

This is the result of a change that I committed in between 0.90.0 and
0.90.1 - sorry if it caused confusion...

The idea is exactly what you observed: legend() only displays those
lines that have an explicit label set.

If a certain line in a figure does not have a label, I think it is
rather pointless displaying it in a legend at all. I mean - the legend
is meant to explain the individual lines and the default labels like
"line0" can hardly be counted as "explanation".

Maybe, though, there should be one additional condition:
* If no lines or patchsets have explicit labels, display all defaults.
* If any line or patch has an explicit label, display only the explicit
labels.

This would probably avoid confusion about a completely empty legend.
What do you think?

Greetings,
Norbert

Jonathan Griffitts wrote:

···

In matplotlib 0.90.1 the behavior of legend() seems to have changed.

Here's a test code fragment:
---------------------------
import pylab
import numpy

y=numpy.arange(-10,10)**2
print y
pylab.plot(y)
pylab.legend()
pylab.show()
---------------------------
Running on python 2.5.1, matplotlib 0.90.0 gives me a reasonable-looking
legend in upper right corner with label "line0".

matplotlib 0.90.1 gives an empty rectangular box, fairly large, right in
the middle of the plot.

Attempting to move the legend with loc=2 or whatever has no effect.
However, giving an explicit label ( pylab.legend(['line1']) ) makes it
start working the same as 0.90.0.

I started looking at the source but don't have time today to understand
how this is supposed to work. Maybe later.

Is this the intended behavior? The announcement for 0.90.1 says:
"Display only meaningful labels when calling legend() without args."
It's not clear to me what this means.

Sorry, I notice that there may be need for an additional explanation:

What I call "explicit label" is one that is given as kwarg to the plot
command like

    plot(x,sin(x),label="sin(x)")

My standard use of legends is to give such an explicit label to every
significant line and call legend() without arguments at the end. This
approach is much more comfortable than using arguments to legend itself,
as you do not need to keep track of handles and labels with every change
that you make in the plotting script.

Greetings,
Norbert

Norbert Nemec wrote:

···

This is the result of a change that I committed in between 0.90.0 and
0.90.1 - sorry if it caused confusion...

The idea is exactly what you observed: legend() only displays those
lines that have an explicit label set.

If a certain line in a figure does not have a label, I think it is
rather pointless displaying it in a legend at all. I mean - the legend
is meant to explain the individual lines and the default labels like
"line0" can hardly be counted as "explanation".

Maybe, though, there should be one additional condition:
* If no lines or patchsets have explicit labels, display all defaults.
* If any line or patch has an explicit label, display only the explicit
labels.

This would probably avoid confusion about a completely empty legend.
What do you think?

Greetings,
Norbert

Jonathan Griffitts wrote:
  

In matplotlib 0.90.1 the behavior of legend() seems to have changed.

Here's a test code fragment:
---------------------------
import pylab
import numpy

y=numpy.arange(-10,10)**2
print y
pylab.plot(y)
pylab.legend()
pylab.show()
---------------------------
Running on python 2.5.1, matplotlib 0.90.0 gives me a reasonable-looking
legend in upper right corner with label "line0".

matplotlib 0.90.1 gives an empty rectangular box, fairly large, right in
the middle of the plot.

Attempting to move the legend with loc=2 or whatever has no effect.
However, giving an explicit label ( pylab.legend(['line1']) ) makes it
start working the same as 0.90.0.

I started looking at the source but don't have time today to understand
how this is supposed to work. Maybe later.

Is this the intended behavior? The announcement for 0.90.1 says:
"Display only meaningful labels when calling legend() without args."
It's not clear to me what this means.

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

This works for me in svn. I also think Norbert's idea of suppressing
an auto-legend entry when there is no label is a good one. It is more
elegant and pithy than the '_nolegend_' approach, though a bit less
explicit.

JDH

···

On 7/5/07, Norbert Nemec <Norbert.Nemec.list@...380...> wrote:

Sorry, I notice that there may be need for an additional explanation:

What I call "explicit label" is one that is given as kwarg to the plot
command like

    plot(x,sin(x),label="sin(x)")

In message <468CEBDF.9080803@...380...>, Norbert Nemec

This is the result of a change that I committed in between 0.90.0 and
0.90.1 - sorry if it caused confusion...

The idea is exactly what you observed: legend() only displays those
lines that have an explicit label set.

If a certain line in a figure does not have a label, I think it is
rather pointless displaying it in a legend at all. I mean - the legend
is meant to explain the individual lines and the default labels like
"line0" can hardly be counted as "explanation".

This philosophy makes perfect sense, but it broke down for me in two
ways:

1) I am plotting 6 lines that are already associated with numbers 0
through 5, so the default labels happened to make perfect sense.

2) In this case, I use the legend not to "explain the individual lines"
but to document the line color sequence.

Obviously, this was trivial to fix now that I understand the new
behavior.

I have to say that the present behavior with no args is pretty strange:
planting a blank box on top of the middle of the plot seems worse than
useless. I personally would have preferred that it throw an exception.

Maybe, though, there should be one additional condition:
* If no lines or patchsets have explicit labels, display all defaults.
* If any line or patch has an explicit label, display only the explicit
labels.

I like this. It would avoid puzzling people who try calling legend with
no args, which would likely include people experimenting interactively
in IPython.

···

<Norbert.Nemec.list@...380...> wrote
--
                        Jonathan Griffitts
AnyWare Engineering Boulder, CO, USA