3
$\begingroup$

I have the following code that makes a 3D plot, with 3 Mesh functions drawn on top of it:

um = 1;
Potential[V0_, k_, w_, x_, z_] := V0*(Sin[k z]^2)*Exp[(-2 x^2)/w^2] 
Plot3D[Potential[1, (2 \[Pi])/(0.725 um), 1 um, x, z], {x, -2.1 um, 
  2.1 um}, {z, -1.1 um, 1.1 um}, PlotRange -> Full, 
 AxesLabel -> MaTeX /@ {"x/\\sigma"}, PlotPoints -> 50, 
 Mesh -> {{0, 0.5, 1}, {2}, {2}}, 
 MeshStyle -> {{Red, Blue, Green}, {Red}, {Blue}}, Boxed -> False, 
 Axes -> {True, False, False}, PlotStyle -> Cyan, 
 Ticks -> {Table[i, {i, -2, 2, 1}]}]

How can I make the three Mesh functions have different colours?

I tried the MeshStyle -> {{Red, Blue, Green}, {Red}, {Blue}} but all 3 functions end up Green...

$\endgroup$
4
$\begingroup$

You need to add the option MeshFunctions -> {# &, #2 &, #3 &} and specify individual styles for mesh divisions using option Mesh:

Plot3D[Potential[1, (2 π)/(0.725 um), 1 um, x, z],
 {x, -2.1 um, 2.1 um}, {z, -1.1 um, 1.1 um}, 
 PlotRange -> Full, PlotPoints -> 50, 
 MeshFunctions -> {# &, #2 &, #3 &}, 
 Mesh -> {Thread[{{0, 0.5, 1}, {Red, Blue, Green}}], 2, 2}, 
 MeshStyle -> {Automatic, Orange, Purple}, Lighting -> "Neutral", 
 Boxed -> False, Axes -> {True, False, False}, PlotStyle -> White, 
 Ticks -> {Table[i, {i, -2, 2, 1}]}]

enter image description here

Notes:

I your code, the Mesh specs {2} for the second and third directions is outside the plot range.

Default value of the MeshFunctions for Plot3D is {#1 &, #2 &}

Options[Plot3D, MeshFunctions]

{MeshFunctions -> {#1 &, #2 &}}

so the mesh in the vertical direction will not be drawn unless you use the option MeshFunctions with setting {#&, #2&, #3&}.

MeshStyle >> Details

  • MeshStyle->{Subscript[g, 1],Subscript[g, 2], ...} specifies that successive directives Subscript[g, i] should be used cyclically to draw elements associated with successive mesh functions.

So you cannot specify different directives for individual mesh divisions corresponding to a given mesh function. To style mesh divisions individually you can use the form {{{mesh1, directive1}, ...}, ...} as the setting for the option Mesh.

$\endgroup$

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.