TikZ/pgfplots: ¿Cómo distribuir uniformemente las subfiguras de tikzpicture después de ocultar las etiquetas de los ejes?

TikZ/pgfplots: ¿Cómo distribuir uniformemente las subfiguras de tikzpicture después de ocultar las etiquetas de los ejes?

Siguienteesta respuesta, al configurar ytick=\emptylas subfiguras segunda y tercera, las subfiguras no están distribuidas uniformemente, entonces, ¿cómo puedo solucionar este resultado dejando que todas las subfiguras tengan el mismo ancho y se distribuyan uniformemente 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}

ingrese la descripción de la imagen aquí

Respuesta1

La razón del diferente espacio aquí es una combinación de factores. Primero, debido a que el primer gráfico es más ancho que los dos últimos, la distancia desde el lado derecho del eje hasta el borde del eje subfigurees menor para el primero que para los dos últimos.

Además, los dos últimos gráficos son más estrechos que los subfigureentornos y, de forma predeterminada, el texto dentro de ellos está alineado a la izquierda. Para arreglar esa parte, agregue \centeringal inicio de cada subfigureentorno, pero eso no es suficiente para obtener la misma distancia entre los ejes 1 y 2 y 2 y 3, debido a lo que se describe en el primer párrafo anterior.

Ahora, bien podría haber otras formas, definitivamente no soy bueno para encontrar soluciones elegantes, pero la solución alternativa que pensé es usarlas \begin{tikzpicture}[trim axis left]para los tres gráficos. trim axis leftes un estilo que cambia el cuadro delimitador de tikzpicturemodo que cualquier cosa a la izquierda de axis(ticklabels, ylabel) no se considere al determinar el cuadro delimitador. Esto tiene el efecto secundario no deseado de que las etiquetas del primer eje pueden ir al margen izquierdo, pero puedes contrarrestarlo agregando algo de espacio horizontal antes de la primera subfigura. (Consulte el ejemplo de código a continuación).

También hice el subfiguresa un poco más estrecho y lo usé \hfillentre ellos para agregar espacio que llene la línea.

Dicho esto, si no va a agregar títulos individuales, entonces diría que está utilizando la herramienta incorrecta para el trabajo y haciendo que el código sea más largo de lo necesario. Sugeriría agregar la groupplotsbiblioteca de pgfplotsy agregar las tres imágenes en el mismo groupplotentorno. (No pensé en esto para tu pregunta anterior) groupplotshace que este tipo de cosas sea muy fácil de hacer.

Con la ayuda \captionofdel captionpaquete también puede agregar subtítulos, colocando \nodes con un text widthrelativo específico a los ejes, ejemplo a continuación. figureSin embargo, parece que es necesario hacer algunos malabarismos con el contador para obtener referencias cruzadas correctas.

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

La parte superior es para groupplotsel código, la parte inferior para subfigureel código.

ingrese la descripción de la imagen aquí

información relacionada