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
$\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$
  • $\begingroup$ I agree, and I'm a bit embarrassed that I didn't come across the TimeSeriesShift function in the Documentation Center $\endgroup$ – Mark Oct 30 '15 at 14:14
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$
  • $\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 Oct 30 '15 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$
  • 1
    $\begingroup$ @Mark you should not be so quick to mark something as accepted. There are other, less intrusive methods. $\endgroup$ – rcollyer Oct 30 '15 at 13:54
  • $\begingroup$ Actually you can omit [[1 ;; 3]]. I routinely use this to drop hours, minutes and seconds. $\endgroup$ – eldo Oct 30 '15 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 Oct 30 '15 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 Oct 30 '15 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 Oct 30 '15 at 14:38

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.