
Gefolgtdiese Antwort, beim Festlegen ytick=\empty
der zweiten und dritten Unterabbildungen sind die Unterabbildungen nicht gleichmäßig verteilt. Wie kann ich also diese Ausgabe beheben, indem ich alle Unterabbildungen gleich breit mache und gleichmäßig verteile 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}
Antwort1
Der Grund für den unterschiedlichen Abstand ist eine Kombination aus mehreren Faktoren. Erstens ist der Abstand von der rechten Seite der Achse zum Rand beim ersten Diagramm kleiner subfigure
als bei den letzten beiden, weil das erste Diagramm breiter ist als die beiden letzten.
Außerdem sind die beiden letzten Diagramme schmaler als die subfigure
Umgebungen, und standardmäßig ist der Text darin linksbündig ausgerichtet. Um das zu beheben, fügen Sie \centering
am Anfang jeder subfigure
Umgebung hinzu, aber das reicht nicht aus, um einen gleichen Abstand zwischen den Achsen 1 und 2 und 2 und 3 zu erreichen, aufgrund dessen, was im ersten Absatz oben beschrieben wird.
Nun, es könnte durchaus andere Möglichkeiten geben, ich bin definitiv nicht gut darin, elegante Lösungen zu finden, aber der Workaround, der mir eingefallen ist, ist, \begin{tikzpicture}[trim axis left]
für alle drei Diagramme zu verwenden. trim axis left
ist ein Stil, der den Begrenzungsrahmen tikzpicture
so ändert, dass alles links von axis
(Ticklabels, Ylabel) bei der Bestimmung des Begrenzungsrahmens nicht berücksichtigt wird. Dies hat den unerwünschten Nebeneffekt, dass die Ticklabels der ersten Achse in den linken Rand gehen können, aber Sie können dem entgegenwirken, indem Sie vor der ersten Unterabbildung etwas horizontalen Abstand hinzufügen. (Siehe Codebeispiel unten.)
Ich habe die SA auch subfigure
etwas schmaler gemacht und \hfill
dazwischen Platz geschaffen, der die Linie ausfüllt.
Wenn Sie jedoch keine einzelnen Bildunterschriften hinzufügen möchten, würde ich sagen, dass Sie das falsche Werkzeug für die Aufgabe verwenden und den Code unnötig verlängern. Ich würde vorschlagen, die groupplots
Bibliothek von hinzuzufügen pgfplots
und alle drei Bilder in derselben groupplot
Umgebung hinzuzufügen. (Daran habe ich bei Ihrer vorherigen Frage nicht gedacht.) groupplots
macht diese Art von Dingen sehr einfach.
Mithilfe \captionof
des caption
Pakets können Sie auch Unterüberschriften hinzufügen, indem Sie \node
s mit einem angegebenen text width
relativen Wert zu den Achsen platzieren, Beispiel unten. Allerdings scheint etwas Jonglieren mit dem figure
Zähler erforderlich zu sein, um korrekte Querverweise zu erhalten.
\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}
Oben ist für groupplots
Code, unten für subfigure
Code.