19

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

rcollyer
  • 33,976
  • 7
  • 92
  • 191
matrix42
  • 6,996
  • 2
  • 26
  • 62

2 Answers2

20

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

rcollyer
  • 33,976
  • 7
  • 92
  • 191
  • 3
    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 – m_goldberg Aug 07 '14 at 21:01
  • @m_goldberg yes, and that has been reported as well. – rcollyer Aug 07 '14 at 21:03
  • @m_goldberg I fleshed it out a hair, as it felt incomplete. – rcollyer Aug 07 '14 at 21:12
  • 4
    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. – Jens Aug 08 '14 at 07:50
4

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.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257