I'd like to prepare some presentations in Mathematica to help students visualize functions of two variables (it's a usual calculus course). I thought it would be both cool and useful to have the graphs as red/cyan anaglyphs. Is it possible to do that, and if yes, how?

Edit: Simon Woods' answer below is great, but it produces a static image. I'd prefer an interactive version (rotatable - is it a word? - with a mouse); if this is not possible, then I'd like to have at least an animation. (I guess the latter shouldn't be too hard - I'd only have to put suitable commands in some loop, export the images and mount them as an animation; the point is, I'm a Mathemathica newbie and don't know (yet) how to do it - but I can probably figure that out on my own.)


I think the basic idea is to create two slightly different views and combine them in the red and (green + blue) channels.

p = Plot3D[Sin[x y]^2, {x, -2, 2}, {y, -2, 2}];

{r, g} = ColorConvert[
 Image[Show[p, ViewPoint -> {3 Sin[#], 3 Cos[#], 2} &[# Degree]],
   ImageSize -> {360, 275}], "Grayscale"] & /@ {141, 139};

ColorCombine[{r, g, g}]

enter image description here

A simple way to animate is just to change the ViewPoint in a loop and Export the individual frames. I use some software called VirtualDub to combine the images into a movie or animated gif:

Do[{r, g} = ColorConvert[
     Image[Show[p, SphericalRegion -> True, 
       ViewPoint -> {3 Sin[#], 3 Cos[#], 2} &[# Degree]], 
      ImageSize -> {360, 275}], "Grayscale"] & /@ {2 a + 1, 2 a - 1}; 
 Export["frame" <> ToString[a] <> ".bmp", ColorCombine[{r, g, g}]]
 , {a, 0, 44}]

enter image description here

  • $\begingroup$ It works for me, but I think you should remove the tick labels as they are vertically displaced somewhat. $\endgroup$ – Sjoerd C. de Vries Aug 11 '12 at 20:06
  • $\begingroup$ Also, shouldn't you keep the ViewCenter the same in both pictures? $\endgroup$ – Sjoerd C. de Vries Aug 11 '12 at 20:09
  • $\begingroup$ Thanks, this is very nice. However, it is "just an image", I cannot e.g. rotate it interactively. I'll modify the question once again;). $\endgroup$ – mbork Aug 11 '12 at 20:30
  • $\begingroup$ @SjoerdC.deVries, the ViewCenter is the same in both images I think. $\endgroup$ – Simon Woods Aug 11 '12 at 21:22
  • $\begingroup$ @mbork, interactive rotation is probably possible, but I don't know how to do it! $\endgroup$ – Simon Woods Aug 11 '12 at 21:26

The idea of interactive rotation of the anaglyph has caught my attention. I propose the following:

    Image[Show[pl, ViewPoint -> {2 Sin[(\[Alpha] + #[[1]]) Degree], 
        2 Cos[(\[Alpha] + #[[1]]) Degree], 
        3 Cos[\[Beta] Degree]}], 
     ImageSize -> {360, 275}]][[#[[2]]]] & /@ {{2, 1}, {0, 
   2 ;; 3}}), {{\[Alpha], 45}, -90, 90}, {{\[Beta], 60}, 0, 180}, 
   ContinuousAction -> True, 
   Initialization :> (pl = 
   Plot3D[Sin[(x y)]^2, {x, -2, 2}, {y, -2, 2}, Boxed -> False, 
 Axes -> False, SphericalRegion -> True, PlotRange -> All, 
 ColorFunction -> "GrayTones", ColorFunctionScaling -> True])]

and my result is the following:

enter image description here

The main problem with anaglyphs are the combination of colors in the image. Try to avoid many explicit reds and blues, or your pseudo-stereo image (plot) will suffer from ghost parts. I recommend a color scheme based on grey tones.

  • Edit update

I have edited the code to be a bit more faster.


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.