3
$\begingroup$

How can I add a marker or meshpoint to the start and end of a line in ListLinePlot or DateListPlot?

I would like to be able specify the marker type, eg. a circle for the start and a cross for the end.

I would like the solution to generalise to ListLinePlot, DateListPlot and also where the path might be a TimeSeries object

data = {-253, -219, -303, -333, -333, -340, -298, -229, -203, -150, -121, -113, -52, -67, -25, 180, 129};
time = Range@Length@data;
path = Transpose@{time, data};
ListLinePlot[path]

An example of a desired plot:

Example of desired output

$\endgroup$

3 Answers 3

5
$\begingroup$
$Version

"14.0.0 for Mac OS X ARM (64-bit) (December 13, 2023)"

Clear["Global`*"]

data = {-253, -219, -303, -333, -333, -340, -298, -229, -203, -150, \
-121, -113, -52, -67, -25, 180, 129};
time = Range@Length@data;
path = Transpose@{time, data};

ListLinePlot[{path, {path[[1]]}, {path[[-1]]}},
 PlotMarkers -> {"", 
   Style["O", ColorData[97][2], 12, Bold], 
   Style["X", ColorData[97][2], 12, Bold]}]

enter image description here

$\endgroup$
4
$\begingroup$

To draw some markers you can use "Epilog". However, you need to know the scaling, the x and y spread of the data. You get this by:

spread = MinMax /@ {time, data};
spread = -Subtract @@@ spread

{16, 520}

Using this you can draw markers, e.g. circles, with appropriate scaling. Eventually you need a bit of padding, so that the whole marker is displayed:

ListLinePlot[path, 
 Epilog -> {Green, Circle[path[[1]], 0.03 spread], 
   Circle[path[[-1]], 0.03  spread]}, PlotRangePadding -> {1, 0}]

enter image description here

$\endgroup$
2
  • 2
    $\begingroup$ You could also use Scaled[.03], rather than calculating the spread $\endgroup$
    – MelaGo
    Mar 4 at 22:11
  • $\begingroup$ @MelaGo You are right, this is simpler. +1 $\endgroup$ Mar 5 at 8:15
3
$\begingroup$

Since the x-coordinates range from 1 to the length of the data, a trimmed down implementation could be:

data = {-253, -219, -303, -333, -333, -340, -298, -229, -203, -150, \
-121, -113, -52, -67, -25, 180, 129};

ListLinePlot[data
 , Epilog -> {
   Red, Circle[{1, data[[1]]}, Offset[6]]
   , Blue, Text[Style["X", 16], {Length@data, data[[-1]]}]
   }
 ]

enter image description here

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