How to rotate TickMarks in DateListPlot?
Perhaps something like this?
data = {{{2006, 10, 1}, 10}, {{2006, 10, 15}, 12},
{{2006, 10, 30}, 15}, {{2006, 11, 20}, 20}};
p = DateListPlot[data];
Show[p,
Options[p, FrameTicks] /. s_String :> Rotate[s, 90 Degree]
]
If one prefers not to use Show
then:
p /. x : (FrameTicks -> _) :> (x /. s_String :> Rotate[s, 90 Degree])
Given that the original question already uses so many different options, I think the most obvious answer is the one that makes use of exactly these existing options. This requires no post-processing whatsoever. By post-processing, I mean the replacement rules s_String :> ...
data = {{{2006, 10, 1}, 10}, {{2006, 10, 25}, 12}, {{2006, 10, 30},
15}, {{2006, 11, 20}, 20}};
DateListPlot[
data,
FrameTicks ->
{
{Automatic, None},
{
Map[
{#,
Framed[
Rotate[
DateString[#, {"Year", "/", "Month", "/", "Day"}],
90 Degree]
, RoundingRadius -> 5,
FrameStyle -> None,
Background -> LightOrange
],
{0, .01}
} &,
data[[All, 1]]
],
None}
}
]
All the heavy lifting is done by the FrameTicks
option. The list data
contains all the information necessary to create the positions and text for the tick labels, more precisely we need only the first entries: data[[All, 1]]
.
The Map
command uses each date entry in data[[All, 1]]
to create a list that identifies a tick mark with a position and label. The label part is rotated by 90
degrees - I also chose to add a rounded rectangle background using Framed
, and make the tick mark extend outside the frame toward the label, by using a tuple {0, .01}
for the tick length specification.
This approach has some potential advantages, other than making full use of the existing options: in particular, making pattern replacements in a post-processing step may require more carefully crafted conditions (instead of s_String
) if some of the labels on the vertical axis happen to be customized with strings too (e.g., instead of purely numbers from 0
to 20
, you could have one special tick labeled by a word like "special value"
- then that word would unintentionally get rotated along with the date labels).
See also this answer to a closely related question, "Centering date labels over the year in a DateListPlot"
Mr.Wizard has shown you how to rotate the labels. Sometimes, it might also be useful to simply offset alternate labels so that it doesn't clash and is more readable. For example:
Block[{i = 1},
DateListPlot[data, DateTicksFormat -> {"Day", "/", "Month", "/", "Year"}] /.
s_String :> If[s =!= "" && OddQ[i], i += 1; s, i += 1; Column[{"", s}]]
]