# How to set pgfplot dateplot increment?

With pgfplot and dateplot, How can I set the date increment on the horizontal axis? This is what I have so far:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.9}
\pgfplotsset{grid style={dashed}}

\begin{document}
\begin{tikzpicture}
\centering
\begin{axis}[
width=1.2\linewidth,
height=6cm,
xmin=2012-01-01,
xmax=2013-01-01,
date coordinates in=x,
xticklabel={\day-\month-\year},
x tick label style={rotate=45,anchor=north east},
grid=both,
ymin=-1,ymax=10,
xlabel={Date ($day-month-year$)},
ylabel={Wind velocity ($m/s$)}
]
\addplot[black] table [col sep=comma,y=wind speed (m/s)] {./wind_data.txt};
\end{axis}
\end{tikzpicture}
\end{document}


Which generates a plot of the absolute wind velocity in Metro Manila in 2012 in this case

How can a set the horizontal date increment, for example one month?

• I found a solution where you set all dates for the x-axis. Something like: xtick={2012-01-01,2012-02-01,2012-03-01....... ans so on}. But I don like this solution since setting the increment would be more elegant Nov 7 '15 at 10:31
• $day-month-year$ should be day-month-year or even \emph{day-month-year}. Feb 21 '16 at 13:20

You solution is the correct practise: to provide xtick with a suitable list.

However, a "suitable list" may include increments! Try this, for example:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\foreach \value in {2012-01-01,2012-...-01,2012-12-01} {\value\par}
\end{document}


You can combine multiple years into one list, but in that case, you have to provide the first two items in the list such that it can compute the increment by differencing:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\foreach \value in {
2012-01-01,2012-...-01,2012-12-01,
2013-01-01,2013-02-01,2013-...-01,2013-12-01,
}
{\value\par}
\end{document}


You can directly insert these \foreach values into the value of xtick. Since the patterns are the same, it is still tedious. The following small helper macro can simplify the tick generation to some extend:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.9}
\pgfplotsset{grid style={dashed}}

\pgfplotsset{
% a style which generates ticks for each year in '#1' to #2
% (inclusive), at day '#3'.
monthly ticks/.code args={#1#2#3}{%
\xdef\accum{}%
\foreach \year in {#1,...,#2} {%
%
% append a comma if it is not empty:
\ifx\accum\empty
\else
\xdef\accum{\accum,}%
\fi
%
% append the next year:
\xdef\accum{\accum\year-01-#3,\year-02-#3,\year-...-#3,\year-12-#3}%
}%
\message{Using monthly ticks=\accum^^J}%
\pgfkeysalso{/pgfplots/xtick/.expand once={\accum}}%
}%
}

\begin{document}
\begin{tikzpicture}
\centering
\begin{axis}[
width=1.2\linewidth,
height=6cm,
xmin=2012-01-01,
xmax=2013-01-01,
date coordinates in=x,
ymin=-1,ymax=10,
xticklabel={\day-\month-\year},
x tick label style={rotate=45,anchor=north east},
grid=both,
ymin=-1,ymax=10,
xtick distance=30,
xlabel={Date ($day-month-year$)},
ylabel={Wind velocity ($m/s$)},
%
monthly ticks={2011}{2013}{01},
]
\end{axis}
\end{tikzpicture}
\end{document}


• See the update in the other answer, due to some change in pgfplots. Feb 21 '16 at 15:07

As a complete novice in PGFplots I've blindly copied the very handy code provided by Dr Feuersänger above into my editor and have found that it will not work giving me the following error: Undefined control sequence. ]

To all the other newcomers in the PGFplots world note that it will work if you add date ZERO=2012-01-01, within the \begin{axis} portion of the code as follows: \begin{axis}[...,date ZERO = 2012-01-01,...]

Thank you for this very valueable contribution Dr Feuersänger .

• Welcome to TeX - LaTeX! This is a useful and correct addition to the answer of Christian Feuersänger. It should really be a comment or update on his answer, but you can't do that until you have more reputation. Feb 21 '16 at 15:06