为何我的圆偏离中心?

为何我的圆偏离中心?
\usepackage[all]{xy}
\usepackage{pgfplots}

\begin{document}


 \begin{tikzpicture}
\begin{axis}[nodes near coords={(\coordindex)}]
\addplot[draw=none]
coordinates {
(1,2) (2,1) (2,3) (3.7, 2) };
\end{axis}
\foreach \point  in { (1,2), (2,1), (2,3), (3.7, 2)} {
  %  \draw[red,fill=red, opacity=0.2] \point circle (3pt);}
   \draw[opacity=0.2, fill=green] \point circle (1.1);
   \draw[fill=black] \point circle (2pt);  }
\end{tikzpicture}


\end{document}

生成:

四个圆圈

答案1

将循环移到axis环境中。由于其工作方式,您必须\point手动扩展变量()。

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[nodes near coords={(\coordindex)}]
\addplot[draw=none] coordinates { (1,2) (2,1) (2,3) (3.7, 2) };
\foreach \point  in { (1,2), (2,1), (2,3), (3.7, 2)} {
  \edef\tmp{%
    \noexpand\draw[opacity=0.2, fill=green] \point circle (1.1);
    \noexpand\draw[fill=black] \point circle (2pt);
  }
  \tmp
}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

您必须在轴环境(坐标系)内定义坐标。

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

\begin{document}

 \begin{tikzpicture}
\begin{axis}[nodes near coords={(\coordindex)}]
    \addplot[draw=none] coordinates { (1,2) (2,1) (2,3) (3.7, 2) };
    \coordinate (A) at (1,2);
    \coordinate (B) at (2,1);
    \coordinate (C) at (2,3);
    \coordinate (D) at (3.7,2);
\end{axis}
\foreach \point  in {(A),(B),(C),(D)} {
    %  \draw[red,fill=red, opacity=0.2] \point circle (3pt);}
    \draw[opacity=0.2, fill=green] \point circle (1.1);
    \draw[fill=black] \point circle (2pt);  }
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

您正在寻找这样的东西吗? 在此处输入图片描述

如果是这样,那么改变标记就\addplot比在环境外绘图更容易axis

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

\begin{document}
\begin{tikzpicture}
\begin{axis}[nodes near coords={(\coordindex)}]
\addplot[only marks,mark=*,mark size=1.1cm,fill=green,opacity=0.2] coordinates {(1,2) (2,1) (2,3) (3.7, 2)};
\addplot[only marks,mark=*,mark size=2pt,fill=black]               coordinates {(1,2) (2,1) (2,3) (3.7, 2)};
\end{axis}
\end{tikzpicture}
\end{document}

编辑:或者,定义样式和重复的坐标:

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

\newcommand{\mycoords}{(1,2) (2,1) (2,3) (3.7,2)}
\tikzset
{
  big circle/.style={only marks,mark=*,mark size=1.1cm,fill=green,opacity=0.2},
  small circle/.style={only marks,mark=*,mark size=2pt,fill=black}
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[nodes near coords={(\coordindex)}]
\addplot[big   circle] coordinates {\mycoords};
\addplot[small circle] coordinates {\mycoords};
\end{axis}
\end{tikzpicture}
\end{document}

相关内容