I have an unidimensional array (let's call it list)ranging from 0 to 86400, each one representing a second of a day (a day has 86400 secs).

For example,

list = {73496.3, 85974.6, 86085., 86136., 16.6256, 19.6975, 25.8896, 26.1856, 26.3937}

enter image description here

My goal is to change the ticks of plotted list with ListPlot, so that instead of the numbers, it gives you the hour. Also, there must be 24 ticks, each one representing one hour.

For example, the number 49616 would become 13:46:56

  • $\begingroup$ ListPlot[list, PlotRange -> {0, 24 60 60}, Ticks -> {Automatic, Array[{3600 #, #} &, 24]}]? $\endgroup$ – Dr. belisarius Feb 19 '14 at 0:40
  • $\begingroup$ Well, I meant "13:00" and so, but I can use yours too. I don't know how I didn't think of that… Thanks $\endgroup$ – Arcotick Feb 19 '14 at 0:43
  • 1
    $\begingroup$ ToString[DateString[ DateList[{0, 0, 0, 0, 0, #}]*{0, 0, 0, 1, 1, 1}, {"Hour", ":", "Minute", ":", "Second"}]] & /@ list will give you a list of string labels of the format you want corresponding to your list seconds values. These can be used to label as you see fit. $\endgroup$ – ciao Feb 19 '14 at 0:52

You have a few options, the easiest of them being ListPlot[list/3600] :) To change the ticks to your desired format, you'll have to generate them manually. Here's an example:

    toHrMin = DateString[# {"Hour", ":", "Minute"}] &, 
    ticks = Range[0, 24, 4] 3600},

    ListPlot[list, Ticks -> {Automatic, Transpose@{ ticks, toHrMin /@ ticks}}]

You can change the last tick to 24:00 or (+1) 00:00 if you want.

  • $\begingroup$ @Kuba posted something like this in a comment and then deleted it: ListLinePlot[Quantity[list, "Seconds"], TargetUnits -> "Hours"] $\endgroup$ – Dr. belisarius Feb 19 '14 at 1:07
  • $\begingroup$ @belisarius I saw that and also agree with your deleted comment :) While those Quantity things are cool, it isn't a general solution, especially when the ticks need to be converted to something more complicated. Nevertheless, it's a good answer to this question and Kuba should post it as an answer. $\endgroup$ – rm -rf Feb 19 '14 at 1:17
  • $\begingroup$ At least it deserves to be kept as a comment. Not that I recommend it though:) $\endgroup$ – Dr. belisarius Feb 19 '14 at 1:24
  • $\begingroup$ @belisarius I've deleted it because of what rm-rf said :) but ok, I'll post this. p.s. I think "@" reminder is not working well lately, I've seen those comments by accident. $\endgroup$ – Kuba Feb 19 '14 at 6:08
  • $\begingroup$ @Kuba The ping thing works only if you already participated in the conversation. I knew you weren't going to be notified, but it wasn't important enough to try to get your attention. I just wanted to set the attribution for the idea $\endgroup$ – Dr. belisarius Feb 19 '14 at 14:10

Just another approach, Ticks have still to be reformatted but one does not have to worry about conversion from seconds to hours:

ListPlot[Quantity[list, "Seconds"], TargetUnits -> "Hours", 
          Ticks -> {Automatic,  Array[{#, StringForm["``:00", #]} &, 9, {0, 24}]}]

enter image description here

p.s. Array will not work this way for < V.9 so use Table instead.


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.