Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 9 down vote accepted

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

share|improve this answer
Would be ok to use HoldForm[] instead of Defer[] here ? I tried it and it works, but are there any caveats? – Zet Nov 6 '13 at 23:29
@Zet Yes, of course, HoldForm is probably even safer as Defer gets evaluated when given explicitly as input. – Ajasja Nov 6 '13 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

share|improve this answer
Next time I will be very careful on what I'm wishing :P Thanks for the link and the solution ! – Zet Nov 6 '13 at 23:13
I don't understand the Belisarius Superfluous Comma trick. It produces an error on my machine... – cormullion Nov 8 '13 at 11:44
@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. – Dr. belisarius Nov 8 '13 at 12:18
@belisarius Damn, I thought I had spotted another devious trick... – cormullion Nov 8 '13 at 12:20
@cormullion What you've spotted is called stupidity by the locals here – Dr. belisarius Nov 8 '13 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

share|improve this answer

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.


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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