8
$\begingroup$

How can I plot times along the Y-axis, (with dates along the X-axis) e.g. {{DateObject[{2016,1,1}], TimeObject[{6,0,0}]}, {DateObject[{2016,1,2}], TimeObject[{8,0,0}]}} so that the labels on the Y-axis have reasonable values? (i.e., not using AbsoluteTime)

For example, if I have the list of the sunrise and sunset times for the next year, how can I plot them with dates along the x-axis and times along the y-axis?

$\endgroup$
  • $\begingroup$ Have you tried DateListPlot? $\endgroup$ – Mr.Wizard Jan 24 '16 at 5:58
  • $\begingroup$ That only works for the x-axis $\endgroup$ – Soumya Jan 24 '16 at 5:59
  • $\begingroup$ Sorry, I somehow ignored that detail. :o $\endgroup$ – Mr.Wizard Jan 24 '16 at 6:00
4
$\begingroup$

It looks like DataListPlot does not understand TimeObjects. You can convert them to Quantity "Hours" and plot this instead.

data = {{DateObject[{2016, 1, 1}], TimeObject[{6, 0, 0}]},
        {DateObject[{2016, 1, 2}], TimeObject[{8, 0, 0}]}};

hrData = MapAt[UnitConvert[Total@DateValue[#, {"Hour", "Minute", "Second"}, Quantity], "Hours"] &, {All, 2}]@data

{{DateObject[{2016, 1, 1}], Quantity[6, "Hours"]}, {DateObject[{2016, 1, 2}], Quantity[8, "Hours"]}}

Then plot hrData

DateListPlot[hrData, FrameLabel -> Automatic, 
 DateTicksFormat -> {"MonthNameShort", " ", "Day"}]

enter image description here

The y-axis is in hours and gets an automatic "h" frame label.

Hope this helps.

$\endgroup$
4
$\begingroup$

I can't seem to find a built-in way so I propose an extension of my old axisFlip routine:

axisFlip = # /. {x_Point | x_Line | x_GraphicsComplex :> MapAt[# ~Reverse~ 2 &, x, 1], 
     x : ((PlotRange | FrameTicks) -> _) :> x ~Reverse~ 2} &;

DateListPlot[{1, 1, 2, 3, 5, 8, 11}, {2000, 8}] // axisFlip

enter image description here

DateListPlot[{1, 1, 2, 3, 5, 8, 11}, {2000, 8}, Joined -> False] // axisFlip

enter image description here

This should work with TimeObject as well; let me know if it doesn't.

$\endgroup$
  • $\begingroup$ What if my x-axis is already a DateObject? I'll update the question. $\endgroup$ – Soumya Jan 24 '16 at 6:14
  • $\begingroup$ @Soumya I don't think I understand the problem; please describe it explicitly. Anyway I am sleepy and I'm going to bed, but I'll take another look tomorrow. $\endgroup$ – Mr.Wizard Jan 24 '16 at 7:35
  • $\begingroup$ In my case I'm trying to plot sunrise and sunset times, but I can imagine this being useful to track workout sessions, transit/commuting times, or even sleep habits. $\endgroup$ – Soumya Jan 24 '16 at 20:41
  • $\begingroup$ How can I get this to plot points (the default for DateListPlot) rather than a line? I tried changing x_Line to x_Point but it did not work, in the sense that I got a blank plot. Using the option PlotStyle -> PointSize[0.1] (or some other size) did not work either, still a blank plot. Thx $\endgroup$ – pdini Jan 15 '17 at 12:12
  • $\begingroup$ @pdini I included x_Point myself (updated answer) and now it works in e.g. DateListPlot[{1, 1, 2, 3, 5, 8, 11}, {2000, 8}, Joined -> False] // axisFlip in Mathematica 10.1.0. Which version are you using? $\endgroup$ – Mr.Wizard Jan 15 '17 at 15:33

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.