좌표를 합산하기 위해 "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사용자 정의 변환(포함), 로그, 데이터 스케일링 변환 또는 pgfplot이 일반적으로 수행하는 모든 작업을 적용 symbolic x coords하고 결과적으로 낮은 수준의 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

무슨 일이 일어나고 있는지 더 잘 이해하기 위해 좌표(축 cs: 2,0)는 (상대 축 cs: 0,0)로부터의 거리입니다. (축 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}

관련 정보