1

I'm working to a bar chart with datas values between 0 and 1 representing percentages like this:

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

\begin{tikzpicture}
\begin{axis}[ybar,
             bar width=1,
             ymin=0,
             enlarge x limits={abs=1},
             xtick=data,
             nodes near coords,
             nodes near coords style={/pgf/number format/precision=3},
             xticklabels={A,B,C,D},
             yticklabel={\pgfmathparse{\tick*100}\pgfmathprintnumber{\pgfmathresult}\%},
             tickwidth=0,
            ]

    \addplot table[x expr=\coordindex,y index=0,row sep=\\] {0.104\\0.254\\0.309\\0.271\\};
\end{axis}
\end{tikzpicture}

\end{document}

bar chart's MWE output

I need to have the values expressed as percentages on both y axis and column labels. I found the option yticklabel={\pgfmathparse{\tick*100}\pgfmathprintnumber{\pgfmathresult}\%} to change y axis' values, but I can't figure out how to do the same for the labels. If I understand how it works, I should add a line like \pgfmathparse{\〈something〉*100}\pgfmathprintnumber{\pgfmathresult}\%} in nodes near coords option, but I don't know what \〈something〉 should be.

Is my hypothesis right and if it is, what is \〈something〉? If it isn't, how can I change labels' valure to the equivalent percentage value? Note that I need to keep all the decimals, so 0.104 must become 10.4% (not 10%).

1
  • 1
    You shoud be able to use the same with nodes near coords={...} – BambOo May 31 '20 at 16:39
0

The default for nodes near coords is \pgfmathprintnumber\pgfplotspointmeta. So you can to change this to

nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%} 

and also multiply the default meta by 100 to get percentages.

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

\begin{tikzpicture}
\begin{axis}[ybar,
             bar width=1,
             ymin=0,
             enlarge x limits={abs=1},
             xtick=data,
             point meta=y*100,
             nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
             nodes near coords style={/pgf/number format/precision=3,
             /pgf/number format/fixed},
             xticklabels={A,B,C,D},
             yticklabel={\pgfmathparse{\tick*100}\pgfmathprintnumber{\pgfmathresult}\%},
             tickwidth=0,
            ]

    \addplot table[x expr=\coordindex,y index=0,row sep=\\] {0.104\\0.254\\0.309\\0.271\\};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

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.