너비와 높이를 모두 제공하면서 플롯의 종횡비 유지

너비와 높이를 모두 제공하면서 플롯의 종횡비 유지

알았어, 읽고 나면플롯 상자 종횡비를 설정하는 방법키를 사용하여 플롯의 종횡비를 설정할 수 있습니다. 즉, 주어진 값 에 맞게 크기를 조정하고 미리 정의된 종횡비를 유지하는 aspect ratio방식으로 설정할 수 있습니다.widthheight

\pgfplotsset{aspect ratio/.code args={#1:#2}{%
  \def\axisdefaultwidth{#1 cm}%
  \def\axisdefaultheight{#2 cm}%
}

이제 주어진 패키지 keepaspectratio와 같은 키를 갖는 것이 가능한지 궁금합니다.graphicxheight 그리고 width결과 플롯은 이전에 정의된 종횡비를 유지하기 위해 치수 중 하나를 자동으로 무시합니다. 다음과 같은 것 :

\pgfplotsset{keep aspect ratio/.code={%
  \pgfmathsetmacro{\currentratio}{\currentwidth/\currentheight}%
  \ifnum\currentratio<\predefinedratio%
     ignore height%
  \else%
     ignore width%
  \fi%
}

테스트용 더미 MWE는 다음과 같습니다.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{aspect ratio/.code args={#1:#2}{%
  \def\axisdefaultwidth{#1 cm}%
  \def\axisdefaultheight{#2 cm}},
  compat=newest
}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[aspect ratio=16:9, width=.8\textwidth]
        \addplot[domain=0:10] {0.5*x+6};
    \end{axis}
  \end{tikzpicture}

  \begin{tikzpicture}
    \begin{axis}[aspect ratio=4:3, width=.8\textwidth]
        \addplot[domain=0:10] {0.5*x+6};
    \end{axis}
  \end{tikzpicture}

  \begin{tikzpicture}
    \begin{axis}[aspect ratio=4:3,
                 width=.8\textwidth,
                 height=.8\textheight,
                 %keep aspect ratio
                 ]
        \addplot[domain=0:10] {0.5*x+6};
    \end{axis}
  \end{tikzpicture}
\end{document}

주석을 제거한 후의 세 번째 플롯은 keep aspect ratio두 번째 플롯과 동일해야 한다는 것은 말할 필요도 없습니다.

관련 정보