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.

How can you use the same colour to the data in Epilog as the data in the ListLinePlot?

I am plotting 25 lists (4 in this question) on which I would need to mark each maximum.

I would like the point marking the maximum to have the same colour as the line (if possible with a black exterior circle).

enter image description here

Is it possible?

a = {1, 3, 6, 3, 2};
b = {3, 2, 5, 6, 9};
c = {2, 7, 3, 1, 6};
d = {4, 2, 8, 8, 10};
am = {Position[a, Max[a]][[1, 1]], Max[a]}
bm = {Position[b, Max[b]][[1, 1]], Max[b]}
cm = {Position[c, Max[c]][[1, 1]], Max[c]}
dm = {Position[d, Max[d]][[1, 1]], Max[d]}
ListLinePlot[{a, b, c, d}, AxesLabel -> {"Iteraciones", "Objetivo"}, 
 LabelStyle -> {24, Bold}, 
 PlotStyle -> {{Thickness[0.01]}, {Thickness[0.01]}, {Thickness[
     0.01]}}, Epilog -> {PointSize[0.04], Point[{am, bm, cm, dm}]}]
share|improve this question
2  
The default colours are ColorData[1, n] for $n = 1, 2, \ldots$ –  Rahul Apr 28 at 9:41

3 Answers 3

up vote 3 down vote accepted

First you define the colors with PlotStyle and then you draw the points twice in the Epilog - large in black and smaller in the same colors as the lines. Adapt the PlotRange in order to have the points not clipped.

cols = {Red, Green, Blue, Purple}

ListLinePlot[{a, b, c, d}, PlotStyle ->cols, AxesLabel -> {"Iteraciones", "Objetivo"},
LabelStyle -> {24, Bold}, PlotStyle -> {{Thickness[0.01]}, {Thickness[0.01]},
{Thickness[0.01]}}, Epilog -> {{PointSize[0.06], Point[{am, bm, cm, dm},
VertexColors -> {Black}]}, {PointSize[0.04], Point[{am, bm, cm, dm},
VertexColors -> cols]}}, PlotRange -> {{1, 5.5}, {1, 11}}, ImageSize -> 500]

Mathematica graphics

share|improve this answer
    
PERFECT. Only one question, I have series of 35-50 lineas and their maximuns. If I want use any "MAP"(I don´t know how to do.) or I want to use the default colors. How Can I use these colors(default.. but not black) TOO in the Epilog ( interior points... Blue, Purple, Yelloow, Green,... ciclelly) ? without typing ..using the default cicle of MAthematica use for lines... TOO in POINTS. –  Mika Ike Apr 28 at 9:30

For completeness purposes and thanks to the maxes function in kguler's answer, here is an example working for $n$ extrema.

a = {1, 3, 6, 3, 2};
b = {3, 2, 9, 6, 9};
c = {2, 7, 3, 1, 6};
d = {4, 2, 8, 8, 10};
varToPlot = {a, b, c, d};
maxes = Thread@Through[{Flatten[Position[#, Max[#]]] &, Max}[#]] & /@ varToPlot;
ListLinePlot[varToPlot, AxesLabel -> {"Iteraciones", "Objetivo"}, 
  PlotStyle -> Thickness[0.01], 
  Epilog -> {EdgeForm[{Thick, Black}], 
    {ColorData[1, #], 
     Disk[#, {.1, 1/3}] & /@ maxes[[#]]} & /@ Range@Length@maxes}, 
  ImageSize -> 500, PlotRange -> {{1, 5.5}, {1, 11}}]

enter image description here

share|improve this answer
    
I like very much your answer (IS A PERFECT SOLUTION), but Kardasher answer first and Kardasher has less points. –  Mika Ike Apr 28 at 14:26

Alternative ways without Epilog:

lists = {a, b, c,  d} = 
   {{1, 3, 6, 3, 2}, {3, 2, 5, 6, 9}, {2, 7, 3, 1, 6, 7}, {4, 2, 8, 8, 10}};
(* note: the list `c` modified to have two maxima *)
len = Length@lists;
maxes = Thread@Through[{Flatten[Position[#, Max[#]]] &, Max}[#]] & /@lists;
colors = ColorData[3, "ColorList"] (* or your choice of colors*);

1. If you did not need the black edges, you could have simply used

ListPlot[Join[lists, maxes],
  Joined -> ArrayPad[{True, False}, len - {1, 1}, "Fixed"],
  PlotStyle -> Directive[Thick, PointSize[.04]]]

or

 Show[ListLinePlot[lists, PlotStyle -> Thick], 
      ListPlot[maxes, PlotStyle -> Directive[Thick, PointSize[.04]]]]

For specific color lists of your choice (say colors):

ListPlot[Join[lists, maxes],
  Joined -> ArrayPad[{True, False}, len - {1, 1}, "Fixed"],
  PlotStyle -> (Directive[Thick, PointSize[.04], #] & /@colors[[;; len]])]

2. If you do need the black edges on the markers you can use PlotMarkers. Define a custom marker function using the tricks from this answer by @rm-rf

markerF := {Graphics[{EdgeForm[{Gray, Thick}],
  Dynamic@FaceForm[CurrentValue["Color"]], Disk[{0, 0}, #]}], .07} &;

and use in a single ListPlot:

ListPlot[Join[lists, maxes],
   PlotStyle ->Thread[{Thick, colors[[;; len]]}],
   Joined -> Join[ConstantArray[True, len], ConstantArray[False, len]], 
   PlotMarkers :> (markerF /@ Join[ConstantArray[0, len], ConstantArray[1, len]]),
   PlotRangePadding -> .4]

enter image description here

share|improve this answer
    
I like very much your detailed answer, but Kardasher answered first and has Kardasher less points. –  Mika Ike Apr 28 at 14:27
1  
@MikaIke, i too like Kardasher's and Oska's answers better; they address your question directly. –  kguler Apr 28 at 14:51

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.