在 pgfplots 中将标签作为数字放置在此图中每个条形的顶部

在 pgfplots 中将标签作为数字放置在此图中每个条形的顶部

我有一张条形图,其中数字似乎没有在每个条形的顶部居中。我尝试在 \node 区域中写入“center”,但没有成功。代码是使用 pgfplots 编写的。顶部的数字是每个条形的一些标签。以下是我的代码:

    \pgfplotsset{
    every axis/.append style={font=\large},}
\begin{tikzpicture}[scale=0.35]
  \begin{axis}[
  title=Data,
    xbar, xmin=0,
    every node near coord/.append style={font=\large},
    width=10cm, height=15cm,
    xlabel={\large A},
    symbolic y coords={M1,M2,M3,M4,M5,M6,M7,M8,M9,M10,M11,M12,M13,M14,M15},
    ytick=data,
    nodes near coords align={horizontal},
    ]
    \addplot [color=black,fill=black] coordinates {(0.930,M1) (0.868,M2) (0.857,M3) (0.799,M4) (0.741,M5) (0.858,M6) (0.862,M7) (0.862,M8) (0.862,M9) (0.862,M10) (0.864,M11) (0.865,M12) (0.861,M13) (0.866,M14) (0.868,M15)};
    \node[above right] at (axis cs:0.930,M1) {80};
    \node[above right] at (axis cs:0.868,M2) {50};
    \node[above right] at (axis cs:0.857,M3) {30};
    \node[above right] at (axis cs:0.799,M4) {60};
    \node[above right] at (axis cs:0.741,M5) {70};
    \node[above right] at (axis cs:0.858,M6) {50};
    \node[above right] at (axis cs:0.862,M7) {50};
    \node[above right] at (axis cs:0.862,M8) {80};
    \node[above right] at (axis cs:0.862,M9) {80};
    \node[above right] at (axis cs:0.862,M10) {60};
    \node[above right] at (axis cs:0.864,M11) {70};
    \node[above right] at (axis cs:0.865,M12) {70};
    \node[above right] at (axis cs:0.861,M13) {60};
    \node[above right] at (axis cs:0.866,M14) {60};
    \node[above right] at (axis cs:0.868,M15) {60};
  \end{axis}
\end{tikzpicture}

输出条形图,每个条形顶部带有数字作为标签

答案1

在选项规范中更改above right为:right\node

\documentclass{article}
\usepackage{pgfplots}

\pgfplotsset{
  every axis/.append style={font=\large},}

\begin{document}

\begin{tikzpicture}[scale=1]
  \begin{axis}[
  title=Data,
    xbar, xmin=0,
    %every node near coord/.append style={font=\large},
    width=10cm, height=15cm,
    xlabel={\large A},
    symbolic y coords={M1,M2,M3,M4,M5,M6,M7,M8,M9,M10,M11,M12,M13,M14,M15},
    ytick=data,
    %nodes near coords align={horizontal},
    ]
    \addplot [color=black,fill=black] coordinates {(0.930,M1) (0.868,M2) (0.857,M3) (0.799,M4) (0.741,M5) (0.858,M6) (0.862,M7) (0.862,M8) (0.862,M9) (0.862,M10) (0.864,M11) (0.865,M12) (0.861,M13) (0.866,M14) (0.868,M15)};
    \node[right] at (axis cs:0.930,M1) {80};
    \node[right] at (axis cs:0.868,M2) {50};
    \node[right] at (axis cs:0.857,M3) {30};
    \node[right] at (axis cs:0.799,M4) {60};
    \node[right] at (axis cs:0.741,M5) {70};
    \node[right] at (axis cs:0.858,M6) {50};
    \node[right] at (axis cs:0.862,M7) {50};
    \node[right] at (axis cs:0.862,M8) {80};
    \node[right] at (axis cs:0.862,M9) {80};
    \node[right] at (axis cs:0.862,M10) {60};
    \node[right] at (axis cs:0.864,M11) {70};
    \node[right] at (axis cs:0.865,M12) {70};
    \node[right] at (axis cs:0.861,M13) {60};
    \node[right] at (axis cs:0.866,M14) {60};
    \node[right] at (axis cs:0.868,M15) {60};
  \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

使用above right意味着放置的锚点将是south west解释垂直移动的锚点;向右改变意味着使用锚点west,因此不会产生垂直移动(相对于中心)。

相关内容