Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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}}]
share|improve this answer
    
Very nice!.. I did't know I could use Gridlines like that. Tks! +1 –  Murta Dec 27 '12 at 0:35
    
is there a possibility to put GridLines in the foreground? –  RMMA Dec 27 '12 at 12:50
    
@Frink, use the Method option with the setting Method -> {"GridLinesInFront" -> True}. –  kguler Dec 27 '12 at 12:55
add comment

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}]
share|improve this answer
    
Cool.. there is another based in yours: If[Last@# === 0, {#, Gray}, {#, LightGray}] & /@ DateRange[{2012, 12, 12}, {2012, 12, 31}, "Hour"] –  Murta Dec 27 '12 at 0:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.