在 pgfplots 中通过调用节点填充路径遇到麻烦

在 pgfplots 中通过调用节点填充路径遇到麻烦

我尝试将 pgfplots 图的一些坐标存储到节点中,以便第二次使用它们。例如,我通过以下方式定义三个节点

\node[inner sep=0pt] (A) at (axis cs:2,2) {};
\node[inner sep=0pt] (B) at (axis cs:6,2) {};
\node[inner sep=0pt] (C) at (axis cs:3,4) {};

为了绘制一个实心三角形ABC在图表上。它的工作原理是使用“手动”方式

\draw[fill=cyan](axis cs:2,2)--(axis cs:6,2)--(axis cs:3,4)--(axis cs:2,2);

但我遇到了麻烦

\draw[color=red,fill=orange](A)--(B)--(C)--(A);

以下代码生成图像(tkz-euclide)仅用于

\tkzDrawPoints(A,B,C)
\tkzLabelPoints(A,B,C)

代码:

\documentclass[11pt,a4paper]{article}
\usepackage{tikz,pgfplots,tkz-euclide}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid=both, %none
minor x tick num=0,
minor y tick num=0,
width=10cm, height=10cm,
axis x line=middle, 
axis y line=middle, 
samples=100,
ymin=-3.2, ymax=5.2,
xmin=-1.2, xmax=7.2,
domain=-6:6,
xlabel=$x$,
ylabel={$y$},
axis equal image % per assi in scala 1:1
]
\node[inner sep=0pt] (A) at (axis cs:2,2) {};
\node[inner sep=0pt] (B) at (axis cs:6,2) {};
\node[inner sep=0pt] (C) at (axis cs:3,4) {};
\draw[color=red,fill=orange](A)--(B)--(C)--(A);
\draw[fill=cyan](axis cs:2,-2)--(axis cs:6,-2)--(axis cs:3,0)--(axis cs:2,-2);
\end{axis}
\tkzDrawPoints(A,B,C)
\tkzLabelPoints(A,B,C)
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

定义A,B,C,Dcoordinates 或使用

\draw[color=red,fill=orange](A.center)--(B.center)--(C.center)--cycle;

在此处输入图片描述

\documentclass[11pt,a4paper]{article}
\usepackage{tikz,pgfplots,tkz-euclide}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid=both, %none
minor x tick num=0,
minor y tick num=0,
width=10cm, height=10cm,
axis x line=middle, 
axis y line=middle, 
samples=100,
ymin=-3.2, ymax=5.2,
xmin=-1.2, xmax=7.2,
domain=-6:6,
xlabel=$x$,
ylabel={$y$},
axis equal image % per assi in scala 1:1
]
\node[inner sep=0pt] (A) at (axis cs:2,2) {};
\node[inner sep=0pt] (B) at (axis cs:6,2) {};
\node[inner sep=0pt] (C) at (axis cs:3,4) {};
%\draw[color=red,fill=orange](A)--(B)--(C)--(A);
\draw[color=red,fill=orange](A.center)--(B.center)--(C.center)--cycle;
\draw[fill=cyan](axis cs:2,-2)--(axis cs:6,-2)--(axis cs:3,0)--(axis cs:2,-2);
\end{axis}
\tkzDrawPoints(A,B,C)
\tkzLabelPoints(A,B,C)
\end{tikzpicture}
\end{document}

答案2

如果该pgfplots包不是必需的,你可以tikz单独使用它。@LoopSpace 编写了一种样式,可让你轻松绘制刻度轴

截屏

\documentclass[11pt,a4paper]{article}
\usepackage{tikz,pgfplots,tkz-euclide}

% LoopSpace style for simple graduated axis 
% from https://tex.stackexchange.com/a/510583/138900
\tikzset{
  axis/.style={
    ultra thick,
  },
  grid/.style={
    help lines,
    draw
  },
  pics/axes/.style args={#1:#2;#3:#4}{
    code = {
      \path[grid] (#1-.5,#3-.5) grid (#2+.5,#4+.5);
      \draw[->,axis] (#1 - .5,0) -- (#2 + .5,0);
      \draw[->,axis] (0,#3 - .5) -- (0,#4 + .5);
      \foreach \x in {#1,...,#2} {
        \ifnum\x=0\else
        \draw[axis] (\x,0) -- (\x,-.2);
        \node[below left] at (\x+.1,-.2) {\(\x\)};
        \fi
      }
      \node[below] at (#2+.5,-.2) {\(\pgfkeysvalueof{/tikz/pics/axes/x label}\)};
      \foreach \y in {#3,...,#4} {
        \ifnum\y=0\else
        \draw[axis] (0,\y) -- (-.2,\y);
        \node[below left] at (-.1,\y) {\(\y\)};
        \fi
      }
      \node[left] at (-.1,#4+.5) {\(\pgfkeysvalueof{/tikz/pics/axes/y label}\)};
      \node[below left] at (0,0) {0};
    }
  },
  pics/axes/.default={-6:6;-6:6},
  pics/axes/x label/.initial=x,
  pics/axes/y label/.initial=y,
}
% end LoopSpace style

\begin{document}
\begin{tikzpicture}
\pic{axes=-1:7;-3:5};
\coordinate (A) at (2,2) ;
\coordinate (B) at (6,2) ;
\coordinate (C) at (3,4) ;
\draw[color=red,fill=orange](A)--(B)--(C)--(A);
\draw[fill=cyan](2,-2)--(6,-2)--(3,0)--(2,-2);
\end{tikzpicture}
\end{document}

相关内容