¿Cómo eliminar la doble barra en la leyenda de cada entrada en pgfplots?

¿Cómo eliminar la doble barra en la leyenda de cada entrada en pgfplots?

El siguiente código genera

 \documentclass{standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    ybar,
    bar width=12,
    enlargelimits={abs=1.5},
    legend style={at={(0.75,0.9)},
      anchor=north,legend columns=-1},
    ylabel={\textbf{A}},
    xtick={1,2,3,4,5,6}, % Numeric coordinates for x-axis
    xticklabels={\textbf{E1}, \textbf{E2}, \textbf{E3}, \textbf{E4}, \textbf{E5}, \textbf{E6}}, % Corresponding labels
    nodes near coords={},
    nodes near coords align={vertical},
    width=15cm,
    height=8cm,
  ]
    
    % Dataset 1
    \addplot+[bar shift=-12, ybar, error bars/.cd, y dir=both, y explicit] coordinates {
      (1, 91.32) +- (0, 0.16)
      (2, 92.95) +- (0, 0.12)
      (3, 89.05) +- (0, 0.20)
      (4, 96.92) +- (0, 0.36)
      (5, 94.49) +- (0, 0.31)
      (6, 97.08) +- (0, 0.74)
    };
    \addlegendentry{$w=0$}
    
    % Dataset 2
    \addplot+[bar shift=0, ybar, error bars/.cd, y dir=both, y explicit] coordinates {
      (1, 92.8) +- (0, 0.22)
      (2, 92.6) +- (0, 0.18)
      (3, 93.1) +- (0, 0.25)
      (4, 93.32) +- (0, 0.16)
      (5, 93.47) +- (0, 0.30)
      (6, 93.39) +- (0, 0.41)
    };
    \addlegendentry{$w=1$}
    % Dataset 3
    \addplot+[bar shift=12, ybar, error bars/.cd, y dir=both, y explicit] coordinates {
      (1, 94.1) +- (0, 0.15)
      (2, 94.3) +- (0, 0.20)
      (3, 93.8) +- (0, 0.17)
      (4, 93.32) +- (0, 0.16)
      (5, 93.47) +- (0, 0.30)
      (6, 93.39) +- (0, 0.41)
    };
  

    % Add legend
    \addlegendentry{$w=2$}
    
  \end{axis}
\end{tikzpicture}

\end{document}

Quiero eliminar la doble barra en la leyenda para cada valor $W$

ingrese la descripción de la imagen aquí

Respuesta1

Tu código tiene algunos problemas relacionados con que no estableces el compatnivel. -Siempre establece el nivel de compatibilidad a menos que por alguna extraña razón quieras ejecutar en modo antiguo.

Añadir single ybar legenda las tramas.

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar=0,
enlargelimits={abs=1.5},
legend style={legend columns=-1},
ylabel={\textbf{A}},
xtick={1,2,3,4,5,6}, % Numeric coordinates for x-axis
xticklabels={\textbf{E1}, \textbf{E2}, \textbf{E3}, \textbf{E4}, \textbf{E5}, \textbf{E6}}, % Corresponding labels
width=15cm, height=8cm,
]
\addplot+[single ybar legend,error bars/.cd, y dir=both, y explicit] coordinates {
(1, 91.32) +- (0, 0.16)
(2, 92.95) +- (0, 0.12)
(3, 89.05) +- (0, 0.20)
(4, 96.92) +- (0, 0.36)
(5, 94.49) +- (0, 0.31)
(6, 97.08) +- (0, 0.74)
};
\addlegendentry{$w=0$}
\addplot+[single ybar legend, error bars/.cd, y dir=both, y explicit] coordinates {
(1, 92.8) +- (0, 0.22)
(2, 92.6) +- (0, 0.18)
(3, 93.1) +- (0, 0.25)
(4, 93.32) +- (0, 0.16)
(5, 93.47) +- (0, 0.30)
(6, 93.39) +- (0, 0.41)
};
\addlegendentry{$w=1$}
\addplot+[single ybar legend, error bars/.cd, y dir=both, y explicit] coordinates {
(1, 94.1) +- (0, 0.15)
(2, 94.3) +- (0, 0.20)
(3, 93.8) +- (0, 0.17)
(4, 93.32) +- (0, 0.16)
(5, 93.47) +- (0, 0.30)
(6, 93.39) +- (0, 0.41)
};
\addlegendentry{$w=2$}
\end{axis}
\end{tikzpicture}
\end{document}

Gráfico de barras con leyenda

Respuesta2

Puede reemplazar el código legend image codecon el aspecto que desee que tengan las entradas de la leyenda. Por ejemplo, para un solo rectángulo:

 \documentclass{standalone}
\usepackage{pgfplots}

\pgfplotsset{
 /pgfplots/ybar legend/.style={
        /pgfplots/legend image code/.code={
            \draw [##1,/tikz/.cd] (0cm,-0.3em) rectangle (0.2em,0.4em);
        },
    },
}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    ybar,
    bar width=12,
    enlargelimits={abs=1.5},
    legend style={at={(0.75,0.9)},
      anchor=north,legend columns=-1},
    ylabel={\textbf{A}},
    xtick={1,2,3,4,5,6}, % Numeric coordinates for x-axis
    xticklabels={\textbf{E1}, \textbf{E2}, \textbf{E3}, \textbf{E4}, \textbf{E5}, \textbf{E6}}, % Corresponding labels
    nodes near coords={},
    nodes near coords align={vertical},
    width=15cm,
    height=8cm,
  ]
    
    % Dataset 1
    \addplot+[bar shift=-12, ybar, error bars/.cd, y dir=both, y explicit] coordinates {
      (1, 91.32) +- (0, 0.16)
      (2, 92.95) +- (0, 0.12)
      (3, 89.05) +- (0, 0.20)
      (4, 96.92) +- (0, 0.36)
      (5, 94.49) +- (0, 0.31)
      (6, 97.08) +- (0, 0.74)
    };
    \addlegendentry{$w=0$}
    
    % Dataset 2
    \addplot+[bar shift=0, ybar, error bars/.cd, y dir=both, y explicit] coordinates {
      (1, 92.8) +- (0, 0.22)
      (2, 92.6) +- (0, 0.18)
      (3, 93.1) +- (0, 0.25)
      (4, 93.32) +- (0, 0.16)
      (5, 93.47) +- (0, 0.30)
      (6, 93.39) +- (0, 0.41)
    };
    \addlegendentry{$w=1$}
    % Dataset 3
    \addplot+[bar shift=12, ybar, error bars/.cd, y dir=both, y explicit] coordinates {
      (1, 94.1) +- (0, 0.15)
      (2, 94.3) +- (0, 0.20)
      (3, 93.8) +- (0, 0.17)
      (4, 93.32) +- (0, 0.16)
      (5, 93.47) +- (0, 0.30)
      (6, 93.39) +- (0, 0.41)
    };
  

    % Add legend
    \addlegendentry{$w=2$}
    
  \end{axis}
\end{tikzpicture}

\end{document}

ingrese la descripción de la imagen aquí

información relacionada