I am studying an infinite square well in the context of quantum mechanics.


(* Length of the well *)
L = 1;

(* The eigenfunctions, n=1,2,3,... *)
u[n_, x_] := Sqrt[2/L] Sin[n π x / L]

(* Probability density function, ρ[x] = |u[n,x]|^2 *)
ρ[n_, x_] := Simplify[u[n, x]\[Conjugate] u[n, x], {n ∈ Integers, x ∈ Reals}]

(* Plots of ρ[n,x] for various values of n *)
Table[ Plot[ρ[n, x], {x, 0, L}, PlotLabel -> "ρ[" <> ToString@n <> ",x]"], {n, 1, 4}]

And here is the output:

enter image description here

My questions is:

Can I use Mathematica pattern magic to rewrite PlotLabel -> "ρ[" <> ToString@n <> ",x]" ?

For instance, I tried (although I knew it wouldn't work): PlotLabel -> HoldForm@ρ[n,x], but that of course leaves n unevaluated as well, although I would like n to be evaluated and then the result to be held.


4 Answers 4


I think your original method is fine, but perhaps this will be more to your liking:

Table[With[{n = ni},
  Plot[ρ[n, x], {x, 0, L}, PlotLabel -> Defer@ρ[n, x]]
  ], {ni, 1, 4}]

Another, perhaps less fundamental way is

    Plot[ρ[n, x], {x, 0, L}, PlotLegends -> Placed["Expressions", Top]]
 ,{n, 1, 4}]

Example image

  • 2
    $\begingroup$ Would be ok to use HoldForm[] instead of Defer[] here ? I tried it and it works, but are there any caveats? $\endgroup$
    – stathisk
    Commented Nov 6, 2013 at 23:29
  • 1
    $\begingroup$ @Zet Yes, of course, HoldForm is probably even safer as Defer gets evaluated when given explicitly as input. $\endgroup$
    – Ajasja
    Commented Nov 6, 2013 at 23:36

This is probably more Mathematica-ish. But sincerely, I would go with your solution :D

Table[Plot[ReleaseHold@# /. n -> r, {x, 0, L}, 
           PlotLabel -> (# /. n -> r)] &@HoldForm[ρ[n, x]], {r, 1, 4}]

Mathematica graphics

  • $\begingroup$ Next time I will be very careful on what I'm wishing :P Thanks for the link and the solution ! $\endgroup$
    – stathisk
    Commented Nov 6, 2013 at 23:13
  • $\begingroup$ I don't understand the Belisarius Superfluous Comma trick. It produces an error on my machine... $\endgroup$
    – cormullion
    Commented Nov 8, 2013 at 11:44
  • $\begingroup$ @cormullion Thanks, I did several mistakes in the previous version. It worked here, but probably only because some dirty previous definitions or a simple copy-paste error. $\endgroup$ Commented Nov 8, 2013 at 12:18
  • $\begingroup$ @belisarius Damn, I thought I had spotted another devious trick... $\endgroup$
    – cormullion
    Commented Nov 8, 2013 at 12:20
  • $\begingroup$ @cormullion What you've spotted is called stupidity by the locals here $\endgroup$ Commented Nov 8, 2013 at 12:22

I'm always a fan of making a new function for plotting any time I'm going to do the same type of plot multiple times.

\[Rho]plot[n_, plotopts : OptionsPattern[]] := 
 Plot[\[Rho][n, x], {x, 0, L}, 
  Evaluate[FilterRules[{plotopts}, Options[Plot]]], 
  PlotLabel -> "\[Rho][" <> IntegerString[n] <> ",x]"]

Now you can do a simple plot like


and get

enter image description here

or you can add any options to it to fancy it up:

  Table[\[Rho]plot[n, ImageSize -> 400, BaseStyle -> 20, 
    Frame -> True], {n, 4}]
  , 2]]

enter image description here


To my simple mind all the approaches suggested so far are overkill. I would do it the simple way -- I would use Row.

Table[Plot[\[Rho][n, x], {x, 0, L},
   PlotLabel -> Row[{"ρ[", n, ",\[ThinSpace]x]"}]], {n, 1, 4}]

Please take note of the use of a thin-space glyph. It is needed to make the spacing look right.



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.