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]]

Test plot

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]

Test plot 2

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

Thanks in advance!

  • 2
    $\begingroup$ What about PlotLabel->("a="<>ToString[a])? $\endgroup$ – celtschk Oct 6 '12 at 8:58
  • $\begingroup$ Yep. Thanks. :) $\endgroup$ – Ooku Oct 6 '12 at 8:59
  • $\begingroup$ Possible duplicates; I'll let others decide which is closer: (1) (2) $\endgroup$ – 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]]


Mathematica graphics

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


Mathematica graphics

  • $\begingroup$ 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? $\endgroup$ – Valerio Jan 9 '13 at 8:59
  • $\begingroup$ @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. $\endgroup$ – Mr.Wizard Jan 9 '13 at 9:39
  • $\begingroup$ That doesn't work, thanks the same. I'll now post a question. $\endgroup$ – Valerio Jan 9 '13 at 10:55
  • $\begingroup$ What if i have to insert multiple parameters? $\endgroup$ – AndreaPaco Feb 27 '17 at 16:56
  • 1
    $\begingroup$ @AndreaPaco Is this what you mean?: testplot3[a_, b_] := Plot[Sin[a + b x], {x, 0, Pi}, PlotLabel -> Row@{HoldForm[Sin["a" + "b" "x"]], " with a = ", a, "; b = ", b}] then testplot3[Pi/4, 3.2] $\endgroup$ – Mr.Wizard Feb 27 '17 at 20:47

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]]


plot with parametrized label

  • $\begingroup$ 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? $\endgroup$ – Mr.Wizard Apr 5 '13 at 6:05
  • $\begingroup$ "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. $\endgroup$ – J. M. will be back soon 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]]


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:



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.]

  • $\begingroup$ 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. $\endgroup$ – Mr.Wizard Apr 5 '13 at 6:10
  • $\begingroup$ Thanks @Mr.Wizard for the suggestion ... please see added notes above. $\endgroup$ – wolfies Apr 6 '13 at 8:16
  • $\begingroup$ 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. $\endgroup$ – Mr.Wizard Apr 6 '13 at 8:26
  • $\begingroup$ Did you ever create a new post (Question) about this bug? It does not appear to affect either version 7 or 10.1 but I think it is important to record nevertheless. $\endgroup$ – Mr.Wizard Oct 7 '15 at 17:13

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.