5

I want to plot some data depending on dates, but as the date range is quite large (4-5 years) I would like to have ticks in the form dd.mm\\yyyy, is it possible ? It would be even better not to repeat the year for each tick, by displaying only year changes, but it is not a necessity ;)

Here is a working example :

\begin{tikzpicture}
    \begin{axis}[
    axis on top,
    enlarge x limits = false,
    date coordinates in=x,
    xticklabel=\day.\month.\year,
    date ZERO = 2013-08-13
]
\addplot[blue] coordinates {
    (2013-08-13, 5)
    (2013-08-18, 10)
    (2013-08-21, 4)
    (2013-08-26, 7)
    (2013-09-01, 1)
    (2013-09-02, 0)
    (2013-09-06, 4)
    (2013-09-11, 5.5)
    (2013-09-15, 3)
    (2013-11-13, 5)
    (2013-11-18, 10)
    (2013-11-21, 4)
    (2013-12-26, 7)
    (2014-01-01, 1)
    (2014-01-02, 0)
    (2014-01-06, 4)
    (2014-02-11, 5.5)
    (2014-02-15, 3)
};
\end{axis}
\end{tikzpicture}

I tried replacing xticklabel=\day.\month.\year by xticklabel={\day.\month\\ \year} without success.

Mammouth
  • 323

1 Answers1

3

Similar to axis labels and TikZ nodes in general, one way to introduce line breaks is to explicitly specify the text alignment by setting

xticklabel style={align=center}

In order to only print the year when it changes, you can store the previous label's year value in a macro and compare whether the current year label is equal to that:

xticklabel={
    \day.\month.\\\ifx\year\prevyear\else\year\fi%
    \xdef\prevyear{\year}%
}

Complete code

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\begin{document}

\begin{tikzpicture}
\edef\prevyear{}
    \begin{axis}[
    axis on top,
    enlarge x limits = false,
    date coordinates in=x,
    xticklabel={
        \day.\month.\\\ifx\year\prevyear\else\year\fi%
        \xdef\prevyear{\year}%
    },
    xticklabel style={align=center},
    date ZERO = 2013-08-13
]
\addplot[black] coordinates {
    (2013-08-13, 5)
    (2013-08-18, 10)
    (2013-08-21, 4)
    (2013-08-26, 7)
    (2013-09-01, 1)
    (2013-09-02, 0)
    (2013-09-06, 4)
    (2013-09-11, 5.5)
    (2013-09-15, 3)
    (2013-11-13, 5)
    (2013-11-18, 10)
    (2013-11-21, 4)
    (2013-12-26, 7)
    (2014-01-01, 1)
    (2014-01-02, 0)
    (2014-01-06, 4)
    (2014-02-11, 5.5)
    (2014-02-15, 3)
};
\end{axis}
\end{tikzpicture}
\end{document}
Jake
  • 232,450
  • I tried it, probably with a typo... works perfectly now, thank you ! – Mammouth Aug 06 '14 at 13:01
  • @Mammouth: Glad it works now. I edited my answer to show a way of only printing the year when it changes. – Jake Aug 06 '14 at 13:04
  • Thank you ! I don't want to ask too much, but is it possible to have the date in format like "feb. 2014" (with the year still on a different line) ? I guess it is possible by taking \month argument an transform it, but I don't know how... – Mammouth Aug 06 '14 at 13:20
  • I tried something like xticklabel={\xdef\mm{04} \ifx\month\mm\text{apr.}\\\ifx\year\prevyear\else\year\fi% \xdef\prevyear{\year}%} But when there is more than one month, they are displayed irregularly : not on the same baseline. – Mammouth Aug 06 '14 at 13:45