0
$\begingroup$

Suppose given a Timeseries as follows:

    timeseries = 
     TimeSeries[{{DateObject[{2019, 10, 11, 9, 30, 0.}, "Instant", "Gregorian", 
   "GMT"], Quantity[3.31034, 1/(
   "Days")]}, {DateObject[{2019, 10, 11, 12, 55, 12.7246}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[8.03562, 1/(
   "Days")]}, {DateObject[{2019, 10, 11, 12, 59, 38.6056}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[10.6702, 1/(
   "Days")]}, {DateObject[{2019, 10, 11, 19, 38, 7.59068}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[10.7163, 1/(
   "Days")]}, {DateObject[{2019, 10, 11, 19, 47, 14.0512}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[14.5236, 1/(
   "Days")]}, {DateObject[{2019, 10, 12, 11, 17, 38.344}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[10.1256, 1/(
   "Days")]}, {DateObject[{2019, 10, 12, 11, 24, 25.9655}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[12.473, 1/(
   "Days")]}, {DateObject[{2019, 10, 12, 15, 47, 23.4446}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[12.8601, 1/(
   "Days")]}, {DateObject[{2019, 10, 12, 15, 57, 29.7299}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[14.9472, 1/(
   "Days")]}, {DateObject[{2019, 10, 12, 16, 15, 27.5256}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[16.9695, 1/(
   "Days")]}, {DateObject[{2019, 10, 13, 11, 57, 49.6882}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[12.5608, 1/(
   "Days")]}, {DateObject[{2019, 10, 13, 12, 5, 33.0523}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[14.0108, 1/(
   "Days")]}, {DateObject[{2019, 10, 13, 12, 15, 42.4877}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[15.4455, 1/(
   "Days")]}, {DateObject[{2019, 10, 13, 20, 43, 19.3294}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[14.6496, 1/(
   "Days")]}, {DateObject[{2019, 10, 13, 20, 49, 7.69868}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[15.9309, 1/(
   "Days")]}, {DateObject[{2019, 10, 14, 13, 30, 23.8059}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[13.5239, 1/(
   "Days")]}, {DateObject[{2019, 10, 14, 13, 35, 41.3057}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[14.5704, 1/(
   "Days")]}, {DateObject[{2019, 10, 14, 13, 41, 43.7842}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[15.6127, 1/(
   "Days")]}, {DateObject[{2019, 10, 14, 13, 46, 54.216}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[16.391, 1/(
   "Days")]}, {DateObject[{2019, 10, 14, 20, 8, 55.3773}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[16.3049, 1/(
   "Days")]}, {DateObject[{2019, 10, 15, 10, 59, 29.7726}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[14.5725, 1/(
   "Days")]}, {DateObject[{2019, 10, 15, 11, 5, 52.2133}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[15.4151, 1/(
   "Days")]}, {DateObject[{2019, 10, 15, 14, 1, 7.18972}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[15.8583, 1/(
   "Days")]}, {DateObject[{2019, 10, 15, 14, 6, 28.1324}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[16.4715, 1/(
   "Days")]}, {DateObject[{2019, 10, 15, 18, 48, 11.7907}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[16.6273, 1/(
   "Days")]}, {DateObject[{2019, 10, 16, 0, 18, 58.2619}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[16.2788, 1/(
   "Days")]}, {DateObject[{2019, 10, 17, 12, 29, 30.7814}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[12.9294, 1/(
   "Days")]}, {DateObject[{2019, 10, 17, 12, 40, 8.33544}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[13.5091, 1/(
   "Days")]}, {DateObject[{2019, 10, 17, 12, 47, 24.3058}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[14.0923, 1/(
   "Days")]}, {DateObject[{2019, 10, 17, 14, 43, 44.6881}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[14.5118, 1/(
   "Days")]}, {DateObject[{2019, 10, 17, 14, 48, 27.909}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[15.0909, 1/(
   "Days")]}, {DateObject[{2019, 10, 17, 14, 52, 29.968}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[15.524, 1/(
   "Days")]}, {DateObject[{2019, 10, 17, 19, 48, 37.0089}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[15.4967, 1/(
   "Days")]}, {DateObject[{2019, 10, 17, 21, 50, 27.8946}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[15.8744, 1/(
   "Days")]}, {DateObject[{2019, 10, 17, 22, 1, 8.0006}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[16.4192, 1/(
   "Days")]}, {DateObject[{2019, 10, 18, 9, 30, 0.}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[15.6493, 1/(
   "Days")]}, {DateObject[{2019, 10, 25, 9, 30, 0.}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[8.28531, 1/(
   "Days")]}, {DateObject[{2019, 10, 30, 0, 5, 19.0775}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[6.40224, 1/(
   "Days")]}, {DateObject[{2019, 10, 30, 0, 8, 43.0075}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[6.50554, 1/(
   "Days")]}, {DateObject[{2019, 10, 30, 0, 37, 14.5114}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[6.60282, 1/(
   "Days")]}, {DateObject[{2019, 10, 30, 19, 29, 28.1093}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[6.5433, 1/(
   "Days")]}, {DateObject[{2019, 10, 30, 19, 34, 36.2571}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[6.7419, 1/(
   "Days")]}, {DateObject[{2019, 10, 30, 19, 39, 50.0777}, "Instant", 
   "Gregorian", "GMT"], 
  Quantity[6.9404, 1/("Days")]}, {DateObject[{2019, 11, 1, 9, 30, 0.},
    "Instant", "Gregorian", "GMT"], 
  Quantity[6.52652, 1/(
   "Days")]}, {DateObject[{2019, 11, 8, 9, 30, 0.}, "Instant", 
   "Gregorian", "GMT"], Quantity[4.99927, 1/("Days")]}}];

I want to plot the TimeSeries with conditional PlotMarkers which are red for values above 10.

Since I can't find the StylingFunction I tried to fake it with LabelingFunction as follows:

DateListPlot[timeSeries, PlotMarkers -> Automatic, 
 LabelingFunction -> (If[Last@#1 > 10, 
     Callout[Style["•", Red], Center]] &)]

But the result is very bad. Is there a proper way to do this?

$\endgroup$

1 Answer 1

1
$\begingroup$

1. You can post-process the output to change the color of Inset objects:

postProcess = # /.  GeometricTransformation[Inset[a_, b___], c_] :> 
     MapThread[GeometricTransformation[Inset[Style[a, #2], b], #] &,
     {{Select[#[[1, -1]] >= 10 &][c], Select[#[[1, -1]] < 10 &][c]}, {Red, Blue}}] &;

postProcess @ DateListPlot[timeseries, PlotMarkers -> Automatic]

enter image description here

2. Alternatively, generate a second plot with the option Joined -> False and style individual data elements using PlotStyle and use Show to combine the two plots:

Show[DateListPlot[timeseries],
 DateListPlot[List /@ timeseries["Path", 1], Joined -> False, 
  BaseStyle -> PointSize[Large], 
  PlotStyle -> (If[QuantityMagnitude[#2] >= 10, Red, Blue] & @@@ timeseries["Path", 1])]]

enter image description here

3. You can add markers as metadata and use the options LabelingSize and LabelingFunction to control the size and placement of the markers:

markers = If[#[[2, 1]] >= 10, Style[●, Red], Style[●, Blue]] & /@ timeseries["Path", 1]; 

DateListPlot[timeseries -> markers, 
 LabelingSize -> Medium, 
 LabelingFunction -> (Placed[#3[[3, 1]], Center] &), 
 GridLines -> {None, {10}}, 
 PlotLegends -> PointLegend[{Red, Blue}, {"more than 10", "less"}, 
   LegendMarkers -> Automatic, Joined -> False]]

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.