如何在非日期坐标环境中使用日期坐标?

如何在非日期坐标环境中使用日期坐标?

如何使用日期(例如,2019-03-28 而不是 5.2)在下面的\draw和命令中指定 x 坐标?谢谢。\node

\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

作为一种解决方法,您可以在内部定义坐标axis并使用这些坐标来绘制括号。注意我过去常常raise将括号向下移动。

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

相关内容