如何使用“calc”库来计算坐标之和?

如何使用“calc”库来计算坐标之和?

为什么下面的代码会画一条倾斜的线?我以为总和 ($(1,6) + (2,0)$) 会返回一个点 (3,6)。

\documentclass[dvipsnames]{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{pgfplots,tikz}
\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}
\pgfplotsset{compat=newest}
\usepgflibrary{arrows}                                                  
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{tikzpicture}
  \begin{axis}[ axis lines = middle,
                xmin = -1,
                ymin = -5,
                xmax = 10,
                ymax = 14,
                domain = -1:10,
                xtick = {1,2,...,9},
                ytick = \empty,
                xlabel style={below right},
                ylabel style={above left},
                x tick label style={below},
                samples = 100,
                axis on top=true,
                xlabel = {$x$}, 
                ylabel = {$f$}
              ]


    \addplot[very thick, domain=0:11] {5 + x};
    \draw[thick, dashed] (1,6) -- ($(1,6) + (2,0)$);
  \end{axis}
\end{tikzpicture}

\end{document}

结果是:

在此处输入图片描述

提前致谢!

答案1

来自 pgfplots 文档:

为了表达相对位置(或长度),需要使用轴方向 cs。

所以你必须使用:

\draw[thick, dashed] (1,6) -- ($(1,6) + (axis direction cs:2,0)$);

在此处输入图片描述

例子:

\documentclass[dvipsnames]{article}
%\usepackage[utf8]{inputenc}% need for outdated TeX distributions
\usepackage{pgfplots}% loads tikz and xcolor
\pgfplotsset{compat=newest}

\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[ axis lines = middle,
                xmin = -1,
                ymin = -5,
                xmax = 10,
                ymax = 14,
                domain = -1:10,
                xtick = {1,2,...,9},
                ytick = \empty,
                xlabel style={below right},
                ylabel style={above left},
                x tick label style={below},
                samples = 100,
                axis on top=true,
                xlabel = {$x$}, 
                ylabel = {$f$}
              ]
    \addplot[very thick, domain=0:11] {5 + x};
    \draw[thick, dashed] (1,6) -- ($(1,6) + (axis direction cs:2,0)$);
  \end{axis}
\end{tikzpicture}
\end{document}

解释

轴内部的坐标axis cs默认使用。

来自文档:

的效果axis cs是应用任何自定义转换(包括symbolic x coords)、对数、数据缩放转换或 pgfplots 通常执行的任何操作并提供低级 pgf 坐标作为结果。

低级 pgf 坐标指的是坐标(rel axis cs:0,0)。这是轴区域的左下角(而不是轴的原点)。因此axis cs坐标是轴中的绝对位置。如果将它们相加,您会得到意想不到的结果:

在此处输入图片描述

代码:

\documentclass[dvipsnames]{article}
%\usepackage[utf8]{inputenc}% need for outdated TeX distributions
\usepackage{pgfplots}% loads tikz and xcolor
\pgfplotsset{compat=newest}

\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}
\usepgfplotslibrary{fillbetween}

\tikzset{point/.style={circle,fill=black,inner sep=1pt},>=latex'}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[ axis lines = middle,
                xmin = -1,
                ymin = -5,
                xmax = 10,
                ymax = 14,
                domain = -1:10,
                xtick = {1,2,...,9},
                ytick = \empty,
                xlabel style={below right},
                ylabel style={above left},
                x tick label style={below},
                samples = 100,
                axis on top=true,
                xlabel = {$x$}, 
                ylabel = {$f$},
              ]
    \addplot[very thick, domain=0:11] {5 + x};
    \path
      (1,6) coordinate(P) node[point,label=above:P]{}
      (2,0) coordinate(Q) node[point,label=above left:Q]{}
      (rel axis cs:0,0) node{x}
    ;
    \draw[blue!50!black] (1,6) -- ($(1,6) + (2,0)$);
  \end{axis}
  \path (0,0) coordinate(O) node[point,label=below:O]{};% origin of the rel axis cs
  \begin{scope}[->,red]
    \draw (O)--(P);
    \draw (O)--(Q);
    \draw (Q)--+(P)node[point]{};
  \end{scope}
\end{tikzpicture}
\end{document}

可以使用以下任一方法修复此问题

\draw[thick, dashed] (1,6) -- ($(1,6) + (axis direction cs:2,0)$);

或者

\draw[thick,dashed] (1,6) -- ($(1,6) + (2,0)-(0,0)$);

在此处输入图片描述

代码:

\documentclass[dvipsnames]{article}
%\usepackage[utf8]{inputenc}% need for outdated TeX distributions
\usepackage{pgfplots}% loads tikz and xcolor
\pgfplotsset{compat=newest}

\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}
\usepgfplotslibrary{fillbetween}

\tikzset{point/.style={circle,fill=black,inner sep=1pt},>=latex'}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[ axis lines = middle,
                xmin = -1,
                ymin = -5,
                xmax = 10,
                ymax = 14,
                domain = -1:10,
                xtick = {1,2,...,9},
                ytick = \empty,
                xlabel style={below right},
                ylabel style={above left},
                x tick label style={below},
                samples = 100,
                axis on top=true,
                xlabel = {$x$}, 
                ylabel = {$f$},
                clip=false
              ]
    \addplot[very thick, domain=0:11] {5 + x};
    \draw[thick,blue!50!black] (1,6) -- ($(1,6) + (2,0)$);
    \draw[thick,green!50!black] (1,6) -- ($(1,6) + (2,0)-(0,0)$);
    \draw[thick,dashed] (1,6) -- ($(1,6) + (axis direction cs:2,0)$);
    %
    \path
      (rel axis cs:0,0) coordinate(O) node[point,label=below:O]{}
      (0,0) coordinate(A) node[point,label=above left:A]{}
      (1,6) coordinate(P) node[point,label=above:P]{}
      (2,0) coordinate(Q) node[point,label=above left:Q]{}
    ;
    \begin{scope}[->,red]
      \draw[thick,dotted,cyan] (A)--(O);
      \draw (O)--(P);
      \draw (O)--(Q);
      \draw (Q)--+(P)node[point]{};
      \draw[thick,cyan] (Q) ++(P)-- +($(O)-(A)$)node[point]{};
    \end{scope}
  \end{axis}
\end{tikzpicture}
\end{document}

答案2

为了更好地理解发生了什么,坐标 (axis cs: 2,0) 是距离 (rel axis cs: 0,0) 的距离。而不是 (axis cs: 0,0)。y 方向上额外的 5 来自 [ymin=-5]。

\documentclass[dvipsnames]{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{pgfplots,tikz}
\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}
\pgfplotsset{compat=newest}
\usepgflibrary{arrows}                                                  
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{tikzpicture}
  \begin{axis}[ axis lines = middle,
                xmin = -1,
                ymin = -5,
                xmax = 10,
                ymax = 14,
                domain = -1:10,
                xtick = {1,2,...,9},
                ytick = \empty,
                xlabel style={below right},
                ylabel style={above left},
                x tick label style={below},
                samples = 100,
                axis on top=true,
                xlabel = {$x$}, 
                ylabel = {$f$}
              ]


    \addplot[very thick, domain=0:11] {5 + x};
    \coordinate (A) at (1,6);% axis cs: is the default
    \coordinate (B) at (2,0);
    \coordinate (origin) at (0,0);
  \end{axis}
  \draw[thick, dashed] (A) -- ($(A) + (B) - (origin)$);
\end{tikzpicture}

\end{document}

相关内容