히스토그램의 막대에 레이블 추가

히스토그램의 막대에 레이블 추가

이것은 현재 다음 히스토그램을 생성해야 하는 코드입니다.

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}

\begin{axis}[
    ybar,
    ymin=0,
    xlabel={Classes},
    ylabel={Frequency},
    width=10cm,
    height=6cm,
    xtick=data,
    xticklabels={A, B, C, D}, % Add more labels as needed
    enlarge x limits=0.15,
    nodes near coords, % Add labels above each bar
    nodes near coords align={vertical},
    nodes near coords style={font=\tiny},
    ]
    ]
    
    \addplot[fill=blue!30] coordinates {(1, 15) (2, 12) (3, 8) (4, 10) };

    \addplot[fill=green!30] coordinates {(1, 14) (2, 11) (3, 13) (4, 9) };

\end{axis}
\end{tikzpicture}
\end{document}

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

막대 위에 순위를 표시하는 대신 그래픽이 이와 같이 보이도록 하고 싶습니다. 여기에 이미지 설명을 입력하세요

답변1

좋습니다. 여기에 한 가지 방법이 있습니다. 열쇠:

  • point meta=explicit symbolic각각에 추가\addplot
  • 각 좌표 뒤에 원하는 값/텍스트를 추가하십시오.[ ]

pgfplots 매뉴얼, Ch. 4, p. 자세한 내용은 114, 버전 1.18.1을 참조하세요.

결과

%\documentclass{article}
\documentclass[10pt,border=3mm,tikz]{standalone}    % nicer for development
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}

\begin{axis}[
    ybar,
    ymin=0,
    xlabel={Classes},
    ylabel={Frequency},
    width=10cm,
    height=6cm,
    xtick=data,
    xticklabels={A, B, C, D}, % Add more labels as needed
    enlarge x limits=0.15,
    nodes near coords, % Add labels above each bar
    nodes near coords align={vertical},
    nodes near coords style={font=\tiny},
    ]
    ]
    
    \addplot[fill=blue!30,
             point meta=explicit symbolic
    ] coordinates {
        (1, 15) [1]
        (2, 12) [2]
        (3, 8)  [4]
        (4, 10) [3]
    };

    \addplot[fill=green!30,
             point meta=explicit symbolic
    ] coordinates {
        (1, 14) [1]
        (2, 11) [3]
        (3, 13) [2]
        (4, 9)  [4]
        };

\end{axis}
\end{tikzpicture}
\end{document}

관련 정보