如何在 pgfplot 中使用已定义的坐标?

如何在 pgfplot 中使用已定义的坐标?

以下是我目前所拥有的。

\newcommand\medlsquare[4]
{    
    \addplot[thick] coordinates { (#1, #2) (#1+#3, #2) (#1+#3, #2+#3) (#1, #2+#3) (#1, #2)
};

我想重复使用如下所示的坐标

\newcommand\medlsquare[4]
{    

    \coordinate (#4A) at (#1, #2);
    \coordinate (#4B) at (#1++3, #2);
    \coordinate (#4C) at (#1+#3, #2+#3);
    \coordinate (#4D) at (#1, #2+#3);
     
    // what is the option do do something like below?
     \addplot[thick] coordinates {/#4A, /#4B, /#4C, /#4D};
};
    

答案1

你也\addplot可以使用\draw plot

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\newcommand{\medlsquare}[4]{%    
    \coordinate (#4A) at (#1, #2);
    \coordinate (#4B) at (#1+#3, #2);
    \coordinate (#4C) at (#1+#3, #2+#3);
    \coordinate (#4D) at (#1, #2+#3);
    \draw plot[thick] coordinates {(#4A) (#4B) (#4C) (#4D) (#4A)};
}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[xmin=0,xmax=3,ymin=0,ymax=3]
        \medlsquare{1}{1}{1}{square}
    \end{axis}

    \draw[red] (squareA) -- (squareC); % created coordinates are still accessible
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容