# Parameters in plot titles

My issue is how to mix strings with variables, specifically in the context of PlotLabel:

I can put a string as my plot title, and I can also put a variable as a plot title, but I can't put a string and a variable in my plot title. Example:

testplot[a_] := Plot[a x^2, {x, 0, 3}, PlotLabel -> Style[a,20]]
testplot[1]


But what I really want is the plot label to be "a=1". I've tried:

testplot2[a_] := Plot[a x^2, {x, 0, 3}, PlotLabel -> Text["a="a]
testplot2[1]


Which obviously doesn't work as intended. How to do it?

-
What about PlotLabel->("a="<>ToString[a])? –  celtschk Oct 6 '12 at 8:58
Yep. Thanks. :) –  Ooku Oct 6 '12 at 8:59
Possible duplicates; I'll let others decide which is closer: (1) (2) –  Mr.Wizard Oct 6 '12 at 9:19

I'd swear this is a duplicate but I cannot find an exact match. For now:

testplot2[a_] := Plot[a x^2, {x, 0, 3}, PlotLabel -> HoldForm["a" = a]]

testplot2[1]


testplot2[a_] := Plot[a x^2, {x, 0, 3}, PlotLabel -> Row@{"a = ", a}]

testplot2[2]


-
I've been using Row inside FrameLabel to make titles, for example: FrameLabel -> {"x", "y", Row[ ...]}. Now with Mathematica 9, the plot doesn't show the bit referring to Row. Does someone know how to fix this? –  Valerio Jan 9 '13 at 8:59
@Valerio I don't have version 9 so I cannot experiment. You may be able to use ToString, e.g. ToString[ Row[...], TraditionalForm]. If that doesn't work (or perhaps even if it does) I suggest posting a new question specifically about this issue. –  Mr.Wizard Jan 9 '13 at 9:39
That doesn't work, thanks the same. I'll now post a question. –  Valerio Jan 9 '13 at 10:55

StringForm[] (a function that seems to me to not get much love from most users) can of course be used for this case:

testplot[a_] := Plot[a x^2, {x, 0, 3}, PlotLabel -> StringForm["a=1", a]]

testplot[2]


-
I've used StringForm for other things but I simply don't see the advantage here. Both HoldForm and Row are shorter, and to my eye cleaner. Can you give an example where the converse is true? –  Mr.Wizard Apr 5 '13 at 6:05
"Cleaner" is indeed subjective. I'm used to being able to define "generic" strings with slots/placeholders and then using a function to for embedding values in the strings. Also, Row[] is a relatively new function which I haven't really found to be more useful than the routes I am accustomed to. –  Ｊ. Ｍ. Apr 5 '13 at 6:13

I used to use the StringForm / StringJoin varieties shown above ... but I think that a neater way is now to use Defer (since v6). This also has the advantage that it will generate your plot label in fancy TraditionalForm (unlike the other solutions posted here) ... because it is not a string:

testplot[a_, i_] := Plot[i x^2, {x, 0, 3}, PlotLabel -> Defer[a = i]]


Example:

testplot[b, 3]


================================================================================= Update:

In a helpful comment below, @Mr.Wizard suggests that it may be better to use HoldForm than Defer, because if an end-user evaluates the plot as input, the Defer will get activated. Just to clarify, if our plot is AA = Plot[blah …], then Show[AA] will NOT activate the Defer. But it will happen if the user inadvertently (or otherwise) places the cursor in the plot output in the front-end, and then evaluates that graphical output as input … which could possibly happen by mistake or error.

Taking on the advice of MrWizard, I proceeded to change my code to HoldForm. MUCH TO MY SURPRISE … HoldForm caused MORE assignment problems than Defer did. For example, starting from a fresh kernel:

lis = {0, 1, 2};
Plot[Evaluate[x+lis], {x,0,1},  PlotLegends -> Map[TraditionalForm[HoldForm[p = #]]&, lis]]


produces a nice Plot with appropriate legends, but then … shock --- horror … if you then evaluate p, look what has happened to it using HoldForm:

p
2

Clear[p];


OUCH! This does not happen with Defer. This must be some sort of bug. The TraditionalForm is needed to make the labels present 'nicely'. One way to work around this apparent bug is to wrap the TraditionalForm just around the p:

Plot[Evaluate[x+lis], {x,0,1},   PlotLegends -> Map[HoldForm[TraditionalForm[p] = #]&, lis]]


and then p is not evaluated, and it works correctly. [Obviously, I am avoiding the String versions because TraditionalForm output is desired.]

-
I like terse syntax, and Defer is certainly short, but I think it is misapplied here. You create an actual assignment to b and the output changes if you evaluate the plot as input, as that's exactly what Defer is supposed to do. It's better to use HoldForm which keeps the argument unevaluated even when it is used as input. Also, HoldForm[a = i] will render in TraditionalForm but I interpreted the question as explicitly wanting a text-like rendering. –  Mr.Wizard Apr 5 '13 at 6:10
Thanks @Mr.Wizard for the suggestion ... please see added notes above. –  wolfies Apr 6 '13 at 8:16
Interesting. I'm assuming you're using v9 PlotLegends, which I don't have. Using PlotLabel instead I don't need TraditionalForm (it is done automatically) and I also don't have this bug (with or without TraditionalForm). I suggest you post a new question about this as a record of the bug. –  Mr.Wizard Apr 6 '13 at 8:26