10
$\begingroup$

Bug introduced in 9.0 or earlier and persisting through 11.0


I have a set of data and several function operating on the same data, that I wish to plot together in a ListPlot. I need different PlotMarkers for each function to visualise the data. Because the data is quite closely spaced, I specified the number of markers using Mesh in the way I found here: Custom Intervals of Markers

So I have the following:

a = Table[ii, {ii, 0, 2 \[Pi], 0.01}];
b = Sin[a];
c = Sin[a + \[Pi]/4 ];

ListPlot[
  {Transpose[{a, b}],
   Transpose[{a, c}]
  },
 PlotMarkers -> Automatic,
 Joined -> True,
 Mesh -> 20]

However, the Mesh command suddenly makes all markers the same shape and colour, so this is what I get out:

enter image description here

How can I make sure that the PlotMarkers' colour and shape remain unchanged while being able to specify the number of markers in the plot?

I'm using Mathematica 9.

$\endgroup$
6
  • $\begingroup$ Possibly related: (7201) $\endgroup$
    – Mr.Wizard
    Commented Jul 8, 2013 at 15:04
  • $\begingroup$ The given code does not yield the given plot under my Mma 9. Are you sure this is what you are entering? For starters, why would a ListPlot join the dots ... unless told to do so? Orrrr ... are you actually using ListLinePlot rather than ListPlot? $\endgroup$
    – wolfies
    Commented Jul 8, 2013 at 15:15
  • 1
    $\begingroup$ You are right, it was exactly what I entered, but I'd forgotten that I'd added a SetOptions[ListPlot, Joined -> True]; earlier in the Notebook, sorry for that. It also seems like that is exactly what makes the difference. Without the Joined->True it works, with it, the markers change. Seems like a bug to me. $\endgroup$
    – Jelle
    Commented Jul 8, 2013 at 15:27
  • $\begingroup$ You're mixing plot points and mesh points. What you are seeing is the mesh points, but if you happen to choose the number of mesh points correctly where they are plot points, then you get the behavior you're seeing. And, yes, it is a bug, and already reported. $\endgroup$
    – rcollyer
    Commented Jul 8, 2013 at 16:54
  • 1
    $\begingroup$ I don't completely understand: how exactly do you suggest to choose the Mesh points correctly? And can it be done when you assume that the data (in this case a) is randomly sampled, rather than evenly distributed like in this example? $\endgroup$
    – Jelle
    Commented Jul 8, 2013 at 17:00

3 Answers 3

4
$\begingroup$

You could use the version of Mesh where you specify an explicit list of coordinates, taking these directly from a:

Module[{meshpoints = 20},
 With[{step = Round[Length[a]/(meshpoints + 1)]}, 
  ListPlot[{Transpose[{a, b}], Transpose[{a, c}]}, 
   PlotMarkers -> Automatic, Joined -> True, 
   Mesh -> {a[[step ;; -step ;; step]]}]]]

enter image description here

$\endgroup$
4
  • $\begingroup$ +1. It's interesting that this circumvents the bug, but Mesh -> {Table[i, {i, 0, 2 Pi, .2}]} does not. $\endgroup$
    – Michael E2
    Commented Jul 8, 2013 at 21:05
  • $\begingroup$ @MichaelE2, it looks like a rounding error thing - if you use 1/100 and 1/5 instead of 0.01 and 0.2 the bug goes away. $\endgroup$ Commented Jul 8, 2013 at 21:11
  • $\begingroup$ So the mesh coordinates have to be (some of) the first coordinates of the of the second list, I guess. (Oops, for some reason I had used SameQ instead of just subtracting a[[;; -1 ;; Round[0.2/0.01]]] - Table[i, {i, 0, 2 Pi, 0.2}].) $\endgroup$
    – Michael E2
    Commented Jul 8, 2013 at 21:34
  • $\begingroup$ This is a beautiful way of working around the bug indeed: it even works for non-equally spaced data, and still keeps the option to add anything else you may want to add to ListPlot (like a legend). Thanks! $\endgroup$
    – Jelle
    Commented Jul 9, 2013 at 10:53
5
$\begingroup$

This seems to be a bug, though I cannot pinpoint it. I suggest you plot the lines separately and combine with Show, e.g.:

Show[
 MapIndexed[
  ListLinePlot[#, PlotMarkers -> Graphics`PlotMarkers[][[#2]], 
    PlotStyle -> ColorData[1, "ColorList"][[#2]], Mesh -> 20] &,
  {Transpose[{a, b}], Transpose[{a, c}]}
 ],
 PlotRange -> All
]

enter image description here

PlotRange -> All is included for the case where the first plot doesn't cover the full range of the second plot; without there would be truncation.

$\endgroup$
1
  • $\begingroup$ It does look like a bug indeed. This is a good suggestion, although I didn't mention I have to add a Legend too. That's a deal harder with combined plots in Show... $\endgroup$
    – Jelle
    Commented Jul 8, 2013 at 15:48
3
$\begingroup$

A somewhat awkward way to get points onto your ListLinePlot would be to use Epilog

a = Table[ii, {ii, 0, 2 \[Pi], 0.01}];
b = Sin[a];
c = Sin[a + \[Pi]/4];

ListLinePlot[{Transpose[{a, b}], Transpose[{a, c}]}, 
 Epilog -> {PointSize[0.02], Blue, 
   Point@Take[Transpose[{a, b}], {1, -1, 20}], Red, 
   Point@Take[Transpose[{a, c}], {1, -1, 20}]}, 
 PlotLegends -> PointLegend[{Blue, Red}, {"one", "two"}]]

This addresses your desire to have one plot command without using Show and provides a mechanism for including a legend. I'll leave it to others to come up with more elegant solutions.

$\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.