Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Graph by Date

  • To: mathgroup at smc.vnet.net
  • Subject: [mg93255] Re: [mg93212] Graph by Date
  • From: Darren Glosemeyer <darreng at wolfram.com>
  • Date: Sat, 1 Nov 2008 05:09:33 -0500 (EST)
  • References: <200810310806.DAA10661@smc.vnet.net>

Charles L. Snyder wrote:
> Hi
>
> Although I've searched the Mathematica documentation and this
> newsgroup's files, I am having trouble producing a Plot by Month -
> specifically a graph by month on the x-axis, and a total of the values
> in column 3 for that month: eg.,
>
> mydata = {{1/5/2004, "widgetA", 55.2},{1/15/2004, "widgetD32", 5.2},
> {2/7/2004, "no_Widgets", 34.5},{5/6/2004, "widget62", 100.2},
> {5/16/2004, "widget6", 30.5},{9/13/2004, "widget 55t", 225.79}}
>
> result would be  a graph of
> Jan 2004 57.4   (55.2+5.2)
> Feb 2004 34.2
> Mar 2004 0
> April 2004 0
> May 2004  130.7 (100.2 + 30.5)
> June 2004 0
> July 2004 0
> Aug 2004 0
> Sep 2004 225.79
> all other months 0
>
> I did find a way to summarize the dates and plot it:
>
> data = #[[1]] & /@ mydata;
> cs = Tally[Sort[data]];
>
> DateListPlot[cs, Filling -> Bottom,
>  FillingStyle -> Blue, PlotStyle -> {Red, PointSize[Large]},
>  PlotRange -> {{{2004, 1}, {2004, 12}}, Automatic},
>  DateTicksFormat -> {"Month", "/", "YearShort"}]
>
> Even this gives me the error messages for the dates such as -
> DateList::ambig: "Warning: the interpretation of the string \
> \!\(\"10/1/2004\"\) as a date is ambiguous"
>
> Thanks again
>
> clsnyder
>   

Here is one way.

In[1]:= mydata = {{"1/5/2004", "widgetA", 55.2}, {"1/15/2004", "widgetD32",
            5.2}, {"2/7/2004", "no_Widgets", 34.5}, {"5/6/2004", 
"widget62",
            100.2}, {"5/16/2004", "widget6", 30.5}, {"9/13/2004",
            "widget 55t", 225.79}};


The following function will be used in comparing the months of the data 
points. If your data were to run over multiple years, you could replace 
"Month" by {"Month", "Year"} to compare month and year.


In[2]:= stringfun[date_] :=
         DateString[{date, {"MonthShort", "DayShort", "Year"}}, "Month"]


Now split the data (with the middle column omitted) based on the month. 
This assumes the data are in chronological order.


In[3]:= monthgroups =
         Split[mydata[[All, {1, 3}]],
          stringfun[#1[[1]]] === stringfun[#2[[1]]] &]

Out[3]= {{{1/5/2004, 55.2}, {1/15/2004, 5.2}}, {{2/7/2004, 34.5}},
 
 >    {{5/6/2004, 100.2}, {5/16/2004, 30.5}}, {{9/13/2004, 225.79}}}


Take the month and year for the first element in each group and add up 
the second elements in each subgroup to get monthly totals.

In[4]:= groupedcounts =
         Map[{DateString[{#[[1, 1]], {"MonthShort", "DayShort",
               "Year"}}, {"Month", "/", "Year"}], Total[#[[All, -1]]]} &,
          monthgroups]

Out[4]= {{01/2004, 60.4}, {02/2004, 34.5}, {05/2004, 130.7}, {09/2004, 
225.79}}


Now the data can be plotted.


In[5]:= DateListPlot[groupedcounts]

Out[5]= -Graphics-


To fill in 0s for other months, you can create a table of 
{{"Month","/","Year"},0} pairs over the desired time range, select the 
dates not represented in the original data and combine that with the 
grouped data.


In[6]:= zeromonths =
         Select[Table[{DateString[{2004, i}, {"Month", "/", "Year"}], 
0}, {i,
            9}], Not[MemberQ[groupedcounts[[All, 1]], #[[1]]]] &]

Out[6]= {{03/2004, 0}, {04/2004, 0}, {06/2004, 0}, {07/2004, 0}, 
{08/2004, 0}}

In[7]:= DateListPlot[Join[groupedcounts, zeromonths]]

Out[7]= -Graphics-


Also in the DateListPlot sent, there is an ambiguity message issued 
because some date strings could be interpreted as month/day/year or 
day/month/year. The option DateFunction -> (DateList[{#, {"MonthShort", 
"DayShort", "Year"}}] &) could be added to avoid the ambiguity and the 
message.

Darren Glosemeyer
Wolfram Research


  • Prev by Date: Re: Re: Hypergeometric2F1
  • Next by Date: Re: Pure functions?
  • Previous by thread: Re: Graph by Date
  • Next by thread: Re: Expressions with ellipsis (...)