9
$\begingroup$

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

enter image description here

I am expecting an output which looks like this.

enter image description here

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

enter image description here

I am expecting this output.

enter image description here

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.

$\endgroup$
1
  • $\begingroup$ 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...) $\endgroup$
    – P. Fonseca
    Commented Aug 22, 2015 at 8:37

2 Answers 2

14
$\begingroup$

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

screenshot

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

plot

Or if you want the default colors of version 10 (use ColorData[1] for default colors of earlier versions):

Show[MapIndexed[
  Plot[#1, {t, 0, 2 Pi}, Mesh -> 10, PlotStyle -> ColorData[97] @@ #2, 
    MeshStyle -> Directive[PointSize[Large], ColorData[97] @@ #2]] &, {Sin[t], Cos[t]}]]

plot

Individual Mesh specification can be used for every plot:

Show[MapIndexed[
  Plot[#1, {t, 0, 2 Pi}, Mesh -> {10, 30}[[#2]], PlotStyle -> ColorData[97] @@ #2, 
    MeshStyle -> Directive[PointSize[Large], ColorData[97] @@ #2]] &, {Sin[t], Cos[t]}]]

plot

Cyclically apply individual Mesh specs to successive plots:

Show[MapIndexed[
  Plot[#1, {t, 0, 2 Pi}, Mesh -> RotateLeft[{30, 10}, #2 - 1], 
    PlotStyle -> ColorData[97] @@ #2, 
    MeshStyle -> Directive[PointSize[Large], ColorData[97] @@ #2]] &, {Tan[t], Sin[t], 
   Csc[t]}]]

plot

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[97] /@ {1, 2}}]]

plot

$\endgroup$
3
  • 1
    $\begingroup$ …and of course, use ColorData[97] (or ColorData[1] in earlier versions) if you want the default colors. $\endgroup$ Commented Aug 22, 2015 at 9:52
  • $\begingroup$ How can you get two distinct number of mesh points 10 and 20? $\endgroup$ Commented Aug 22, 2015 at 10:03
  • 2
    $\begingroup$ @Enrique, MapThread[] would be useful in that case, or an appropriate modification of Alexey's first approach. $\endgroup$ Commented Aug 22, 2015 at 10:22
6
$\begingroup$

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

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

which produces

plot

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

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.