Добавление меток к столбцам гистограммы

Добавление меток к столбцам гистограммы

Вот код, который мне сейчас нужен для генерации следующей гистограммы:

\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, гл. 4, стр. 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}

Связанный контент