5
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ @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. $\endgroup$ – glS May 17 '15 at 10:40
  • $\begingroup$ Manipulate[ RegionPlot[ Evaluate@{x^2 + y^2 <= c, x < 0}, {x, -Pi, Pi}, {y, -Pi, Pi}, PlotLegends -> "Expressions"], {{c, 3.}, 0., 10., 0.1}] $\endgroup$ – Kuba May 17 '15 at 10:55
  • $\begingroup$ I'm not sure but I think I saw similar quesition here. $\endgroup$ – Kuba May 17 '15 at 10:55
  • $\begingroup$ @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. $\endgroup$ – glS May 17 '15 at 11:00
  • 1
    $\begingroup$ @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. $\endgroup$ – glS May 17 '15 at 11:43
4
$\begingroup$

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}
]
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – glS May 17 '15 at 12:07

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.