3
$\begingroup$

How do I make a graph of a function which takes a date as input, over a full year?

[Edit] To clarify, I have an existing function, say f, which takes a date as input and returns a value between 0 and 90. I want to plot this function over the year 2016, so plot the value for every day in 2016.

My try

I can reduce this question to making a list like

Table[
 {d, f[d]},
 {d, DateObject[{2016, 1, 1}], DateObject[{2016, 12, 31}]}
 ]

and then doing ListPlot or something, but obviously I need to give Mathematica at least the step size (one day) and find a way to add a day to a date in such an iteration.

Maybe DateRange is an idea to make a list of dates of a year, but not sure how I would attach my function to that properly.

PS I'm on Mathematica 11.0

[Edit] As per request, my own function is a little big too put here, but for example function

g[d_] := (
  DateValue[d, "Month"] + DateValue[d, "Day"]
  )

Will do, I guess.

$\endgroup$
  • $\begingroup$ what do you want your graph to look like? $\endgroup$ – Conor Cosnett Aug 20 '16 at 16:22
  • $\begingroup$ I'm guessing you want date on x axis? what do you want on y axis? $\endgroup$ – Conor Cosnett Aug 20 '16 at 16:23
  • 1
    $\begingroup$ @ConorCosnett (sorry for the delay) No sorry I think you misinterpreted the question, I'm asking how to make a graph of a function which takes a date as input, I already have a (big) function which works fine, it returns a value between 0 and 90 given a date. I'll try to clarify the question, hope it helps $\endgroup$ – PHPirate Aug 20 '16 at 17:06
  • 1
    $\begingroup$ @ConorCosnett Edited question for example function $\endgroup$ – PHPirate Aug 20 '16 at 17:21
  • 1
    $\begingroup$ I fixed my function now @PHPirate $\endgroup$ – Conor Cosnett Aug 20 '16 at 17:21
3
$\begingroup$

solution with DateListPlot

Clear[f,g]
f = Function[x, DateObject[{2016, 1, 1}] + Quantity[(x-1), "Days"]]
g[d_] := (DateValue[d, "Month"] + DateValue[d, "Day"])
listOfDates = Table[{f[d], g[f[d]]}, {d, 1, 365}];

DateListPlot[listOfDates]    

enter image description here


ListPlot[Table[g[f[d]], {d, 1, 365}]]   

enter image description here


Here is a function which you give 1 to return the first day 1 in 2016 ect.

use Quantity

f = Function[x, DateObject[{2016, 1, 1}] + Quantity[(x-1), "Days"]]

Table[{f[d], g[f[d]]}, {d, 1, 365}]

enter image description here


$\endgroup$
  • $\begingroup$ For me, the addition of dates doesn't work... I get back for f[3] the date plus days, so 3 + DateObject[{2015, 12, 31}] (Oh, I'm on MMa 11, if that is relevant) $\endgroup$ – PHPirate Aug 20 '16 at 16:29
  • $\begingroup$ @PHPirate Could you describe the output of your function? $\endgroup$ – Conor Cosnett Aug 20 '16 at 16:44
  • 1
    $\begingroup$ Oh I just saw it, yes that works, but is there a possibility to put some real dates on the x-axis instead of day of year? Just for the fancy looks... Maybe the month? $\endgroup$ – PHPirate Aug 20 '16 at 17:29
  • 1
    $\begingroup$ Just googled for that, I think it should be fixed with using DateListPlot? Anyway, +1 for helping me learn that adding a number to a date doesn't work, but adding a quantity does! $\endgroup$ – PHPirate Aug 20 '16 at 17:32
  • 1
    $\begingroup$ @PHPirate I included DateListPlot now $\endgroup$ – Conor Cosnett Aug 20 '16 at 17:44

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.