# Problem With Mesh on 2D Plot

How to make the mesh color on 2D plot automatically same with the plot color? The code below doesn't work and produces "Automatic is not a graphics directive" error message.

Plot[Sin[x], {x, 0, 2 Pi}, Mesh -> 10, MeshStyle -> Directive[PointSize[Large], Automatic]] I am expecting an output which looks like this. Second question, how to set different mesh size on more than one plot using single plot command? Tor example, I want to set mesh size 10 on Sin and mesh size 20 on Cos. Of course the mesh color must be automatically aligned with the color of respective plot. Same as the previous code, this one doesn't work and also produces "Automatic is not a graphics directive" error message.

Plot[{Sin[t], Cos[t]}, {t, 0, 2 Pi}, Mesh -> {10, 20},
MeshStyle -> Directive[PointSize[Large], Automatic]] I am expecting this output. I use Mathematica 10.2 on Windows 10 64bit. Thank you.

Comment: Thanks for all the answers. I didn't know this simple task needed relatively complex codes to solve. I was expecting something as simple as

Plot[{Sin[t], Cos[t]}, {t, 0, 2 Pi}, PlotStyle -> {Red, Blue}]


Maybe WRI can make simpler solution in the future.

• Since Directive can have whatever you want inside, as Opacity, etc., either we could have the same colour as default (but there might be strange nuances, like with colour functions, etc), and so, there would be no need to put the word Automatic, or it would probably be preferable a different syntax. Like Colour->Automatic (but this is no good since Directive ins't set by rules, or RGBColour[Automatic], or ColourAutomatic, etc. (my ideas are not the best...) – P. Fonseca Aug 22 '15 at 8:37

Let us look at the structure of the produced Graphics expression using the shortInputForm function:

Plot[{Sin[t], Cos[t]}, {t, 0, 2 Pi}, Mesh -> {10, 20},
MeshStyle -> Directive[PointSize[Large]]] // shortInputForm We see that all the Mesh points present as single Point primitive. It means that even on the level of internal structure of the Plot output Mesh can have only single style. So we are forced to do something like this:

Show[Plot[#1, {t, 0, 2 Pi}, Mesh -> 10, PlotStyle -> #2,
MeshStyle -> Directive[PointSize[Large], #2]] & @@@ {{Sin[t], Blue}, {Cos[t], Red}}] Or if you want the default colors of version 10 (use ColorData for default colors of earlier versions):

Show[MapIndexed[
Plot[#1, {t, 0, 2 Pi}, Mesh -> 10, PlotStyle -> ColorData @@ #2,
MeshStyle -> Directive[PointSize[Large], ColorData @@ #2]] &, {Sin[t], Cos[t]}]] Individual Mesh specification can be used for every plot:

Show[MapIndexed[
Plot[#1, {t, 0, 2 Pi}, Mesh -> {10, 30}[[#2]], PlotStyle -> ColorData @@ #2,
MeshStyle -> Directive[PointSize[Large], ColorData @@ #2]] &, {Sin[t], Cos[t]}]] Cyclically apply individual Mesh specs to successive plots:

Show[MapIndexed[
Plot[#1, {t, 0, 2 Pi}, Mesh -> RotateLeft[{30, 10}, #2 - 1],
PlotStyle -> ColorData @@ #2,
MeshStyle -> Directive[PointSize[Large], ColorData @@ #2]] &, {Tan[t], Sin[t],
Csc[t]}]] Another alternative is MapThread approach suggested by Guess who it is:

Show[MapThread[
Plot[#1, {t, 0, 2 Pi}, Mesh -> #2, PlotStyle -> #3,
MeshStyle -> Directive[PointSize[Large], #3]] &, {{Sin[t], Cos[t]}, {10, 30},
ColorData /@ {1, 2}}]] • …and of course, use ColorData (or ColorData in earlier versions) if you want the default colors. – J. M.'s ennui Aug 22 '15 at 9:52
• How can you get two distinct number of mesh points 10 and 20? – Enrique Pérez Herrero Aug 22 '15 at 10:03
• @Enrique, MapThread[] would be useful in that case, or an appropriate modification of Alexey's first approach. – J. M.'s ennui Aug 22 '15 at 10:22

If you are trying to be concise and want to avoid scoping problems, I recommend this variant.

Show @@
With[{color = ColorData[#3]},
Plot[#1[t], {t, 0, 2 Pi},
Mesh -> #2,
PlotStyle -> color,
MeshStyle -> {color, PointSize[Large]}] &
@@@
{{Sin, 10, 1}, {Cos, 20, 2}}]


which produces exactly reproducing your expected output. It is more robust than some the other offerings because the argument array (which gets evaluated) uses pure functions or function names, not expressions. That is, supplying Sin is preferable to supplying Sin[t].