4

I am trying to make an xbar chart using pgfplots, and I'd like to add the bar's value to the bar.

Simply using "nodes near coords" positions the value for a ybar chart, which isn't correct here. What's the best way to position these nodes? enter image description here

My code:

\documentclass[letterpaper]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{filecontents}{my.dat}
Label     value       num
992       70          1
993       120         2
994       30          3
995       330         4
999       50          5
988       50          6
989       50          7
983       50          8
\end{filecontents}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
    xbar,
    xlabel=My value,
    ylabel=Label here,
    xmajorgrids=true,
    ytick=data,
    yticklabels from table={my.dat}{Label},
    nodes near coords,
]
    \addplot table [x=value, y=num]
    {my.dat};
\end{axis}

\end{tikzpicture}
7

You can tell pgfplots to label the coordinates correctly by setting nodes near coords align=horizontal.

  • 2
    Interesting - that should have worked automatically for xbar. Probably a bug; I'll take a note to look into it. – Christian Feuersänger Oct 16 '11 at 10:58
  • Doesn't work for me... – Pygmalion Jul 12 '17 at 13:33

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.