# Using PlotLegends->“Expressions” inside Manipulate or DynamicModule doesn't work

Take this simple example:

Manipulate[
RegionPlot[
{x^2 + y^2 <= c,
x < 0},
{x, -Pi, Pi},
{y, -Pi, Pi},
PlotLegends -> "Expressions"
],
{{c, 3.}, 0., 10., 0.1}
]


As you can see (unless it's just me of course) the legend text does not display correctly. In particular the value of c is not substituted correctly when the legend text is evaluated.

Doing some research I found that Manipulate basically works like a DynamicModule, and indeed the following breaks in a very similar way, and I guess for the same reasons:

DynamicModule[{c = 3},
RegionPlot[
{x^2 + y^2 <= c,
x < 0},
{x, -Pi, Pi},
{y, -Pi, Pi},
PlotLegends -> "Expressions"
]
]


while using for example With instead of DynamicModule works correctly.

I could probably fix this by manually inserting the legend with something like

PlotLegends->"x^2+y^2<="<>c


but then I would have to manually tweak the font and all that.

Why does this happen? And what is the correct way to deal with it?

• @Kuba where should I put that? If you mean it as replacement of "Expressions" it doesn't seem to work for me. The x is substituted with the value 4 and the x<0 part is not shown. – glS May 17 '15 at 10:40
• Manipulate[ RegionPlot[ Evaluate@{x^2 + y^2 <= c, x < 0}, {x, -Pi, Pi}, {y, -Pi, Pi}, PlotLegends -> "Expressions"], {{c, 3.}, 0., 10., 0.1}] – Kuba May 17 '15 at 10:55
• I'm not sure but I think I saw similar quesition here. – Kuba May 17 '15 at 10:55
• @Kuba It did work after I evaluated a ClearAll[x]. It seems that with the Evaluate the Manipulate doesn't work unless xand y have not previous values, which makes sense given what Evaluate does. Unfortunately I cannot fix this other issue (or annoyance if you want) without wrapping it all into a DynamicModule or such, with which the previous issue returns. – glS May 17 '15 at 11:00
• @Kuba not sure what you mean. In the legend text I want c to be substituted with the value currently given by the manipulate, which is correctly done with your solution. The problem with it is that if x or y have some previously assigned value that value is immediately substituted in the Evaluate and the plot is consequently broken. Wrapping the expression in a Module almost fixes this, except for the x being then written as x\$21438. – glS May 17 '15 at 11:43

This happens because legend items are wrapped in HoldForm to prevent the elements from taking on values: Observe:

c = 5;

RegionPlot[{x^2 + y^2 <= c, x < 0}, {x, -Pi, Pi}, {y, -Pi, Pi},
PlotLegends -> "Expressions"][[2, 1, 2]] // InputForm

{HoldForm[x^2 + y^2 <= c], HoldForm[x < 0]}


Without the HoldForm any global values (such as c = 5) would be substituted. Most of the time that would be a bad thing.

To get the behavior you want you just need to replace c within the RegionPlot before it is evaluated. This is the same problem as Function in Table and the same methods can be used, e.g. the Accepted answer using With:

Manipulate[
With[{c = c},
RegionPlot[{x^2 + y^2 <= c, x < 0}, {x, -Pi, Pi}, {y, -Pi, Pi},
PlotLegends -> "Expressions"]
],
{{c, 3.}, 0., 10., 0.1}
]

• Perfect. I saw that With[{c=c} trick in the documentation while trying to understand the workings of Manipulate and Dynamic but didn't really think of applying it here. Thank you. – glS May 17 '15 at 12:07