Plot: Option MeshStyle: color rotation matching PlotStyle color rotation

This is a follow up question to

plot: option plotstyle: start with second color in standard rotation

I want the mesh points to have the same color as in PlotStyle or the points to not appear at all. Simple example that reproduces the issue

Plot[{t, 2 t}, {t, 0, 180}, PlotPoints -> 20, MaxRecursion -> 0, Mesh -> All,
MeshStyle -> Rest[ColorData[97, “ColorList”]],
PlotStyle -> Rest[ColorData[97, “ColorList”]]]

I feel this not a duplicate to this question

Different MeshStyle for different functions

as the goal of that question is not to match mesh and plot style.

=================

  

 

Possible duplicate of Different MeshStyle for different functions
– Kuba
Apr 14 at 12:02

  

 

Maybe it’s not an exact duplicate but that answer is an answer to your question too.
– Kuba
Apr 14 at 12:17

  

 

The answer in the other thread is not very helpful and does not relate to this question very well. I do not want to plot them seperatly and then use Show. I feel that that would be more of a workaround.
– Asking Questions
Apr 14 at 12:18

1

 

Indeed, because you can’t use MeshStyle to color each function separately.
– Kuba
Apr 14 at 12:19

=================

3 Answers
3

=================

As showed in linked question, it can;t be done withing one Plot.

Block[{t}, With[{opts = Sequence[PlotPoints -> 20, MaxRecursion -> 0, Mesh -> All]},
Show @ MapThread[
Plot[#, {t, 0, 180}, opts, MeshStyle -> #2, PlotStyle -> #2] &,
{
{t, 2 t},
ColorData[97, “ColorList”][[2 ;; 3]]
}
]
]
]

  

 

Is there an easy explanation why this can not be done within Plot? I am thankful for this example, do not get me wrong, but I would prefer an easier solution.
– Asking Questions
Apr 14 at 12:20

  

 

@AskingQuestions – it seems that MeshStyle isn’t designed to work like this. You can give multiple styles, but they are applied to the different MeshFunctions. If you look in the help you find an example where they use red mesh in the x direction and blue mesh in the y direction. I’d say the most natural way to do what you are trying to do would be to make a ListPlot
– JasonB
Apr 14 at 12:26

  

 

@AskingQuestions I don’t know :-/ This is how I would do this. Not pretty buy at the end it is not so much.
– Kuba
Apr 14 at 12:26

1

 

You can also remove MeshStyle -> #2 and use MeshStyle->Automatic in opts (+1)
– kglr
Apr 14 at 23:06

Here is another method,

Plot[{t, 2 t}, {t, 0, 180}, PlotPoints -> 20,
PlotStyle -> Rest[ColorData[97, “ColorList”]],
MaxRecursion -> 0] /. Line[a_] :> {Point[a], Line[a]}

Here the points will intrinsically match whatever PlotStyle you apply.

If all you are trying to do is include evenly spaced points in the x-direction for your plot, use ListLinePlot with PlotMarkers

list1 = Table[{t, t}, {t, 0, 180, 10}];
list2 = Table[{t, 2 t}, {t, 0, 180, 10}];
ListLinePlot[{list1, list2},
PlotMarkers -> First@Graphics`PlotMarkers[],
PlotStyle -> Rest[ColorData[97, “ColorList”]]]

  

 

It doesn’t respect Mesh specification.
– Kuba
Apr 14 at 12:25

  

 

Which specifications are those?
– Asking Questions
Apr 14 at 12:26

  

 

@Kuba, but it works here for Mesh->All. I’d think a ListPlot would be easier….
– JasonB
Apr 14 at 12:29

  

 

Maybe you could give the example with ListPlot
– Asking Questions
Apr 14 at 12:30

  

 

@JasonB yes it works for Mesh->All only, sorry for generalization. It may be your method is better for OP, depends what is a final goal.
– Kuba
Apr 14 at 12:31

You can also use the option EvaluationMonitor to retrieve the mesh points from Plot and feed them to ListPlot:

myfuncs = {Sin,Cos};

data = Reap[Plot[y = #[t], {t, 0, 2}, PlotPoints -> 20, MaxRecursion -> 1,
Mesh -> All, EvaluationMonitor :> Sow[{t, y}]];] & /@ myfuncs // #[[All, 2, 1]] &;

ListPlot[Sort /@ data, Joined -> True, PlotMarkers -> Style[“\[FilledCircle]”, 12],
PlotStyle -> ColorData[97, “ColorList”][[2 ;; 3]]]

(Of course, this solution only makes sense if your interested in the “adaptative sampling” of Plot as already mentioned in the comments.)