2
$\begingroup$

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.

$\endgroup$
13
  • 1
    $\begingroup$ Try MeshShading -> {Green, Yellow} see the docs on MeshFunctions >> Details on how MeshFunctions work. $\endgroup$
    – kglr
    Mar 3, 2017 at 16:35
  • $\begingroup$ 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) $\endgroup$
    – kglr
    Mar 3, 2017 at 16:44
  • $\begingroup$ What does the #1& refer to? $\endgroup$
    – user47047
    Mar 3, 2017 at 16:48
  • $\begingroup$ 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]}] $\endgroup$
    – yode
    Mar 3, 2017 at 16:49
  • 2
    $\begingroup$ @Quantitative -- do not sabotage this site. $\endgroup$
    – bill s
    Apr 19, 2017 at 22:08

2 Answers 2

5
$\begingroup$

Since this work,I post it as an answer for reading

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]}]

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

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]}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.