1

I would like to have the node of each bar being the actual value and its corresponding percentage.

Here is what I have:

\begin{figure}[h]
    \centering
\begin{tikzpicture}
                \tikzstyle{every node}=[font=\scriptsize]
                \begin{axis}[
                axis lines*=left,
                title=myTitle,
                xbar, 
                xmin=0,
                xmax=50,
                width=9cm,
                height=5cm,
                enlarge y limits=0.3,
                xlabel={Number of Stuff},
                symbolic y coords={A, B, C},
                ytick=data,
                nodes near coords,
                point meta={x*100/44},
                nodes near coords={(\pgfmathprintnumber\pgfplotspointmeta\%)},
                nodes near coords align={horizontal},
                y tick label style={font=\scriptsize,text width=1.2cm,align=center}
                ]
                \addplot coordinates{(42,C) (40,B) (32,A)};
                \end{axis}
\end{tikzpicture}
    \caption{A test}
    \label{hist:auth}
\end{figure}

Which gives the following: mweResult

So I would like to have for example next to the bar for A the following node: 32 (73%).

I have been playing around with nodes near coords={(\pgfmathprintnumber\pgfplotspointmeta\%)} but I wasn't able to figure out.

2 Answers2

1

The raw data is stored in rawx etc. \tikzstyle is deprecated.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}[every node/.append style={font=\scriptsize}]
  \begin{axis}[visualization depends on=rawx \as \myx,
     axis lines*=left,
     title=myTitle,
     xbar, 
     xmin=0,
     xmax=50,
     width=9cm,
     height=5cm,
     enlarge y limits=0.3,
     xlabel={Number of Stuff},
     symbolic y coords={A, B, C},
     ytick=data,
     nodes near coords,
     point meta={x*100/44},
     nodes near coords={\pgfmathprintnumber\myx~(\pgfmathprintnumber\pgfplotspointmeta\%)},
     nodes near coords align={horizontal},
     y tick label style={font=\scriptsize,text width=1.2cm,align=center}
     ]
     \addplot coordinates{(42,C) (40,B) (32,A)};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Oh nice! I wasn't aware of \tikzstyle being deprecated. Good to know. – user1527152 Feb 21 '19 at 01:21
  • @user1527152 It is not "badly" deprecated. In the case at hand, it wouldn't have made too much of a difference. But there are cases where it will, and there are no real advantages (IMHO) to use it, so I personally avoid using it. –  Feb 21 '19 at 04:07
0

Here is a way using pure TikZ

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[x=1.5cm]
\pgfkeys{/pgf/number format/.cd,fixed,precision=2}

\foreach \i in {0,1,...,5}{
\pgfmathsetmacro{\num}{int(10*\i)}  
\draw (\i,0) node[below=1mm]{$\num$}--+(90:2mm);
}
% Total 44 participants
\foreach \j/\jtext/\jnum in {1/A/32,2/B/40,3/C/42}{
\draw (0,\j) node[left=5mm]{\jtext}--+(180:2mm);
\pgfmathsetmacro{\jpercent}{\jnum/44*100};       
\draw[blue!50,line width=3mm] (0,\j)--+(0:\jnum/10) 
node[right,blue]{$\jnum \;(\pgfmathprintnumber{\jpercent} \%)$};
}

\draw (0,3.5)--(0,0)--(5,0);
\path
(current bounding box.north) node[above]{My title}
(current bounding box.south) node[below=2mm]{Number of Participants};
\end{tikzpicture}
\end{document}

enter image description here

Black Mild
  • 17,569
  • Sorry but I'm still confused on what is the point you are trying to get across? I used the figure environment in my example because it is like that in my sources. Are you recommending to separate tikz picture from an whole document? – user1527152 Feb 23 '19 at 15:49
  • @ user152712 You just copy \begin{tikzpicture}....\end{tikzpicture} in my code, then paste into \begin{tikzpicture}....\end{tikzpicture} in your code you posted in the question – Black Mild Feb 23 '19 at 18:44