5
$\begingroup$

I just upgraded to Mathematica 10, and I am trying to get the hang of the new TimeSeries data objects, which are preventing my old strategy of overlaying data from different years on the same plot. As an example, comparing temperatures from different years:

d2014 = WeatherData["KMSP", "MeanTemperature", {{2014, 9}, {2014, 12}, "Day"}];
d2015 = WeatherData["KDAY", "MeanTemperature", {{2015, 9}, {2015, 10}, "Day"}];
DateListPlot[{d2014, d2015}]

The above code results in the image below; in Mathematica 9 I could manually shift the year of one data set so two data sets from different years would overlay like I wanted.

DateListPlot image

$\endgroup$

3 Answers 3

4
$\begingroup$

Rather than messing with the internals, let Mathematica do the work for you. The function you are looking for is TimeSeriesShift:

d2014 = WeatherData["KMSP", "MeanTemperature", {{2014, 9}, {2014, 12}, "Day"}];
d2015 = WeatherData["KDAY", "MeanTemperature", {{2015, 9}, {2015, 10}, "Day"}];
DateListPlot[{d2014, TimeSeriesShift[d2015, {-365, "Day"}]}]

Mathematica graphics

The advantage over changing things manually is the fact that you don't have to mess with date objects and so on: all the transformations are done transparently.

$\endgroup$
4
  • $\begingroup$ I agree, and I'm a bit embarrassed that I didn't come across the TimeSeriesShift function in the Documentation Center $\endgroup$
    – Mark
    Commented Oct 30, 2015 at 14:14
  • $\begingroup$ Not all years have 365 days... $\endgroup$ Commented Oct 4, 2022 at 0:14
  • $\begingroup$ @user2705196 Sure, but neither 2014 nor 2015, the two years in the question, were leap years, so they were indeed 365 days long. $\endgroup$
    – MarcoB
    Commented Oct 4, 2022 at 11:24
  • 1
    $\begingroup$ @MarcoB Absolutely true. I just commented because I ran into the same problem as the OP and used your elegant solution. And only at the very end I realized I was shifting things incorrectly for some of the years in my data set due to leap years. But TimeSeriesShift[myDATA, {-1, "Year"} worked beautifully. Thanks! $\endgroup$ Commented Oct 4, 2022 at 12:33
3
$\begingroup$

Here's another method using DateFunction to change how the dates are interpreted by DateListPlot

DateListPlot[{d2014, d2015}, 
  DateFunction -> ({2015}~Join~DateValue[#, {"Month", "Day"}] &)]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you, I was hoping that there was an option that could be passed to DateListPlot that would do the work for me. This is very helpful $\endgroup$
    – Mark
    Commented Oct 30, 2015 at 14:12
1
$\begingroup$
x1 = Normal @ d2014 /. {a_Integer, b_} :> {DateList[a][[1 ;; 3]], b};

x2 = Normal @ d2015 /. {a_Integer, b_} :> {DateList[a][[1 ;; 3]], b} /. (2015) -> 2014

DateListPlot[{x1, x2}]

enter image description here

$\endgroup$
5
  • 1
    $\begingroup$ @Mark you should not be so quick to mark something as accepted. There are other, less intrusive methods. $\endgroup$
    – rcollyer
    Commented Oct 30, 2015 at 13:54
  • $\begingroup$ Actually you can omit [[1 ;; 3]]. I routinely use this to drop hours, minutes and seconds. $\endgroup$
    – eldo
    Commented Oct 30, 2015 at 13:55
  • $\begingroup$ And actually, some experimentation showed that I could omit the entire following section and get the same result: {a_Integer, b_} :> {DateList[a][[1 ;; 3]], b} $\endgroup$
    – Mark
    Commented Oct 30, 2015 at 14:27
  • $\begingroup$ @rcollyer I'm new to this, and my initial understanding was the first answer to solve the problem won the vote; my bad :S $\endgroup$
    – Mark
    Commented Oct 30, 2015 at 14:29
  • $\begingroup$ @Mark in general, we recommend waiting a day before accepting an answer, just in case multiple answers are forthcoming. Note: you are always capable of switching who gets the checkmark. Since I'm one who might benefit from that, please don't take that as pressure to change it; I have plenty of reputation, already. $\endgroup$
    – rcollyer
    Commented Oct 30, 2015 at 14:38

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.