幅と高さの両方を与えながらプロットのアスペクト比を維持する

幅と高さの両方を与えながらプロットのアスペクト比を維持する

さて、読んだ後プロットボックスのアスペクト比を設定する方法キーを使用してプロットのアスペクト比を設定できます。つまり、を指定するか、この値にスケーリングし、定義済みのアスペクト比を維持するというaspect ratio方法です。widthheight

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

今、私はパッケージkeepaspectratioからキーを取得することは可能かどうか疑問に思っていました。graphicxheight そして width結果のプロットでは、以前に定義されたアスペクト比を維持するために、次元の 1 つが自動的に無視されます。次のようになります。

\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}

言うまでもなく、コメントを解除した後の 3 番目のプロットはkeep aspect ratio2 番目のプロットと同じになるはずです。

関連情報