4

With pgfplot using dateplot when plotting data over a small date range, some dates are shown multiple times to "fill" the horizontal axis. I wonder if it is possible to show only discrete dates. Since I automate the generation of figures I don't want to set the ticks manually.

Here my working example:

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.12}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
date,value
2015-01-01, 3.2
2015-01-02, 6.5
2015-01-04, 6.8
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\centering
\begin{axis}[
    date coordinates in=x,
    xticklabel={\day-\month-\year},
    x tick label style={rotate=45,anchor=north east},
    date ZERO=2015-01-01,
    grid=both,
    enlarge x limits=false,
    xlabel={Date (day-month-year)},
    ylabel={Value},
]
\addplot table [x=date, y=value, col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Stefan Pinnow
  • 29,535

1 Answers1

3

One possibility is to adapt the solution from pgfplot discrete integers on axis to define a macro with the tick label embedded in it and suppress the tick if that macro is already defined:

enter image description here

Related Question:

Code:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{etoolbox}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.12}
\usepackage{filecontents}

\begin{filecontents}{data.csv} date,value 2015-01-01, 3.2 2015-01-02, 6.5 2015-01-04, 6.8 \end{filecontents} \begin{document} \begin{tikzpicture} \centering \begin{axis}[ date coordinates in=x, xticklabel={\day-\month-\year}, x tick label style={rotate=45,anchor=north east}, date ZERO=2015-01-01, grid=both, enlarge x limits=false, xlabel={Date (day-month-year)}, ylabel={Value}, xticklabel={% \ifcsdef{Tick Used \tick}{}{% \tick% \csxdef{Tick Used \tick}{}% }% },
] \addplot table [x=date, y=value, col sep=comma] {data.csv}; \end{axis} \end{tikzpicture} \end{document}

Peter Grill
  • 223,288