固定幅のpgfplots図

Sep 03 2020

軸ラベルや凡例エントリを含む合計寸法が設定されたスタンドアロンのpgfplots図を作成するにはどうすればよいですか?

width=5cm, height=5cm,pgfplotsを使用してプロットの寸法を設定すると、ほぼこのサイズの合計値しか得られません。さらに、凡例が軸ボックスの外側に配置されている場合、凡例の寸法が指定された寸法に追加されます。マニュアルでは、(4.10.1一般的なスケーリングオプション)を指定しています。

pgfplotsは、軸ラベルと目盛りラベルに必要なサイズのみを推定することに注意してください。見積もりでは、軸ボックスの外側にあるものすべてに対して一定量のスペースを想定しています。これには、最終的な画像が規定の寸法よりもわずかに大きくなったり小さくなったりするという効果があります。

そしてさらに

scale only axis = false(デフォルト)の場合、pgfplotsは、ラベル、タイトル、目盛りを含む目的の幅を生成しようとします。

私のMWE:

\documentclass[crop]{standalone}

\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}

\begin{axis}[
%Grouping
    scale only axis=false,
    height=,
    width=10cm,
%Domain
    xmin=2000, xmax=2020,
    ymode=log,
%Ticks
    /pgf/number format/1000 sep={},
%Axis Labels
    ylabel= Price \text{[}\$(2020)/kg\text{]},
%Legend
    legend cell align={left},
    legend pos=outer north east,
    legend style={draw=none},
    legend entries={Entry},
]
\addplot[draw=none] coordinates {(2005,10000)};
\end{axis}

\end{tikzpicture}
\end{document}

以下は、JohnKormyloによって投稿されたソリューションの視覚的表現です。

回答

2 JohnKormylo Sep 03 2020 at 13:45

見た目はひどいですが、5cm x5cmの長方形に正確に収まります。

\documentclass[crop]{standalone}

\usepackage[utf8]{inputenc}
\usepackage{tikz}% reducndant
\usepackage{pgfplots}

\newsavebox{\temp}

\begin{document}
\savebox{\temp}{\begin{tikzpicture}

\begin{axis}[
%Grouping
    name=border,
    height=5cm,
    width=5cm,
%Domain
    xmin=2000, xmax=2020,
    ymode=log,
%Ticks
    /pgf/number format/1000 sep={},
%Axis Labels
    ylabel= Price \lbrack\$(2020)/kg\rbrack, %Legend legend cell align={left}, legend pos=outer north east, legend style={draw=none}, legend entries={Entry}, ] \addplot[draw=none] coordinates {(2005,10000)}; \end{axis} \end{tikzpicture}}% trial \begin{tikzpicture} \begin{axis}[ %Grouping name=border, height={\dimexpr 10cm-\ht\temp}, width={\dimexpr 10cm-\wd\temp}, %Domain xmin=2000, xmax=2020, ymode=log, %Ticks /pgf/number format/1000 sep={}, %Axis Labels ylabel= Price \lbrack\$(2020)/kg\rbrack,
%Legend
    legend cell align={left},
    legend pos=outer north east,
    legend style={draw=none},
    legend entries={Entry},
]
\addplot[draw=none] coordinates {(2005,10000)};
\end{axis}
\draw[red] (current bounding box.south west) rectangle (current bounding box.north east);
\draw[green] (current bounding box.south west) rectangle ++(5cm,5cm);

\end{tikzpicture}

\end{document}