Wie entferne ich den doppelten Balken in der Legende für jeden Eintrag in pgfplots?

Wie entferne ich den doppelten Balken in der Legende für jeden Eintrag in pgfplots?

Der folgende Code generiert

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

Ich möchte den Doppelbalken in der Legende für jeden Wert $W$ entfernen

Bildbeschreibung hier eingeben

Antwort1

Ihr Code weist einige Probleme auf, die damit zusammenhängen, dass Sie die Ebene nicht festlegen compat. – Legen Sie immer die Kompatibilitätsebene fest, es sei denn, Sie möchten aus irgendeinem unklaren Grund im antiken Modus arbeiten.

single ybar legendZu den Grundstücken hinzufügen .

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

Balkendiagramm mit Legende

Antwort2

Sie können den Code durch das ersetzen legend image code, was auch immer Ihre Legendeneinträge aussehen sollen. Beispiel für ein einzelnes Rechteck:

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

Bildbeschreibung hier eingeben

verwandte Informationen