상위 축 좌표를 사용하여 pgfplots 축 내부에 삽입을 배치하려면 어떻게 해야 합니까?

상위 축 좌표를 사용하여 pgfplots 축 내부에 삽입을 배치하려면 어떻게 해야 합니까?

을 사용하여 부모 축 내부에 삽입 축을 배치하고 싶습니다 pgplots. 삽입 축의 위치, 너비, 높이는 부모 축의 데이터 좌표를 사용하여 지정해야 합니다. 이것이 내가 지금까지 얻은 것입니다:

\documentclass{article} 
\usepackage{pgfplots}
\begin{document} 
\begin{tikzpicture}
  \begin{axis}[width=\textwidth,name=mainplot] 
    \addplot {x};
    \coordinate (insetSW) at (axis cs:-4,2); % south west corner of inset
    \coordinate (insetNE) at (axis cs:-2,4); % north east corner of inset
  \end{axis} 
  \begin{axis}[at={(insetSW)},anchor=south west,name=inset] 
    \addplot {x};
  \end{axis} 
\end{tikzpicture} 
\end{document}

삽입된 부분의 남서쪽 모서리가 올바른 위치에 있습니다. 북동쪽 모서리가 에 있도록 삽입물의 너비와 높이를 어떻게 계산할 수 있습니까 insetNE?

아니면 에 두 개의 좌표를 제공하여 축 크기를 지정할 수 있습니까 \begin{axis}[..]?

답변1

한 가지 가능한 해결책은 명령문 내부의 주어진 좌표로 너비와 높이를 계산하는 것입니다 let. 그 다음에계산 결과를 매크로에 저장삽입 축을 생성할 때 이 매크로를 사용하십시오. MWE는 다음과 같습니다.

\documentclass{article} 
\usepackage{pgfplots}
\usetikzlibrary{calc}
\begin{document} 
\begin{tikzpicture}  
  \pgfkeys{/tikz/savenumber/.code 2 args={\global\edef#1{#2}}}
  \begin{axis}[width=200pt,name=mainplot,xtick={-4,0},ytick={2,6},grid] 
    \addplot {x};
    \coordinate (insetSW) at (axis cs:-4,2); % south west corner of inset
    \coordinate (insetNE) at (axis cs:0,6); % north east corner of inset
    \path
      let
        \p1 = (insetSW),
        \p2 = (insetNE),
        \n1 = {(\x2 - \x1)},
        \n2 = {(\y2 - \y1)}
      in    
      [savenumber={\insetwidth}{\n1},savenumber={\insetheight}{\n2}];
  \end{axis}
  \begin{axis}[
    at={(insetSW)}, anchor=south west, name=inset, width=\insetwidth,
    height=\insetheight, xticklabels={}, yticklabels={}, scale only axis]
    \addplot {x};
  \end{axis} 
\end{tikzpicture}

% DEBUG
inset width: \insetwidth,
inset height: \insetheight,
\end{document}

어느 생산

위 코드의 출력 스크린샷

인세트는 왼쪽 하단 모서리가 (-4,2)이고 오른쪽 상단 모서리가 로 원하는 위치를 갖습니다 (0,6). 오류 방지를 위해 삽입축의 눈금 라벨을 제거했습니다 width/height is too small.... 키는 scale only axis삽입된 축에 제공되어야 축이 원하는 치수와 정확하게 일치합니다.

의견/대체 답변을 환영합니다!

관련 정보