6
$\begingroup$

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)?

$\endgroup$
4
$\begingroup$

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

$\endgroup$
8
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ (+1) the simpler Line[a___]->{Gray, Line[a]} also works here. $\endgroup$ – gpap Aug 4 '14 at 16:54
  • $\begingroup$ @gpap Thanks :) And I rather like to overwrite the colour instead of taking advantage of the fact that it takes the last colour applied :) $\endgroup$ – Öskå Aug 4 '14 at 17:02
  • 2
    $\begingroup$ 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. $\endgroup$ – rcollyer Aug 4 '14 at 17:35
  • $\begingroup$ @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. $\endgroup$ – Öskå Aug 4 '14 at 17:38
  • 1
    $\begingroup$ 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.) $\endgroup$ – rcollyer Aug 4 '14 at 17:41
7
$\begingroup$

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

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

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

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