5

Is there a possibility with pgfplots to print numbers with no decimals for the nodes? By this, I mean printing '2' instead of '2.0e0'.

In my case, I want to remove the decimal part and add the percent sign, which would give'2%' instead of '2.0e0'.

--

Furthermore, how can I put the text node '1%' on the top of the corresponding bar ? (if the bar is negative, it won't be anymore on the top)

--

Here is a sample code and a picture which I want to improve. enter image description here

\documentclass{standalone}
\usepackage{filecontents,pgfplots,pgfplotstable}

\begin{filecontents}{data.dat}
id a b  name
0 1.1 20 Name1
1 2.2 -10 Name2
2 3.3 15 Name3
\end{filecontents}


\begin{document}

\pgfplotstableread{data.dat}{\donnees}

\begin{tikzpicture}
\begin{axis}[ybar,xtick={0,1,2},xticklabels={a,b,c},point meta=explicit symbolic,nodes near coords=$\pgfplotspointmeta\%$]

\addplot table [x={id}, y={b},,meta expr=\thisrow{id}] {\donnees};

\end{axis}
\end{tikzpicture}

\end{document}
4

You can use nodes near coords={\pgfmathprintnumber[fixed, precision=0]{\pgfplotspointmeta}\%} to format the numbers using PGF's very flexible number formatting macro. Note that you have to use point meta=explicit instead of point meta=explicit symbolic if your meta data is numeric, otherwise the number parser will fail.

\documentclass[border=5mm]{standalone}
\usepackage{filecontents,pgfplots,pgfplotstable}

\begin{filecontents}{data.dat}
id a b  name
0 1.1 20 Name1
1 2.2 10 Name2
2 3.3 15 Name3
\end{filecontents}


\begin{document}

\pgfplotstableread{data.dat}{\donnees}

\begin{tikzpicture}
\begin{axis}[
    ybar,
    xtick={0,1,2},
    xticklabels={a,b,c},
    point meta=explicit,
    nodes near coords={\pgfmathprintnumber[fixed, precision=0]{\pgfplotspointmeta}\%}
    ]

\addplot table [x={id}, y={b},meta expr=\thisrow{id}] {\donnees};

\end{axis}
\end{tikzpicture}

\end{document}
  • I get '0%' for each bar, is there an error in the code ? – Laurent Dudok de Wit Sep 6 '13 at 20:38
  • @user81566: I get the result shown in the screenshot when I compile my code. What version of PGFPlots are you using? – Jake Sep 6 '13 at 20:46
  • I have the version 1.7, which should be the latest version according to Miktex. Am I right ? – Laurent Dudok de Wit Sep 6 '13 at 20:58
  • @user81566: The current version is 1.8, but I wouldn't have thought that there was a change between 1.7 and 1.8 that would affect this. When you copy my code into a new document, you get 0%? Or only when you try to adapt this for your own code? – Jake Sep 6 '13 at 21:09
  • 2
    Yes but I get 0,1,2 percent somehow. Not 500,1000 as you have. Oops picture is changed nevermind :) – percusse Sep 8 '13 at 12:43

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.