Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

4 Answers 4

up vote 8 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]

enter image description here

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

enter image description here

share|improve this answer
1  
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

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)];
Graphics3D[
 MapIndexed[
  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:

Graphics3D[
 MapIndexed[
  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

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

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

Mathematica graphics

share|improve this answer

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
3  
@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 mathematica.stackexchange.com/a/1396/121 –  Mr.Wizard Feb 6 '12 at 21:12

Your Answer

 
discard

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.