I need my code to produce a graph of some function (just normal Plot, not Plot3D) with a mesh of 20 based on the x-values and alternating colors between green and yellow.

I have produced the following code:

    Plot[x^3, {x, -20, 20},
PlotStyle -> Thick,
MeshStyle -> {Green, Yellow},
MeshFunctions -> {#1 &}, Mesh -> 20]


Can someone explain what the {#1&} means and how to make the colors alternate, because currently it is only making a yellow mesh.

• Try MeshShading -> {Green, Yellow} see the docs on MeshFunctions >> Details on how MeshFunctions work.
– kglr
Commented Mar 3, 2017 at 16:35
• you can use both ... MeshStyle -> Red, MeshShading -> {Green, Yellow} ...; the two options do different things. Regarding the default mesh functions there is a table in MeshFunctions>> Details showing the default settings for various *Plot* functions. For Plot, the default mesh function uses the x values (#1& is the first argument)
– kglr
Commented Mar 3, 2017 at 16:44
• What does the #1& refer to?
– user47047
Commented Mar 3, 2017 at 16:48
• Do you want Plot[x^3, {x, -20, 20}, PlotLabel -> "Title", PlotStyle -> Thick, MeshStyle -> {Directive[Green, PointSize[Medium]], Directive[Yellow, PointSize[Medium]]}, MeshFunctions -> {#1 &, #1 &}, Mesh -> {Range[-20, 20, 2], Range[-20, 20 , 4]}]
– yode
Commented Mar 3, 2017 at 16:49
• @Quantitative -- do not sabotage this site. Commented Apr 19, 2017 at 22:08

Plot[x^3, {x, -20, 20}, PlotLabel -> "Title", PlotStyle -> Thick,
MeshStyle -> {Directive[Red, PointSize[Medium]],
Directive[Yellow, PointSize[Medium]]},
MeshFunctions -> {#1 &, #1 &},
Mesh -> {Range[-20, 20, 2], Range[-20, 20, 4]}]


• @ydoe: Why include the irrelevant Title? Commented Mar 3, 2017 at 17:30
• @DavidG.Stork That inherit from OP :-)
– yode
Commented Mar 3, 2017 at 17:54
• Change the value in Range[...,...,...] or check the documentation of function Subdivide
– yode
Commented Mar 3, 2017 at 18:54

The #1& means that the mesh is determined by equal steps (say) along the $x$ value on the plot. Use instead #2& if you want it to be along the second or $y$ value on the plot. In a Plot3D, you can use #1& or #2& or #3& ($z$) or combinations thereof.

Try:

Plot[.01 x^3, {x, -20, 20},
PlotStyle -> Thick,
MeshStyle -> {Directive[Green, PointSize[Medium]],
Directive[Yellow, PointSize[Medium]]},
MeshFunctions -> {#2 &, #2 &},
Mesh -> {Range[-20, 20, 2], Range[-20, 20, 4]}]