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}

여기에 이미지 설명을 입력하세요

관련 정보