2

I am plotting some date-based data with pgfplots where the date is on the x axis, which works so far (see MWE below). However, I would like to have a second x axis which is linked to the first one such that it reads days or weeks relative to a specific date, as annotated in red in the attached example output.

I somehow have the feeling that this could be achieved in analogy to How to have linked axes on plots? but I don't get the entry point where the calendar calculation part comes into play.

\begin{filecontents*}{data.dat}
date;value
2018-06-02;10
2018-06-04;12
2018-06-05;13
2018-06-06;10
2018-06-11;11
2018-07-30;15
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usepackage{pgfcalendar}    % <-- to convert the dates to Julian integers
\usepackage{pgfplots}
\usepackage{pgfplotstable}  % <-- to manipulate the data file/table
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.14}
\pgfplotstableread[col sep=semicolon]{data.dat}\data
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            date coordinates in=x,
            xtick distance = 28,
            minor x tick num = 3]
            \addplot+ [only marks] table [x=date,y=value] {\data};
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

1

Here's an idea. Use \pgfcalendardatetojulian to convert a yyyy-mm-dd to a Julian day. Define a day zero, and calculate the Julian day of this. Then use xticklabel (not xticklabels) where \year, \month, \day are available, and calculate the Julian day of each tick. Finally use \inteval from xfp to subtract the zero day from the tick day and print the result.

See also some comments in the code.

enter image description here

\begin{filecontents*}{data.dat}
date;value
2018-06-02;10
2018-06-04;12
2018-06-05;13
2018-06-06;10
2018-06-11;11
2018-07-30;15
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{xfp} % for \inteval
\usepackage{pgfplotstable}  % loads pgfplots which loads tikz which loads pgfcalendar
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.14}
\pgfplotstableread[col sep=semicolon]{data.dat}\data
\begin{document}
\newcount\zeroday
\newcount\tickday
\pgfcalendardatetojulian{2018-06-01}{\zeroday}  % define a day zero

\begin{tikzpicture}
\begin{axis}[
    date coordinates in=x,
    xmin=2018-06-01, % to ensure same range, set xmin/xmax for both axes
    xmax=2018-07-30,
    xtick distance = 28,
    minor x tick num = 3,
]
   \addplot+ [only marks] table [x=date,y=value] {\data};
\end{axis}

\begin{axis}[
    date coordinates in=x,
    xmin=2018-06-01,
    xmax=2018-07-30,
    xtick distance=7,
    xticklabel={
      \pgfcalendardatetojulian{\year-\month-\day}{\tickday} % calculate julian day
      \inteval{\the\tickday-\the\zeroday} % subtract the zero-day, prints result
    },
    xtick pos=top,
    axis y line=none
]

     \addplot [draw=none] coordinates {(2018-06-02,10)}; % have to plot something

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