1
$\begingroup$

Dynamic IntervalSlider[] on a DateListPlot[] loses ability to change PlotRange after using Locator[].

If you start by adjusting the interval slider, everything works fine. The DateListPlot[] updates as the slider changes.

However, once you change the location of the epilog plot with the locator, the interval slider stops working as intended and the plot no longer dynamically updates with its values.

The interval slider code was from Mathematica examples on interval slider, and the locator epilog was from a search on plot within plot.

DynamicModule[{interval, ymin, ymax, xmin, xmax, d, pt = {3706819200, 0.5}},
 d = Transpose[{DateRange["Jan 1 2017", Today], 
    RandomReal[1, Length@DateRange["Jan 1 2017", Today]]}];
 {xmin, xmax} = AbsoluteTime /@ d[[{1, -1}, 1]];
 ymin = Min[Last /@ d];
 ymax = Max[Last /@ d];
 Column[{
   Show[
    DateListPlot[d, ImageSize -> Large, Epilog -> {
       Dynamic[
         Locator[Dynamic[pt],
         Plot[{-1, 0, 1}, {x, 0, 10}, Background -> None, 
          Axes -> False, PlotStyle -> Green]]
       ]}],
    PlotRange -> {Dynamic[interval], {ymin, ymax}}
    ],
   IntervalSlider[Dynamic[interval], {xmin, xmax, 1}, ImageSize -> {400, 30}, MinIntervalSize -> 1]
   }]
 ]

Is there something I'm missing here or is this a bug?

Edit

Also just tried it with Dynamic[Inset[]] and it doesn't work also.

$\endgroup$
3
$\begingroup$

First of all Locator[ Dynamic @ pt, ...] should work without Dynamic on top. Somehow it is not the case in Epilog so I usually put it in a separate Graphics in Show. For your case it matters even more because otherwise Plot needs to be recalculated while you are moving it.

Please try to narrow things down and report it to WRI Support. There is something fishy going on, once the Locator is moved, the PlotRange is overwritten, so your inner Dynamic vanishes. Putting Dynamic on top: PlotRange -> Dynamic[...] prevents that.

Another problem is that as soon as Locator is touched the PlotRangePadding is effectively set to 0. To fix it one needs to use PlotRangePadding -> Dynamic[..., None].

Putting things together:

DynamicModule[
  {interval, ymin, ymax, xmin, xmax, d, pt = {3706819200, 0.5}}
, d = Transpose[{DateRange["Jan 1 2017", Today], RandomReal[1, Length@DateRange["Jan 1 2017", Today]]}]
; {xmin, xmax} = AbsoluteTime /@ d[[{1, -1}, 1]]
; ymin = Min[Last /@ d]
; ymax = Max[Last /@ d]

; Column[
    { Show[
        DateListPlot[d, ImageSize -> Large]
      , Graphics[{
          Locator[Dynamic @ pt
          , Plot[{-1, 0, 1}, {x, 0, 10}, Axes -> False, PlotStyle -> Green]
          ]
        }]
      , PlotRange -> Dynamic@{interval, {ymin, ymax}}
      , PlotRangePadding -> Dynamic[Scaled[.05], None]        
      ]
    , IntervalSlider[Dynamic[interval], {xmin, xmax, 1}
      , ImageSize -> {400, 30}, MinIntervalSize -> 1
      ]
    }
  ]
]
$\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.