Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2007

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

Search the Archive

Re: Re: Help with DateListPlot

  • To: mathgroup at smc.vnet.net
  • Subject: [mg82243] Re: [mg82221] Re: Help with DateListPlot
  • From: Darren Glosemeyer <darreng at wolfram.com>
  • Date: Tue, 16 Oct 2007 03:24:34 -0400 (EDT)
  • References: <fesq3k$p4n$1@smc.vnet.net> <200710150526.BAA00439@smc.vnet.net>

Peter Pein wrote:
> KTugbawa at gmail.com schrieb:
>   
>> How do you use the DateList Plot function when you have a 3-vector
>> database. The first column is the date, the second column contains the
>> names, and the last column contains the returns. I know that you have
>> to use a loop.
>>     
>
> I know I have not to use a loop ;-)
>
> The data looks like this:
>   
>> Date                          Item                       return
>> 1/2/01                       TBill                         0.67
>> 1/3/01                        SP                          0.78
>> 1/4/01                       inv                            0.5
>> 1/5/01                       inve                          0.6
>> ..
>> ..
>> ..
>> ..
>> 1/12/01                  Forc                            1.2
>> 1/2/02                     TBill                           0.4
>>  1/3/02                    SP                             0.2
>> 1/4/02                     inv                              0.6
>> 1/5/02                     inve                            0.9
>> ..
>> ..
>> ..
>> ..
>> 1/12/02                 Forc                             1.6
>>
>> This patterns continues until 1/6/2007
>>
>> I need to use DataListPlot to plot the returns  of each item.
>>
>>
>>     
>
> Let`s see how the file is read in:
>
> In[1]:=
> tbl = Import["thefile.txt", "Table"];
>
> have a look at the first line
>
> FullForm[tbl[[1]]]
> Out[2]//FullForm=
> {"1/2/01", "TBill", 0.67}
>
> We have to convert the string "mm/dd/yy" to a list {y,m,d}. Additionally 
> I add 2000 to the year:
>
> In[3]:=
> data = Apply[{ToExpression[StringJoin["{",
>     StringReplace[#1, "/" -> ","],
>    "}"]][[{3, 1, 2}]] + {2000, 0, 0}, #3} & , tbl, {1}]
> Out[3]=
> {{{2001, 1, 2}, 0.67}, {{2001, 1, 3}, 0.78}, {{2001, 1, 4}, 0.5}, 
> {{2001, 1, 5}, 0.6}}
>
> I do not have access to Mathematica 6 but according to 
> http://reference.wolfram.com/mathematica/ref/DateListPlot.html ,
>
> DateListPlot[data]
>
> should work.
>
>
> Regards,
>    Peter
>   

I just wanted to add a couple notes about using DateListPlot with 
different date specifications. The dates given to DateListPlot can be 
date lists, date strings, or absolute times. A few of the posts seemed 
to indicate that the dates needed to be date lists, so I wanted to point 
out that the dates can be strings or absolute times as well.

As mentioned by Bill Rowe, the date strings in this particular data set 
are ambiguous. The DateFunction option can be used to define how dates 
in data should be converted to date lists.

As examples, here are ways to use the DateFunction for the examples posted.

data = {{"1/2/01", "TBill", 0.67}, {"1/3/01", "SP", 0.78}, {"1/4/01",
    "inv", 0.5}, {"1/5/01", "inve", 0.6}};

This interprets the dates as m/d/y.

DateListPlot[data[[All, {1, -1}]],
 DateFunction -> (DateList[{#, {"Month", "Day", "Year"}}] &)]


This interprets the dates as d/m/y.

DateListPlot[data[[All, {1, -1}]],
 DateFunction -> (DateList[{#, {"Day", "Month", "Year"}}] &)]

Here David Annetts' date conversion function is used.

cdate[str_, base_: 2000] :=
 Module[{dy, mo, yr}, {dy, mo, yr} =
   ToExpression@StringSplit[str, "/"];
  {yr + base, mo, dy}]

DateListPlot[data[[All, {1, -1}]], DateFunction -> cdate]

This will use the conversion provided by Peter Pein.

DateListPlot[data[[All, {1, -1}]],
 DateFunction -> (ToExpression[
       StringJoin["{", StringReplace[#, "/" -> ","], "}"]][[{3, 1,
        2}]] + {2000, 0, 0} &)]


Darren Glosemeyer
Wolfram Research


  • Prev by Date: Re: format mixed integers & floats with text styling (see )
  • Next by Date: Re: format mixed integers & floats with text styling (see )
  • Previous by thread: Re: Help with DateListPlot
  • Next by thread: Background Color for a Panel