16
$\begingroup$

I don't get a legend when I use the option PlotLegends -> "Expressions" when the first argument to Plot is a single function. It works OK when I give Plot a list of functions.

Test cases:

Plot[Sin[x], {x, 0, 2 Pi}, PlotLegends -> "Expressions"]
ContourPlot[x^2 + y^2 == 1, {x, -2, 2}, {y, -2, 2}, PlotLegends -> "Expressions"]

enter image description here =================================== enter image description here

$\endgroup$
  • $\begingroup$ I can confirm that in V10 PlotLegends fails when Plot is given a single simple argument such as Sin[x], and this worked in V9. I tested on OS X. Looks like a bug to me. $\endgroup$ – m_goldberg Aug 7 '14 at 10:30
  • 1
    $\begingroup$ I have sent a report on this issue to WRI tech support. $\endgroup$ – m_goldberg Aug 7 '14 at 10:44
  • $\begingroup$ @m_goldberg not a bug. I'll explain below. $\endgroup$ – rcollyer Aug 7 '14 at 15:16
  • $\begingroup$ In which version of Windows (7 or 8) is V10 installed? $\endgroup$ – Vaggelis_Z Aug 15 '14 at 8:46
  • $\begingroup$ @Vaggelis_Z I used Windows 8.1. $\endgroup$ – mathe Aug 16 '14 at 1:54
16
$\begingroup$

This is an intentional change to make PlotLegends -> "Expressions" more consistent with PlotLegends -> Automatic. Both now do not produce legends when only one line is present. What you are looking for is PlotLegends -> "AllExpressions" which has the old behavior, e.g.

Plot[x, {x, 0, 1}, PlotLegends -> "AllExpressions"]

enter image description here

More generally, PlotLegends operates based on whether or not two plots are distinguishable. In other words, if there is no displayed difference between two plots, two legends won't be displayed. For example,

Plot[{x, x^2}, {x, 0, 1}, PlotStyle -> Blue, PlotLegends -> "Expressions"]

enter image description here

Contrast this with

Table[
 Plot[{x, x^2}, {x, 0, 1}, PlotStyle -> Blue, PlotLegends -> legend, PlotLabel -> legend],
 {legend, {"AllExpressions", All, "Placeholders", {a, b}}}
]

enter image description here

After the number of distinguishable plots are determined, that gives you the number of legends to expect, once the no single legend rule is accounted for. So, these all should give two legends,

Plot[{x, x^2, x^3}, {x, 0, 1}, PlotStyle -> {Blue, Green}, PlotLegends -> "Expressions"]

enter image description here

Table[
 Plot[{x, x^2, x^3}, {x, 0, 1}, PlotStyle -> {Blue, Green}, 
   PlotLegends -> legend, PlotLabel -> legend],
 {legend, {"AllExpressions", All, "Placeholders", {a, b}}}
]

enter image description here

$\endgroup$
  • 3
    $\begingroup$ Thanks very much for this answer. It is very edifying. It is also an indictment of the V10 documentation of PlotLegends, which mentions none of the new behavior you discuss. It doesn't even mention that PlotLegends has been modified in V10 $\endgroup$ – m_goldberg Aug 7 '14 at 21:01
  • $\begingroup$ @m_goldberg yes, and that has been reported as well. $\endgroup$ – rcollyer Aug 7 '14 at 21:03
  • $\begingroup$ @m_goldberg I fleshed it out a hair, as it felt incomplete. $\endgroup$ – rcollyer Aug 7 '14 at 21:12
  • 3
    $\begingroup$ In my opinion, it would have been better to maintain the old behavior of "Expressions" and instead add a new option "DistinctExpressions" for the behavior that we're now seeing. With the change as you describe it here, users have to go through their notebooks and hand-edit them to replace the old with the new option "AllExpressions" if they want a single-line legend. Seems like an unnecessary hassle. $\endgroup$ – Jens Aug 8 '14 at 7:50
3
$\begingroup$

I received an answer to the report I filed with WRI tech support on this issue. I think there might some interest it what it said, so I quote the relevant part:

There is some change of behavior for PlotLegends->"Expressions". If you want to generate legends for each function, you can use

$\ \ \ \ \ \ $Plot[Sin[x], {x, 0, 2 Pi}, PlotLegends -> "AllExpressions"]

So this is more of an issue of documentation and I have updated the bug report with this case.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.