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'd like to plot a function of one real and one integer variable, but I don't want them all shown in the same 2-D plot - I'd like to see them as separate curves so I can see both "axes", more like how Plot3D works. I'm sure Mathematica can do this, but how?

Edit: Rephrased, I'd like to Plot3D a function of one real and one integer variable. Instead of seeing a surface, I'd like a discrete set of curves in three space.

share|improve this question
A rather similar question. – J. M. Feb 7 '12 at 0:21
up vote 20 down vote accepted

Mesh will do the trick:

Plot3D[Sin[x^2 - y], {x, -2, 2}, {y, -3, 3}, MeshFunctions -> {#2 &}, 
 PlotStyle -> None, Mesh -> 30]

surface with mesh

Placing the “wires” on integer values is also easy – see example below. For the range {y, -7, 5} there are 13 integers so you need to ask for 11 wires Mesh -> 11 (in red) because 2 are taken by boundary (blue). With such settings "wires" fall exactly on integer values.

Plot3D[Sin[x^2 - y/2], {x, -2, 2}, {y, -7, 5}, MeshFunctions -> {#2 &}, 
PlotStyle -> None, Mesh -> 11, MeshStyle -> {Thick, Red}, 
BoundaryStyle -> {Thick, Blue}]

surface with mesh on integers

share|improve this answer
If one takes this route with some function f[n, x] where n can only take integer values, use the function f[Round[n], x] within Plot3D[]. – J. M. Feb 7 '12 at 11:03
This is the cleanest solution, thanks! – stopple Feb 7 '12 at 19:57

Do you mean something like this?

f[n_, x_] := Sin[x (1 + n/10)]
ParametricPlot3D[Evaluate[Table[{x, n, f[n, x]}, {n, 20}]], {x, 0, 2 Pi}]

Mathematica graphics

share|improve this answer
Excellent, thanks! – stopple Feb 6 '12 at 19:56
I'm curious to the reason Evaluate is there. Is it required? – BeauGeste Feb 6 '12 at 21:02
@BeauGeste No, it's not required, but it makes the function evaluate faster and the graphs are plotted in different colours. This has to do with the fact that ParametricPlot3D (as well as all other members of the Plot family) has attribute HoldAll which means that without Evaluate the Table expression would be reevaluated for every value of x. With Evaluate the Table is expanded into an explicit list before it is used by ParametricPlot3D making the evaluation much faster. – Heike Feb 6 '12 at 21:11
@BeauGeste you want ParametricPlot3D to see the list of functions, not the Table object. You can also use ParametricPlot3D[#, . . .] & Table[ . . . ] which I prefer. See – Mr.Wizard Feb 6 '12 at 21:12

I'm bored, so here is a recreation of Heike's plot without ParametricPlot3D

      Plot[f[n, x], {x, 0, 2 Pi}],
      Line[x_] :> {ColorData[1][n], Line[{#,n,#2} & @@@ x]},
    {n, 20}
  Axes -> True

Mathematica graphics

share|improve this answer

Once upon a time, old versions of Mathematica had a package called Graphics`Graphics3D`, which featured a neat little utility called StackGraphics[] that did exactly what OP wanted. Since the current versions of Mathematica no longer support this package, we are lucky that the upgrading information in the help file features some code for mimicking the functionality of StackGraphics[], which you can easily adapt to your circumstances:

f[n_Integer, x_] := Sin[x (1 + n/10)];
  Cases[#, Line[L_] :> {ColorData[1][First[#2]], 
      Line[Thread[{L[[All, 1]], First[#2], L[[All, 2]]}]]}, -1] &, 
  Table[Plot[f[n, x], {x, 0, 2 Pi}], {n, 20}]], Axes -> True, 
 ViewPoint -> {.4, -1., .5}]

stacked graphics

Of course, if your Plot[]s use the ColorFunction option, stacking graphics is a bit more complicated, since the output internally uses a GraphicsComplex[] object as opposed to a plain Jane Line[]. In that case, something like the following has to be done:

  Cases[#1, GraphicsComplex[pts_, rest__] :> 
     GraphicsComplex[Function[pt, Riffle[pt, First[#2]]] /@ pts, rest], -1]&, 
  Table[Plot[f[n, x], {x, 0, 2 Pi}, 
        ColorFunction -> (ColorData["Rainbow"][ArcCos[Cos[Pi (n/5 - #2)]]/Pi]&)],
        {n, 10}]], Axes -> True, ViewPoint -> {.4, -1., .5}]

stacked graphics with ColorFunction option

share|improve this answer

Case of explicit functions:

ParametricPlot3D[Evaluate[Table[{ww, zz, Exp[-(ww - 0.5 zz)^2/zz^2]}, 
    {zz, {0.2, 0.4, 0.6, 0.8, 1.0, 1.2}}]], {ww, -2, 2}, 
    PlotStyle -> {{Black, Thick}, {Blue, Thick}, {Red, Thick}, {Yellow, Thick}, 
                  {Green, Thick}, {Magenta, Thick}}]

Case of interpolating functions:

mmax = 4;    
tm = Table[m, {m, 1, mmax}];
tap = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
tfp = Table[(n - 1)^m, {m, 1, mmax}, {n, 1, 10}];
tif = Table[{tap[[n]], tfp[[m, n]]}, {m, 1, Length[tfp]}, {n, 1, Length[tap]}]
tg = Table[Interpolation[tif[[m]]], {m, 1, mmax}];
    Evaluate[Table[{x, zz, tg[[zz]][x]}, {zz, tm}]], {x, 0, 1}, 
    PlotStyle -> {{Blue, Thick}, {Red, Thick}, {Green, Thick}, {Magenta, Thick}}]
share|improve this answer
Hi ! But how is your answer different than Heike's ? – Sektor Dec 12 '14 at 12:17

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.