4
$\begingroup$

I would like to know what is the best way to configure one GridLine for the hour in LightGray and another for the days in Gray. In my tests, the FrameTicks are superposed by the GridLines and I don't know how to control the color. The data label should appear just for days.

Here is my attempt that I consider very clumsy.

data={#,RandomVariate[NormalDistribution[0,1]]}&/@RandomReal[{3564979299,3565355902},1000];

DateListPlot[data
   ,AspectRatio->0.2
   ,DateTicksFormat->{"Day","/","Month"}
   ,GridLines->{{#,LightGray}&/@DateRange[{2012,12,12},{2012,12,31},"Hour"],None}
   ,FrameTicks->{Automatic,{DateRange[{2012,12,12},{2012,12,31},"Day"],None}}
   ,Epilog->{Gray,Line[{{#,Min@data[[All,2]]},{#,Max@data[[All,2]]}}]&/@AbsoluteTime/@DateRange[{2012,12,12},{2012,12,31},"Day"]}
]

enter image description here

I don't like to use Epilog for that. It would be nice if I could have subticks between the days ticks too, one subtick for each hour, with no label.

$\endgroup$
4
$\begingroup$

To get rid of the Epilog you can change the setting for Gridlines as follows:

data = {#, RandomVariate[NormalDistribution[0, 1]]} & /@ 
  RandomReal[{3564979299, 3565355902}, 1000];


DateListPlot[data, AspectRatio -> 0.2, 
DateTicksFormat -> {"Day", "/", "Month"},
GridLines -> {Join[{#, LightGray} & /@ 
 DateRange[{2012, 12, 12}, {2012, 12, 31}, "Hour"], {#, Gray} & /@
  DateRange[{2012, 12, 12}, {2012, 12, 31}, "Day"]], None},
FrameTicks -> {Automatic, {DateRange[{2012, 12, 12}, {2012, 12, 31}, "Day"], None}}]

enter image description here

Update: Using @rm-rf's idea to change the color conditionally:

DateListPlot[data, AspectRatio -> 0.2, 
DateTicksFormat -> {"Day", "/", "Month"},
GridLines -> {{#, If[Last@# === 0, Gray, LightGray]} & /@ 
   DateRange[{2012, 12, 12}, {2012, 12, 31}, "Hour"], None}, 
FrameTicks -> {Automatic, {DateRange[{2012, 12, 12}, {2012, 12, 31}, "Day"], None}}]

Update 2: Further variations to specify both Gridlines and FrameTicks

 DateListPlot[data, AspectRatio -> 0.2,
  GridLines -> {{#, {LightGray, Gray}[[2 - Sign[Last[#]]]]} & /@
    DateRange[{2012, 12, 12}, {2012, 12, 31}, "Hour"], None},
  FrameTicks -> {Automatic, {{{#, "",
     {.005, 0}, Directive[Red, Thick]},
    {#, DateString[#, {"Day", "/", "Month"}],
     {0.01, 0}, Directive[Blue, Thickness[.01]]}}[[2 - Sign[Last[#]]]] & /@
  DateRange[{2012, 12, 12}, {2012, 12, 31}, "Hour"], None}}]

enter image description here

Or

 DateListPlot[data, AspectRatio -> 0.2,
  GridLines -> {{#, {LightGray, Gray}[[2 - Sign[Last[#]]]]} & /@
      DateRange[{2012, 12, 12}, {2012, 12, 31}, "Hour"], None},
  FrameTicks -> {Automatic, {If[Last@# === 0,
   {#, DateString[#, {"Day", "/", "Month"}], {0.01, 0}, 
        Directive[Blue, Thickness[.01]]},
   {#, "", {.005, 0}, Directive[Red, Thick]}] & /@
   DateRange[{2012, 12, 12}, {2012, 12, 31}, "Hour"], None}}]
$\endgroup$
  • $\begingroup$ Very nice!.. I did't know I could use Gridlines like that. Tks! +1 $\endgroup$ – Murta Dec 27 '12 at 0:35
  • $\begingroup$ is there a possibility to put GridLines in the foreground? $\endgroup$ – RMMA Dec 27 '12 at 12:50
  • $\begingroup$ @Frink, use the Method option with the setting Method -> {"GridLinesInFront" -> True}. $\endgroup$ – kglr Dec 27 '12 at 12:55
2
$\begingroup$

Using the fact that a valid "Day" using DateRange is already an element in the list returned using "Hour" (the last element is 0), you can use a simple replacement rule as follows for the GridLines:

GridLines -> {DateRange[{2012, 12, 12}, {2012, 12, 31}, "Hour"] /. 
    {d : {_, _, _, 0} :> {d, Gray}, h : {_, _, _, Except[0]} :> {h, LightGray}}, None}

An equivalent replacement rule using If:

x : {_, _, _, _} :> If[Last@x === 0, {x, Gray}, {x, LightGray}]
$\endgroup$
  • $\begingroup$ Cool.. there is another based in yours: If[Last@# === 0, {#, Gray}, {#, LightGray}] & /@ DateRange[{2012, 12, 12}, {2012, 12, 31}, "Hour"] $\endgroup$ – Murta Dec 27 '12 at 0:46

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.