如何删除 pgfplots 中每个条目图例中的双杠?

如何删除 pgfplots 中每个条目图例中的双杠?

以下代码生成

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

我想删除图例中每个值 $W$ 的双线

在此处输入图片描述

答案1

您的代码存在一些与您未设置级别相关的问题compat。-始终设置兼容级别,除非您出于某些不为人知的原因想要在古老模式下运行。

添加single ybar legend到图中。

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

带图例的条形图

答案2

您可以将代码替换legend image code为您想要的图例条目的外观。例如,对于单个矩形:

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

在此处输入图片描述

相关内容