3
$\begingroup$

Suppose I have the data for DateListPlot:

dates = DateList[{2002, 1, 2 #}] & /@ Range@600;
values = Accumulate@RandomReal[{-1, 1}, 600];

DateListPlot[Transpose@{dates, values}, Joined -> True, 
FrameTicks -> {{{#, #, {0, 0.05}} & /@ Range[-30, 30, 2], 
None}, {Automatic, None}}]

gives

enter image description here

But I want the longer FrameTicks for x-axes(same length as y-axes),so I try:

DateListPlot[Transpose@{dates, values}, Joined -> True, 
FrameTicks -> {{{#, #, {0, 0.05}} & /@ Range[-30, 30, 2], 
None}, {{#, #, {0, 0.05}} & /@ Range[2002, 2012, 1], None}}]

enter image description here

Errr~~~x-axes has gone~~What should I do?

$\endgroup$
1
  • 1
    $\begingroup$ The code would work, if the ticks for the x-axes wouldn't require a date format (e.g., like it's the case for ListPlot). $\endgroup$
    – Karsten7
    Commented Jun 23, 2015 at 9:14

2 Answers 2

3
$\begingroup$

There is just one pair of {} missing

DateListPlot[Transpose@{dates, values}, Joined -> True, 
 FrameTicks -> {{{#, #, {0, 0.05}} & /@ Range[-30, 30, 2], None}, 
  {{{#}, #, {0, 0.05}} & /@ Range[2002, 2012, 1], None}}]

enter image description here

By adding the extra {} a shortened date list is created, which is one of the possible date forms needed for the x-axis. Other possible forms are listed in the Details and Options section of DateListPlot.

$\endgroup$
0
1
$\begingroup$
dateTicksF[n_: 5, tl_: {0, .05}, style_: Thin] := Module[{ticks = 
 Graphics`DateListPlotDump`DateTicks[{#, #2}, n, 
   Graphics`DateListPlotDump`TickLength -> tl, Charting`TickStyle -> style]}, 
 Join[ticks, {{ticks[[2, 1]], "", 1.5 tl, Opacity[0] }}]] &

Examples:

dates = DateList[{2002, 1, 15 #}] & /@ Range@600;
values = Accumulate@RandomReal[{-1, 1}, 600];

DateListPlot[Transpose@{dates, values}, Joined -> True, 
 FrameTicks -> {{{#, #, {0, 0.05}} & /@ Range[-30, 30, 2], 
    None}, {dateTicksF[], None}}]

enter image description here

Use dateTicksF[10, {.03, .03}, Red] in place of dateTicksF[] to get

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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