4
$\begingroup$

Very similar to

Parameters in plot titles

in which I want to call a parameter from an array using PlotLabel in my plot using Manipulate. I've tried all of the suggestions in the above post and keep getting the same error. Simple code:

f = {1, 2, 3, 4, 5, 6};
Manipulate[
 Plot[Sin[f[[g]] x], {x, -2, 2}, PlotLabel -> Text["f =" f[[g]]]], {g,
   1, 3, 1}]

enter image description here

Curiously, the first f value does not show up incorrectly to the left of the "f=" string. Instead of Text[] I've also tried HoldForm[], TraditionalForm[], Defer[]. I'm not too picky on where "f =" shows up, but this is pretty confusing to me as why this doesn't work as it should. Thanks

$\endgroup$
1
  • $\begingroup$ f = {1, 2, 3, 4, 5, 6}; Manipulate[ Plot[Sin[f[[g]] x], {x, -2, 2}, PlotLabel -> Row[{"f = ", f[[g]]}]], {{g, 1, "index"}, 1, Length[f], 1, Appearance -> "Labeled"} ] $\endgroup$
    – Nasser
    Commented Sep 16, 2014 at 23:44

2 Answers 2

3
$\begingroup$

What you observe is simply because you use a multiplication without noticing it. Basic example:

"f=" 2

(* 2 "f=" *)

If you look at the full form of this output with FullForm[%] you see that it is indeed Times[2, "f="]. Because the terms in a multiplication are re-ordered by Mathematica, you get the wrong result.

The solution is to either use Row like shown by Nasser in the comment

f = {1, 2, 3, 4, 5, 6}; 
Manipulate[  
  Plot[Sin[f[[g]] x], {x, -2, 2}, 
    PlotLabel -> Row[{"f = ", f[[g]]}]],  {{g, 1, "index"}, 1, 3, 1, 
    Appearance -> "Labeled"}  
]

or you create a full string from your content with

Text["f = " <> ToString[f[[g]]]]]
$\endgroup$
1
$\begingroup$

Just another way using v10 StringTemplate

Manipulate[
 Plot[Sin[f[[g]] x], {x, -2, 2}, 
  PlotLabel -> TemplateApply[s, g]], {g, Range[6]}, 
 Initialization :> (f = Range[6]; 
   s = StringTemplate["f=<*f[[`1`]]*>"])]

enter image description here

If you prefer the traditional form of expressions rather than the version in graphics one way (there are almost certainly better):

Manipulate[
 With[{lab = 
    Rasterize[TraditionalForm[TemplateApply[st, g] // DisplayForm], 
     RasterSize -> 100, ImageSize -> 50]}, 
  Plot[Sin[f[[g]] x], {x, -2, 2}, PlotLabel -> lab]], {g, Range[6], 
  SetterBar}, 
 Initialization :> (f = Range[6]; 
   st = StringTemplate["f=<*f[[`1`]]*>", CombinerFunction -> RowBox])]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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