1

The following MWE generates an y bar stacked plot:

\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}

A bar represents one time range divided in X sub ranges. For clarity, on this example only the bar 12 is shown. The value for each sub range represents the percentage of time spent in this range and so the total of sub ranges is 100%. I want to display (on top of the bar, or maybe inside the sub-range rotated) the maximum value among the sub ranges percentages. How should I acheive this ?

7
+100

Some remarks

  • compat=1.12 is essential, otherwise the metadata would be the cumulated value instead of the true value.

    • that is, the top one is always the largest one with result 100%)
    • I am not saying it is necessary. Just somehow I do not want to chase where the difference comes from.
  • nodes near coords is essential, and is not necessary as long as you want to draw/place the node yourself.

  • \pgfplotspointmetatransformed ranges from 0 to 1000. So checking if it is larger than 999 will probably indicate the largest datapoint.

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.