# Complex Plot with Imaginary Part encoded in color

Heyhey,

I wanted to plot the complex cosine in a 3D graphic where the real part is given by the surface, while the imaginary part shall be indicated by the color. Therefor, I tried the code

Plot3D[Cos[x + I y] // Re, {x, -2 \[Pi], 2 \[Pi]}, {y, -2, 2},
ColorFunction -> (Hue[Cos[#1 + I #2] // Im] &)]


which seems to not work properly, see the picture.

Is there something wrong about my code, e.g. that the #'s are not passed to the ColorFunction option properly? I saw examples where the Argument was encoded into the color, and I don't see what's problematic about my try here.

edit: this is what I get using ColorFunctionScaling->False:

• Add ColorFunctionScaling -> False and report back. This is an unusual coloring, tho; people more often use the phase instead to determine the color. – J. M. will be back soon Jun 5 '16 at 20:38
• Thanks for your answer! I added the picture in my original post. Note that I wanted to obtain something like this functions.wolfram.com/ElementaryFunctions/Cos/visualizations/5/…, to which the plot is quite similar. – sol87 Jun 6 '16 at 7:49
• Yes, those are the colors I'd expect, since Im[] is unbounded, and Hue[] is 1-periodic. You did not say if you want the imaginary part to be scaled as well. – J. M. will be back soon Jun 6 '16 at 7:52
• You're right. Rescaling the Imaginary part did the job as well. I rescaled with respect to the Min and Max of the imaginary part, this worked quite well. – sol87 Jun 6 '16 at 8:02

I am assuming that this not just (#2&) as the color function.

Here are various ways:

f[x_, y_] := Cos[x + I y];
g[x_, y_] := Re@f[x, y];
h[x_, y_] := Im@f[x, y];
scp = SliceContourPlot3D[
h[x, y] - z, {z == g[x, y], z == -2}, {x, -2 Pi, 2 Pi}, {y, -2,
2}, {z, -2, 2}, Contours -> 10, ColorFunction -> "Rainbow",
BoxRatios -> Automatic, ImageSize -> 250];
cp = ContourPlot[h[x, y], {x, -2 Pi, 2 Pi}, {y, -2, 2},
AspectRatio -> Automatic, ColorFunction -> "Rainbow",
ImageSize -> 250];
p3d = Plot3D[g[x, y], {x, -2 Pi, 2 Pi}, {y, -2, 2},
BoxRatios -> Automatic, MeshFunctions -> (h[#1, #2] &), Mesh -> 10,
MeshShading -> Table[ColorData["Rainbow"][j], {j, 0, 1, 0.1}],
ImageSize -> 250];
p3dc = Plot3D[g[x, y], {x, -2 Pi, 2 Pi}, {y, -2, 2},
BoxRatios -> Automatic,
ColorFunction -> (ColorData["Rainbow"][h[#1, #2]] &),
ColorFunctionScaling -> False, Mesh -> False, ImageSize -> 250];
labels = {"SliceContourPlot", "ContourPlot",
"Plot3D with MeshFunctions", "Plot3D"};
grd = Grid[
Partition[
MapThread[Column[{#1, #2}] &, {labels, {scp, cp, p3d, p3dc}}], 2]]


To show the direction of change (rapid in purple zones) after some play:

Plot3D[g[x, y], {x, -2 Pi, 2 Pi}, {y, -2, 2}, BoxRatios -> Automatic,
ColorFunction -> (ColorData["Rainbow"][Log[2 + h[#1, #2]/2]] &),
ColorFunctionScaling -> False, Mesh -> False, ImageSize -> 250]


• Thank you very much for your detailed answer! Your p3dc is very close to what I wanted to plot, however, now that I see these sliced contour plots, I actually prefer them, I think :) – sol87 Jun 6 '16 at 7:53
• @sol87 you are welcome. I showed a number of ways in case one may be preferable for your needs (and as a check for consistency). Good luck:) – ubpdqn Jun 6 '16 at 8:31