4
$\begingroup$

This is very similar to my previous question. I want to use "DateListPlot" to visualize multiple time series (n) and see data values and legend as I move the cursor over plotted lines. To begin with, I have the following codes (preferable n > 2):

Stock1 = FinancialData["NVDA", "Close", {{2017, 1, 1}, Now}];
Stock2 = FinancialData["QQQ", "Close", {{2017, 1, 1}, Now}];
DateListPlot[{Stock1, Stock2},PlotLegends -> {"NVDA Price", "QQQ 
 Price"},PlotStyle -> {Red, Green}]

It produces the graph but does not show the data points as I move my mouse along the plotted lines. Any help would be greatly appreciated. Thank you in advance.

$\endgroup$
  • 1
    $\begingroup$ How about TradingChart: TradingChart[{"GE", {{2017, 1, 1}, Now}}]? $\endgroup$ – kglr Jul 27 '17 at 13:23
  • $\begingroup$ Any improvement on my answer is most welcome. $\endgroup$ – ramesh Sep 4 '17 at 16:57
1
$\begingroup$

You may use a Dynamic Epilog to create a plot that displays the values via MousePosition.

plot tracker

I use Nearest to speed up finding the values in the series.

nearStock1 = Nearest[First@# -> # & /@ MapAt[AbsoluteTime, {All, 1}]@Stock1];
nearStock2 = Nearest[First@# -> # & /@ MapAt[AbsoluteTime, {All, 1}]@Stock2];

The plot's Epilog has a bit of formatting in it but it is nothing out of the ordinary.

DateListPlot[{Stock1, Stock2},
 PlotLegends -> {"NVDA Price", "QQQ Price"},
 PlotStyle -> {Red, Green},
 Epilog ->
  Dynamic@Module[{s1, s2},
    With[{pos = MousePosition[{"Graphics", DateListPlot}, {0, 0}]},
     s1 = First@nearStock1@First@pos;
     s2 = First@nearStock2@First@pos;
     {
      Thin, LightGray, InfiniteLine[pos, {0, 1}],
      PointSize[Large],
      Darker@Red, Point[s1],
      Darker@Green, Point[s2],
      Inset[
       Grid[
        MapAt[NumberForm[#, {5, 2}] &, {All, 2}]@
         MapAt[DateString[#, "ISODate"] &, {All, 1}]@{s1, s2},
        ItemStyle -> {{Automatic}, Darker /@ {Red, Green}},
        Alignment -> {{Automatic, Decimal}, {Automatic}}
        ],
       Scaled[{.02, .98}],
       Scaled[{0, 1}]
       ]
      }
     ]]
  ]

Hope this helps.

$\endgroup$
  • $\begingroup$ @ Edmund, thank you for your time and answer. I really appreciate it. $\endgroup$ – ramesh Nov 6 '17 at 3:47

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.