2

This is a follow-up question of pgfplot dateplot discrete dates on axis, which is answered by Peter Grill. The solution for plotting discrete dates is to use only unique dates.

However, when making two plots with corresponding dates on the axis, the dates are already known and thus not plotted in the second plot (see figure). Is there a way to forget the "Ticks Used" after the first plot? (I tried to undefine the macro \tick with: \let\tick=\undefined, but this doesn't work).

\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}
    \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}

enter image description here

3
  • Simplest solution would be to use unique names for each plot. So Tick Used \tick Plot 1 and Tick Used \tick Plot 2. Alternatively you would need to keep a list of the \csxdef and then at the end of the plot clear them. – Peter Grill Jan 19 '16 at 10:46
  • I get it, but since I auto-generate plots, and re-use the same scripts for different plots, this would not work for me – Joost Döbken Jan 19 '16 at 10:47
  • You can auto generate them with a name that includes a timestamp that each plot is generated, for instance, which would make them unique. – Peter Grill Jan 19 '16 at 10:48
1

If you don't want to use unique names, you can adapt the solution from How keep a running list of strings and then process them one at a time and keep a list of the the undef that need to be done. So, instead of the

\csxdef{Tick Used \tick}{}%

you instead use

\MarkTickAsUsed{Tick Used \tick}% 

Adding the following to the preamble will then clear this list at the start of every tikzpicture:

\newcommand\ClearTickList{}
\newcommand\MarkTickAsUsed[1]{%
    \xdef\ClearTickList{\ClearTickList\csundef{#1}\endgraf}% Store tick so that we can clear it later
    \csxdef{Tick Used \tick}{}%
}
\AtBeginEnvironment{tikzpicture}{\ClearTickList}

enter image description here

Code:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\usepackage{etoolbox}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.12}
\usepackage{filecontents}

%% https://tex.stackexchange.com/questions/14393/how-keep-a-running-list-of-strings-and-then-process-them-one-at-a-time
\newcommand\ClearTickList{}
\newcommand\MarkTickAsUsed[1]{%
    \xdef\ClearTickList{\ClearTickList\csundef{#1}\endgraf}% Store tick so that we can clear it later
    \csxdef{Tick Used \tick}{}%
}
\AtBeginEnvironment{tikzpicture}{\ClearTickList}



\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%
            \MarkTickAsUsed{Tick Used \tick}%
        }%
    },  
    ]
    \addplot table [x=date, y=value, col sep=comma] {data.csv};
    \end{axis}
    \end{tikzpicture}
    \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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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