5
$\begingroup$

I'm plotting complex-valued functions using a common technique: plot the magnitude (modulus) on the vertical axis and fill the area below with color hues to indicate the phase (argument). Here is an example:

f[x_] := Exp[-x^2] Exp[10 I x];
Plot[Abs[f[x]], {x, -3, 3}, 
  Filling -> Axis, 
  ColorFunction -> Function[x, Hue[Arg[f[x]]/(2 Pi)]], 
  ColorFunctionScaling -> False, 
  PlotPoints -> 500]

This works fine, but for aesthetic reasons I'd also like to add a black outline curve, like this:

enter image description here

I made this image by plotting the outline in a separate Plot, then combining the two plots using Show. But that's pretty awkward, so I'm wondering if there's a better way to do it, preferably with a single Plot. The problem seems to be that the ColorFunction setting controls both the curve and the filling underneath it. Ideas?

$\endgroup$
1
  • $\begingroup$ (The PlotPoints setting isn't irrelevant here: a setting greater than the default is needed for color hues to show correctly.) $\endgroup$ Commented Jun 6, 2017 at 18:17

2 Answers 2

4
$\begingroup$

I'm not sure if you can do this with the options to Plot but you can "fix" it afterwards:

Plot[Abs[f[x]], {x, -3, 3}, 
  Filling -> Axis, 
  ColorFunction -> Function[{x, y}, Hue[Arg[f[x]]/(2 Pi)]], 
  ColorFunctionScaling -> False] /. 
  Line[pts_, _] :> {Thick, Black, Line[pts]}

Filling with plot line

(We are removing the VertexColors -> Automatic option from the plot line.)

$\endgroup$
1
$\begingroup$

You can use the options MeshFunctions, Mesh and MeshShading:

Plot[Abs[f[x]], {x, -3, 3}, Filling -> Axis, 
 ColorFunction -> Function[{x, y}, Hue[Arg[f[x]]/(2 Pi)]], 
 ColorFunctionScaling -> False,
 Mesh -> {{-3}}, MeshFunctions -> {#1 &}, 
 MeshShading -> {Directive[Thick, Black], Directive[Thick, Black]}]

Mathematica graphics

Notes: We can not use PlotStyle because as stated in Documentation >> Plot:

Mathematica graphics

However, (see Documentation >> ColorFunction), we can use MeshShading since

Mathematica graphics

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.