I have a time series dataset I'm trying to plot by Year:

{{DateObject[{1991, 7}], 3.52659}, {DateObject[{1991, 8}], 
  3.18089}, {DateObject[{1991, 9}], 3.25222}, {DateObject[{1991, 10}],
   3.611}, {DateObject[{1991, 11}], 3.56587}, {DateObject[{1991, 12}],
   4.30637}, {DateObject[{1992, 1}], 5.08834}, {DateObject[{1992, 2}],
   2.81452}, {DateObject[{1992, 3}], 2.98581}, {DateObject[{1992, 4}],

I plotted it this way DateListPlot[data]:

Time Series

Now I'm trying to plot it by Year, following the example from here: http://www.wolfram.com/mathematica/new-in-10/semantic-data-import/analyze-retail-sales-data.html

But instead of grouping by City, I'm grouping by Year:

dataByYear = 
  GroupBy[data, (DateValue[#[[1]], 
       "Year"]) & -> ({DateValue[#[[1]], "MonthName"], #[[2]]} &)];

But when I try to plot it I get the following error message:

DateListPlot[dataByYear[[All]], PlotLegends -> Keys[dataByYear]]
<|1991->{{July ,3.526591`},{August ,3.180891`},{September
,3.252221`},{October ,3.611003`},{November ,3.565869`},{December
,4.306371`}},<<16>>,2008->{{January ,29.665356`},{February
,21.654285`},{March ,18.264945`},{April ,23.107677`},{May
,22.91251`},{June ,19.43174`}}|>
is not a valid dataset or list of datasets. >>

I checked the dataset returned by the GroupBy from Wolfram's example, and they do look very similar. The only difference seems to be the Keys are the name of the cities and in my case the Keys are the Years.

What's wrong with my code?


I abandoned the previous approach and decide to try plotting every year separately and them "joining" them (Sorry, I don't know the proper Mathematica term for this). This is how far I've managed to go:

Show[Table[DateListPlot[dataByYear[i]], {i, 1992, 2000}], PlotRange -> Automatic]

Second shot

Now how do I label and color each year in order to look something like the image below?


Any help would be much appreciated!

  • $\begingroup$ DateListPlot expects the abscissa to be a full date or Absolutetime, not just an integer or a string month name. Instead of using DateValue try DateList and also separate the values from the Keys using Norm or Part. $\endgroup$
    – rivercfd
    Commented Oct 30, 2014 at 14:08
  • $\begingroup$ I don't think that's it. The abscissa is not a string nor integer. It's a DateObject. And if I plot the same data with only one year with DateListPlot[dateByGroup[1992]] it works fine. It's the same data. $\endgroup$
    – ivanmp
    Commented Oct 30, 2014 at 14:22

3 Answers 3


With version 10

data = WeatherData["KATL", "Temperature", {{2004, 1}, {2014, 10}, "Month"}]
ds = TimeSeriesWindow[data, {{#, 1}, {#, 12}}] & /@ Range[2004, 2014];
ListPlot[ds[[#]]["Values"] & /@ Range@Length@ds, Joined -> True, 
Ticks -> {Transpose[{Range[12], {"Jan", "Feb", "Mar", "Apr", "May", 
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}}], Automatic}]


  • 1
    $\begingroup$ (+1) I cannot call this solution straightforward but it works. Obviously the TimeSeries functionality still needs polishing. $\endgroup$ Commented Oct 30, 2014 at 19:06
  • $\begingroup$ Mathematica was complaining (showing error messages) about my data when I tried to put it into a TimeSeries, but then I realized it was just a frontend problem. I followed through even with the error messages and indeed it worked! Thank you very, very much sir! $\endgroup$
    – ivanmp
    Commented Oct 30, 2014 at 19:21

Just to add some more options and expose some other time series functionality.

TimeSeriesSplit[ts_, dt_, align_: None] := Block[{f, tsa, dates},
  tsa = Sequence @@@ (TimeSeriesAggregate[ts, dt, f]["Values"]);
  dates = Internal`PartitionRagged[ts["Dates"], Length /@ tsa];
  If[align =!= None,
   TemporalData[tsa, {dates[[1, 1]], Automatic, align}]
   TemporalData[tsa, Partition[dates, 1]]

Lets try it out with some weather data.

data = WeatherData["KATL","Temperature", {{2004, 1}, {2014, 10}, "Month"}];

Without setting the last argument it just chunks the data.

DateListPlot@TimeSeriesSplit[data, "Year"]

enter image description here

Or if you set the optional align option...

DateListPlot@TimeSeriesSplit[data, "Year", "Month"]

enter image description here

Probably not very robust but it might serve as a nice starting point for someone.

  • $\begingroup$ This is very helpful. I'll try it out too. Thank you! $\endgroup$
    – ivanmp
    Commented Oct 31, 2014 at 10:09

Just for reference, I will post the code I used for the final solution. It wouldn't have been possible without PatoCriollo's help:

tdata = TemporalData[data[[All, 2]], {Map[DateString, data[[All, 1]]]}];
ds = TimeSeriesWindow[tdata, {{#, 1}, {#, 12}}] & /@ Range[1991, 2008];

ListPlot[ds[[#]]["Values"] & /@ Range@Length@ds, Joined -> True, 
 Ticks -> {Transpose[{Range[12], {"Jan", "Feb", "Mar", "Apr", "May", 
      "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}}], Automatic}, 
 PlotLegends -> Range[1991, 2008]]

Note that if I execute the first 2 lines without ; (semicolon) at the end, Mathematica will print nasty error messages.

I later realized that the problem was only at the frontend, when it tried to show me the results. My data is only at the "month" granularity level (no days, only month and year: "Jan 1994"), and that's causing problems with the Mathematica's frontend.

So I continued and executed the last line and sure enough Mathematica plot my data correctly, even with the previous error messages.

If you leave the ; after the first two lines, it won't try to show the result of those expressions and won't complain.

Here's the final plot:

Final plot thanks to PatoCriollo


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.