如何才能将标题显示在分组栏的顶部?

如何才能将标题显示在分组栏的顶部?

我有一个带有分组条形的条形图。每组应包含组顶部的总和。

例子:

在此处输入图片描述

我的例子有两个问题:

  • 估计 x 位置
  • y 位置(高度)取决于第二根横线

我怎样才能将总数放到正确的位置?

该示例是使用此代码创建的:

\documentclass[border=5mm] {standalone}
\usepackage{pgfplots, pgfplotstable}

\begin{document}

  \begin{tikzpicture}
  \begin{axis}[
    title={Test},
    ybar, ymin=3,  
    bar width=5mm,
    enlarge y limits ={value=0.3,upper},
    symbolic x coords={a,b,c,d},
    xtick=data,
    nodes near coords, 
    nodes near coords align={anchor=north},%Move values in bar anchor=mid
  ]
  %Active
  \addplot [fill=blue] coordinates {
    ({a},15) [xa]
    ({b},25)
    ({c},35)
    ({d},55)};
  \addlegendentry{Active}
  %Inactive
  \addplot [fill=red] coordinates {
    ({a},60)
    ({b},50)
    ({c},40)
    ({d},30)}
      [yshift=5mm]
      node[pos=0] {a: 75}
      node[pos=.25] {b: 75}
      node[pos=.5] {c: 75}
      node[pos=1] {d: 45};
      \addlegendentry{Inactive}  
  \end{axis}
  \end{tikzpicture}
\end{document}

评论:

答案1

\documentclass[border=5mm] {standalone}
\usepackage{pgfplots, pgfplotstable}

\begin{document}

  \begin{tikzpicture}
  \begin{axis}[
    title={Test},
    ybar, ymin=3,  ymax=80,
    bar width=5mm,
    symbolic x coords={a,b,c,d},
    xtick=data,
    nodes near coords, 
    nodes near coords align={anchor=north},%Move values in bar anchor=mid
  ]
  %Active
  \addplot [fill=blue] coordinates {
    ({a},15) [xa]
    ({b},25)
    ({c},35)
    ({d},55)};
  \addlegendentry{Active}
  %Inactive
  \addplot [fill=red] coordinates {
    ({a},60)
    ({b},50)
    ({c},40)
    ({d},30)};
      \addlegendentry{Inactive};
   \node at (axis cs:a,65) {a: 75};
   \node at (axis cs:b,55) {b: 75};
   \node at (axis cs:c,45) {c: 75};
   \node at (axis cs:d,60) {d: 85};
  \end{axis}
  \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容