3

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

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}

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.