Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
Possibly related: (7201) –  Mr.Wizard Jul 8 '13 at 15:04
    
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? –  wolfies Jul 8 '13 at 15:15
    
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. –  Jelle Jul 8 '13 at 15:27
    
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. –  rcollyer Jul 8 '13 at 16:54
    
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? –  Jelle Jul 8 '13 at 17:00
show 1 more comment

3 Answers

up vote 4 down vote accepted

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

share|improve this answer
    
+1. It's interesting that this circumvents the bug, but Mesh -> {Table[i, {i, 0, 2 Pi, .2}]} does not. –  Michael E2 Jul 8 '13 at 21:05
    
@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. –  Simon Woods Jul 8 '13 at 21:11
    
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}].) –  Michael E2 Jul 8 '13 at 21:34
    
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! –  Jelle Jul 9 '13 at 10:53
add comment

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.

share|improve this answer
    
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... –  Jelle Jul 8 '13 at 15:48
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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