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 want to create a ListPlot with three sets of data, where the data in each set are joined by a line. This is an example:

ListPlot[{{1, 2, 3, 5, 8}, {2, 3, 6, 9, 10}, {4, 5, 7, 10, 12}}, 
 PlotMarkers -> {Automatic}, Joined -> {True}]

Question: How do I keep a unique marker and colour for each of the sets but specify that the joining lines be a single color (gray)?

share|improve this question

4 Answers 4

up vote 4 down vote accepted

Try this:

Show[{
  ListLinePlot[{{1, 2, 3, 5, 8}, {2, 3, 6, 9, 10}, {4, 5, 7, 10, 12}}, 
    PlotStyle -> GrayLevel[0.7]], 
  ListPlot[{{1, 2, 3, 5, 8}, {2, 3, 6, 9, 10}, {4, 5, 7, 10, 12}}, 
    PlotMarkers -> Automatic]}]

yielding

Mathematica graphics

share|improve this answer

Or simply:

ListPlot[{{1, 2, 3, 5, 8}, {2, 3, 6, 9, 10}, {4, 5, 7, 10, 12}}, 
  PlotMarkers -> {Automatic},
  Joined -> {True}] /. {Hue[__] | RGBColor[__], Line[x_]} :> {Gray, Line[x]}

Mathematica graphics

or as gpap noted /. Line[a___] :> {Gray, Line[a]} is enough in that case.

In v10, this can be simplified a bit,

ListPlot[{{1, 2, 3, 5, 8}, {2, 3, 6, 9, 10}, {4, 5, 7, 10, 12}}, 
  PlotMarkers -> {Automatic},
  Joined -> {True}] /. {_?ColorQ, Line[x_]} :> {Gray, Line[x]}

using the new function ColorQ. Its specific intent is to make it easier to test if a function is a color or not, as the list of color directives seems to grow daily, or at least every version.

share|improve this answer
    
(+1) the simpler Line[a___]->{Gray, Line[a]} also works here. –  gpap Aug 4 at 16:54
    
@gpap Thanks :) And I rather like to overwrite the colour instead of taking advantage of the fact that it takes the last colour applied :) –  Öskå Aug 4 at 17:02
2  
Unfortunately, Hue[__]|RBGColor[__] doesn't cover all the possible color directives, and you'd be hard pressed to list them all. So, in v10, they added ColorQ, which is used like _?ColorQ. –  rcollyer Aug 4 at 17:35
    
@rcollyer That's a nice function indeed. I'm afraid that listing them all would be quite annoying, I just added RGBColor just in case.., by default Hue seems to be used anyway. And in any case, gpap method would overwrite the colour. –  Öskå Aug 4 at 17:38
1  
Hue works in v9, but not in v10 where the default colors are _RGBColor. And, they added several new color directives in both v9 and v10, so it would be a right pain to list them all. (Yes, I have done so, and, why no, I did not know about the new function at the time.) –  rcollyer Aug 4 at 17:41

The easiest way I can think if is just using MeshStyle for the markers and PlotStyle for the gray lines:

ListPlot[{{1, 2, 3, 5, 8}, {2, 3, 6, 9, 10}, {4, 5, 7, 10, 12}}, 
 PlotStyle -> Gray, PlotMarkers -> {Automatic}, Joined -> {True}, 
 MeshStyle -> {Red, Green, Blue}]

Mathematica graphics

share|improve this answer
    
Great answer. :-) –  Mr.Wizard Aug 4 at 18:54
    
Clever indeed :) But it doesn't preserve the so pretty colours ;( –  Öskå Aug 4 at 18:55
    
@Öskå Then use (ColorData[1] /@ {1, 2, 3}) as option-value of MeshStyle. –  halirutan Aug 4 at 18:58
    
@halirutan That would totally be fancier indeed ;o) :D I upvoted anyway :) –  Öskå Aug 4 at 18:59
    
@halirutan I'm afraid that this doesn't function with PlotLegends -> Automatic. +1 anyway. –  eldo Aug 4 at 19:00

Another possibility is that you define your own markers:

markers = {
   Graphics[{Red, Disk[]}],
   Graphics[{Green, Rectangle[]}],
   Graphics[{Blue, Polygon[{{1, 0}, {0, Sqrt[3]}, {-1, 0}}]}]};


ListPlot[{{1, 2, 3, 5, 8}, {2, 3, 6, 9, 10}, {4, 5, 7, 10, 12}},
 PlotLegends ->
  SwatchLegend[{Red, Green, Blue}, {"A", "B", "C"},
   LegendMarkers -> markers,
   LegendLabel -> "Series",
   LegendFunction -> (Framed[#, RoundingRadius -> 5] &),
   LegendMargins -> 5],
 Joined -> True,
 PlotStyle -> GrayLevel@0.6,
 PlotMarkers -> Table[{size, 0.05}, {size, markers}]]

enter image description here

share|improve this answer

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.