2

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.

bill s
  • 68,936
  • 4
  • 101
  • 191
  • 1
    Try MeshShading -> {Green, Yellow} see the docs on MeshFunctions >> Details on how MeshFunctions work. – kglr Mar 03 '17 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 Mar 03 '17 at 16:44
  • What does the #1& refer to? –  Mar 03 '17 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 Mar 03 '17 at 16:49
  • for #&` see Slot – kglr Mar 03 '17 at 17:11
  • Possible duplicate: http://mathematica.stackexchange.com/questions/15013/dashed-line-with-alternating-colored-dashes – Michael E2 Mar 04 '17 at 14:02
  • For #1 and &, you can select each and execute the menu command Help > Find Selected Function. The second hits explain them, as well as the tutorial "Pure Functions" (third hit for #1). – Michael E2 Mar 04 '17 at 14:04
  • I mind this behavior,your post also include others' efforts. – yode Apr 19 '17 at 21:35
  • @Quantitative - change your mind all you like, but please stop deleting the question. – Jason B. Apr 19 '17 at 21:45
  • @Quantitative - basically because you've asked a question that has upvoted answers you aren't allowed to delete it see this for an explanation of the policy – Jason B. Apr 19 '17 at 21:59
  • @Quantitative - now we come to the real question, why do you need the post deleted? Have you pasted code here that you aren't allowed to paste (as in, you aren't the original author)? – Jason B. Apr 19 '17 at 22:00
  • @Quantitative - would it still be against policy if you rewrote the question, or is the very act of asking a question on here against policy (as in, this is your homework)? – Jason B. Apr 19 '17 at 22:06
  • 2
    @Quantitative -- do not sabotage this site. – bill s Apr 19 '17 at 22:08

2 Answers2

5

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

yode
  • 26,686
  • 4
  • 62
  • 167
2

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]}]
David G. Stork
  • 41,180
  • 3
  • 34
  • 96