1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Possible duplicate of Different MeshStyle for different functions $\endgroup$ – Kuba Apr 14 '16 at 12:02
  • $\begingroup$ Maybe it's not an exact duplicate but that answer is an answer to your question too. $\endgroup$ – Kuba Apr 14 '16 at 12:17
  • $\begingroup$ 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. $\endgroup$ – Asking Questions Apr 14 '16 at 12:18
  • 1
    $\begingroup$ Indeed, because you can't use MeshStyle to color each function separately. $\endgroup$ – Kuba Apr 14 '16 at 12:19
2
$\begingroup$

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

enter image description here

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Asking Questions Apr 14 '16 at 12:20
  • $\begingroup$ @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 $\endgroup$ – Jason B. Apr 14 '16 at 12:26
  • $\begingroup$ @AskingQuestions I don't know :-/ This is how I would do this. Not pretty buy at the end it is not so much. $\endgroup$ – Kuba Apr 14 '16 at 12:26
  • 1
    $\begingroup$ You can also remove MeshStyle -> #2 and use MeshStyle->Automatic in opts (+1) $\endgroup$ – kglr Apr 14 '16 at 23:06
2
$\begingroup$

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

enter image description here

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

enter image description here

$\endgroup$
  • $\begingroup$ It doesn't respect Mesh specification. $\endgroup$ – Kuba Apr 14 '16 at 12:25
  • $\begingroup$ Which specifications are those? $\endgroup$ – Asking Questions Apr 14 '16 at 12:26
  • $\begingroup$ @Kuba, but it works here for Mesh->All. I'd think a ListPlot would be easier.... $\endgroup$ – Jason B. Apr 14 '16 at 12:29
  • $\begingroup$ Maybe you could give the example with ListPlot $\endgroup$ – Asking Questions Apr 14 '16 at 12:30
  • $\begingroup$ @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. $\endgroup$ – Kuba Apr 14 '16 at 12:31
1
$\begingroup$

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

enter image description here

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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