
수행원이 답변, 두 번째 및 세 번째 하위 그림을 설정할 때 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]
세 가지 플롯 모두에 사용하는 것입니다. 경계 상자를 결정할 때 (ticklabels, ylabel) 왼쪽에 있는 항목은 고려되지 않도록 trim axis left
경계 상자를 변경하는 스타일입니다 . 이는 첫 번째 축의 눈금 레이블이 왼쪽 여백으로 들어갈 수 있는 원치 않는 부작용이 있지만 첫 번째 하위 그림 앞에 수평 공간을 추가하여 이를 방지할 수 있습니다. (아래 코드 예시를 참고하세요.)tikzpicture
axis
또한 사를 subfigure
조금 더 좁게 만들고 \hfill
그 사이에 선을 채우는 공간을 추가하는 데 사용했습니다.
즉, 개별 캡션을 추가하지 않을 경우 해당 작업에 잘못된 도구를 사용하고 코드를 필요 이상으로 길게 만드는 것입니다. groupplots
의 라이브러리를 추가 pgfplots
하고 동일한 환경에 세 이미지를 모두 추가하는 것이 좋습니다 groupplot
. (이전 질문에서는 이것을 생각하지 못했습니다.) groupplots
이런 종류의 작업을 매우 쉽게 만듭니다.
\captionof
패키지의 도움으로 아래 예와 같이 축을 기준으로 지정된 위치에 s를 caption
배치하여 하위 캡션을 추가할 수도 있습니다 . 하지만 올바른 상호 참조를 얻으려면 카운터를 조작하는 것이 필요한 것 같습니다.\node
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
코드용입니다.