
下列的這個答案,當設定ytick=\empty
第二個和第三個子圖時,子圖分佈不均勻,那麼如何透過讓所有子圖具有相同的寬度並均勻分佈來修復此輸出textwidth
?
\RequirePackage{luatex85}
\documentclass{article}
\usepackage{pgfplots,caption,subcaption,mwe,showframe}
\pgfplotsset{compat=newest}
\begin{document}
\begin{figure}
\centering
\pgfmathsetlengthmacro{\myaxiswidth}{0.33\textwidth-width(" 300 ")}% subtract width of widest ticklabel, with a space on each side
\begin{subfigure}[t]{0.33\textwidth}
\begin{tikzpicture}
\begin{axis}[enlargelimits=false,width=\myaxiswidth, height=0.3\textheight, scale only axis]
\addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-a};
\end{axis}
\end{tikzpicture}
\end{subfigure}%
%
\begin{subfigure}[t]{0.33\textwidth}
\begin{tikzpicture}
\begin{axis}[enlargelimits=false,width=\myaxiswidth, height=0.3\textheight, scale only axis, ytick=\empty]
\addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-b};
\end{axis}
\end{tikzpicture}
\end{subfigure}%
%
\begin{subfigure}[t]{0.33\textwidth}
\begin{tikzpicture}
\begin{axis}[enlargelimits=false,width=\myaxiswidth, height=0.3\textheight, scale only axis, ytick=\empty]
\addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-c};
\end{axis}
\end{tikzpicture}
\end{subfigure}
\end{figure}
\end{document}
答案1
這裡空間不同的原因是多種因素綜合作用的。首先,因為第一個圖比後兩個圖寬,所以第一個圖從軸右到邊緣的距離subfigure
小於後兩個圖。
此外,最後兩個圖比subfigure
環境更窄,並且預設情況下其中的文字左對齊。要修復該部分,請\centering
在每個環境的開頭添加subfigure
,但這不足以使軸 1&2 和 2&3 之間的距離相等,因為上面第一段所述。
現在,很可能還有其他方法,我絕對不擅長找到優雅的解決方案,但我想到的解決方案是用於\begin{tikzpicture}[trim axis left]
所有三個圖。trim axis left
是一種變更 的邊界框的樣式,以便在確定邊界框時不考慮 (ticklabels, ylabel)tikzpicture
左側的任何內容。axis
這確實會產生不必要的副作用,即第一個軸的刻度標籤可能會進入左邊距,但您可以透過在第一個子圖之前添加一些水平空間來解決這個問題。 (請參閱下面的程式碼範例。)
我還使subfigure
sa 變窄了一點,並\hfill
在它們之間添加了填充線條的空間。
也就是說,如果您不打算添加單獨的標題,那麼我會說您使用了錯誤的工具來完成這項工作,並使程式碼變得比必要的更長。我建議添加groupplots
的庫pgfplots
,並在同一groupplot
環境中添加所有三個圖像。 (對於您之前的問題,我沒有想到這一點。)groupplots
使這種事情變得非常容易做到。
\captionof
在包的幫助下,caption
您還可以透過將\node
s放置在text width
相對於軸的指定位置來新增子標題,如下例。figure
不過,似乎需要對計數器進行一些處理才能獲得正確的交叉引用。
\RequirePackage{luatex85}
\documentclass{article}
\usepackage{pgfplots,caption,subcaption,showframe}
\captionsetup[subfigure]{labelformat=parens}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=newest}
\usepackage{cleveref}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{groupplot}[
group style={
group name=G,
group size=3 by 1,
y descriptions at=edge left,
horizontal sep=10pt % adjust as needed
},
enlargelimits=false,
width=0.33\textwidth-width("300"),
height=0.3\textheight,
scale only axis]
\nextgroupplot
\addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-a};
\nextgroupplot
\addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-b};
\nextgroupplot
\addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-c};
\end{groupplot}
\refstepcounter{figure} % to get correct
\node [text width=0.33\textwidth-width("300"),below right,align=center] at (G c1r1.south west) {\captionof{subfigure}{Subcaption for the first plot\label{a}}};
\node [text width=0.33\textwidth-width("300"),below right,align=center] at (G c2r1.south west) {\captionof{subfigure}{Subcaption for the second plot}};
\node [text width=0.33\textwidth-width("300"),below right,align=center] at (G c3r1.south west) {\captionof{subfigure}{Subcaption for the third plot}};
\addtocounter{figure}{-1}
\end{tikzpicture}
\caption{..}
\end{figure}
\begin{figure}
\centering
\pgfmathsetlengthmacro{\myaxiswidth}{0.33\textwidth-width("300 ")}
\hspace*{\dimexpr0.33\textwidth-\myaxiswidth}%
\begin{subfigure}[t]{0.3\textwidth}
\centering % added
\begin{tikzpicture}[trim axis left]
\begin{axis}[enlargelimits=false,width=\myaxiswidth, height=0.3\textheight, scale only axis]
\addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-a};
\end{axis}
\end{tikzpicture}
\caption{Subcaption for the first plot}
\label{b}
\end{subfigure}%
\hfill
\begin{subfigure}[t]{0.3\textwidth}
\centering % added
\begin{tikzpicture}[trim axis left]
\begin{axis}[enlargelimits=false,width=\myaxiswidth, height=0.3\textheight, scale only axis, ytick=\empty]
\addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-b};
\end{axis}
\end{tikzpicture}
\caption{Subcaption for the second plot}
\end{subfigure}%
\hfill
\begin{subfigure}[t]{0.3\textwidth}
\centering % added
\begin{tikzpicture}[trim axis left]
\begin{axis}[enlargelimits=false,width=\myaxiswidth, height=0.3\textheight, scale only axis, ytick=\empty]
\addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-c};
\end{axis}
\end{tikzpicture}
\caption{Subcaption for the third plot}
\end{subfigure}
\caption{...}
\end{figure}
\end{document}
頂部是groupplots
代碼,底部是subfigure
代碼。