pgfplots 固定寬度的圖

pgfplots 固定寬度的圖

如何建立具有設定總尺寸(包括軸標籤和圖例條目)的獨立 pgfplots 圖形?

如果我用 pgfplots 設定繪圖尺寸width=5cm, height=5cm,只會給我一個總數字大約這個尺寸。此外,如果圖例放置在軸框外部,則圖例尺寸將會加到指定尺寸。手冊規定(4.10.1 常用縮放選項):

請注意,pgfplots 僅估計軸和刻度標籤所需的大小。這個估計假設軸框之外的任何內容都有固定的空間量。這會導致最終影像可能比規定尺寸稍大或稍小。

並進一步

如果僅縮放 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}

以下是發布的解決方案的直觀表示約翰·科米洛:

在此輸入影像描述

答案1

它看起來很糟糕,但它確實適合 5 厘米 x 5 厘米的矩形。

示範

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

相關內容