5

How can I specify the x coordinates in the \draw and \node commands below using dates (e.g., 2019-03-28 instead of 5.2)? Thanks.

\documentclass{article}

\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{pgfplots.dateplot}

\pgfplotsset{height=0.7\textwidth,width=\textwidth,compat=newest}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
      date coordinates in = x,
      date ZERO           = 2019-01-01,
      xmax                = 2019-06-30,
      xmin                = 2019-01-01,
      xtick               = {2019-01-01, 2019-02-01, 2019-03-01,
                              2019-04-01, 2019-05-01, 2019-06-01},
      xticklabels         = {J, F, M, A, M, J},
      ymax                = 5,
      ymin                = 0,
    ]
    \end{axis}
    \draw[decorate,decoration={brace,amplitude=4pt,mirror},yshift=-16pt]
      (0,0)--(5.2,0) node[black,midway,yshift=-10.5pt]{Q1};
    \draw[decorate,decoration={brace,amplitude=4pt,mirror},yshift=-16pt]
      (5.3,0)--(10.6,0) node[black,midway,yshift=-10.5pt]{Q2};
    \node [below=31pt] at (5.25,0) {Date};
  \end{tikzpicture}
\end{document}

1 Answer 1

3

As a workaround you could define coordinates inside the axis and use those to draw the braces. Note I used raise to move the braces down.

\documentclass{article}

\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\pgfplotsset{
  height=0.7\textwidth,
  width=\textwidth,
  compat=newest
}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
      date coordinates in = x,
      date ZERO           = 2019-01-01,
      xmax                = 2019-06-30,
      xmin                = 2019-01-01,
      xtick               = {2019-01-01, 2019-02-01, 2019-03-01,
                              2019-04-01, 2019-05-01, 2019-06-01},
      xticklabels         = {J, F, M, A, M, J},
      ymax                = 5,
      ymin                = 0,
    ]
    \coordinate (a) at (2019-01-01,\pgfkeysvalueof{/pgfplots/ymin});
    \coordinate (b) at (2019-03-30,\pgfkeysvalueof{/pgfplots/ymin});
    \coordinate (c) at (2019-06-30,\pgfkeysvalueof{/pgfplots/ymin});
    \end{axis}
    \draw[decorate,decoration={brace,amplitude=4pt,mirror,raise=15pt}]
      (a)--(b) node[black,midway,yshift=-25pt]{Q1};
    \draw[decorate,decoration={brace,amplitude=4pt,mirror,raise=15pt}]
      (b)--(c) node[black,midway,yshift=-25pt]{Q2};
    \node [below=31pt] at (b) {Date};
  \end{tikzpicture}
\end{document}
1
  • It took me a long time to figure out why your code works and my doesn't. It just so happens that the manually entered coordinates in this example don't contain a single digit number greater than 7 (i.e. 8 and 9), and mine does. If they did, the leading 0 would cause PGF Math's "base 8" error discussed in the comments of here. To see it in action, one can try changing the date in the a coordinate to 08. This bug doesn't occur if the coordinate is plotted through \addplot, which only adds to the head-scratching.
    – bongbang
    Apr 30, 2022 at 9:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .