显示 y 条堆积图的最大值

显示 y 条堆积图的最大值

以下 MWE 生成 y 条堆积图:

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
       \begin{axis}[
                   ybar stacked,
                   enlargelimits=0.15,
                   xlabel={Number of cores},
                   xtick={12},
                   ylabel={Cores load (\%)},
                   ymin=0, ymax=100,
                   ]
         \addplot+[ybar, black, fill=red] plot coordinates { (12,3.53)};
         \addplot+[ybar, black, fill=green!40] plot coordinates { (12,8.34)};
         \addplot+[ybar, black, fill=blue] plot coordinates { (12,6.76)};
         \addplot+[ybar, black, fill=cyan] plot coordinates { (12,4.6)};
         \addplot+[ybar, black, fill=magenta] plot coordinates { (12,9.5)};
         \addplot+[ybar, black, fill=yellow] plot coordinates { (12,14.45)};
         \addplot+[ybar, black, fill=gray] plot coordinates { (12,26.12)};
         \addplot+[ybar, black, fill=purple] plot coordinates { (12,8.45)};
         \addplot+[ybar, black, fill=orange] plot coordinates { (12,9.75)};
         \addplot+[ybar, black, fill=brown] plot coordinates { (12,3.18)};
         \addplot+[ybar, black, fill=red!40] plot coordinates { (12,4.87)};
         \addplot+[ybar, black, fill=green] plot coordinates { (12,0.46)};
      \end{axis}
   \end{tikzpicture}
\end{document}

一个条形图表示一个时间范围,分为 X 个子范围。为清楚起见,此示例仅显示条形图 12。每个子范围的值表示在此范围内所花费时间的百分比,因此子范围的总和为 100%。我想显示(在条形图顶部,或者可能在旋转的子范围内)子范围百分比中的最大值。我该如何实现?

答案1

一些评论

  • compat=1.12是必不可少的,否则元数据将是累积值而不是真实值。

    • 也就是说,最上面的那个总是最大的,结果是 100%)
    • 我不是说它是必要的。只是不知为何我不想去追究差异从何而来。
  • nodes near coords是必不可少的,只要您想自己绘制/放置节点,则不是必需的。

  • \pgfplotspointmetatransformed范围从 0 到 1000。因此,检查它是否大于 999 可能表示最大的数据点。

\documentclass{standalone}
\usepackage{pgfplots}\pgfplotsset{compat=1.12}

\begin{document}
\begin{tikzpicture}
       \begin{axis}[
                   ybar stacked,
                   enlargelimits=0.15,
                   xlabel={Number of cores},
                   xtick={12},
                   ylabel={Cores load (\%)},
                   ymin=0, ymax=100,
                   nodes near coords={
                      \ifdim\pgfplotspointmetatransformed pt>999pt
                         \kern50pt\pgfmathprintnumber{\pgfplotspointmeta}\%
                      \fi
                   }
                   ]
         \addplot+[ybar, black, fill=red] plot coordinates { (12,3.53)};
         \addplot+[ybar, black, fill=green!40] plot coordinates { (12,8.34)};
         \addplot+[ybar, black, fill=blue] plot coordinates { (12,6.76)};
         \addplot+[ybar, black, fill=cyan] plot coordinates { (12,4.6)};
         \addplot+[ybar, black, fill=magenta] plot coordinates { (12,9.5)};
         \addplot+[ybar, black, fill=yellow] plot coordinates { (12,14.45)};
         \addplot+[ybar, black, fill=gray] plot coordinates { (12,26.12)};
         \addplot+[ybar, black, fill=purple] plot coordinates { (12,8.45)};
         \addplot+[ybar, black, fill=orange] plot coordinates { (12,9.75)};
         \addplot+[ybar, black, fill=brown] plot coordinates { (12,3.18)};
         \addplot+[ybar, black, fill=red!40] plot coordinates { (12,4.87)};
         \addplot+[ybar, black, fill=green] plot coordinates { (12,0.46)};
      \end{axis}
   \end{tikzpicture}
\end{document}

相关内容