[Tex/LaTex] pgfplots: Set exponent of scientific number format for nodes near coords

nodes-near-coordspgfplots

As you can see, my plot currently has node labels with scale factors of both 10^6 and 10^5:

enter image description here

My goal is to specify a scale factor of 10^6 for every node label:

enter image description here

So my question is: How can I explicitly set the exponent in scientific number formatting?

\begin{tikzpicture}
\begin{axis}[
    width=6cm,
    xtick=\empty,
    ybar,
    ymin=0,
    ymax=3e6,
    bar width=1cm,
    scaled y ticks=base 10:-6,
    ymajorgrids,
    nodes near coords={\pgfmathprintnumber[sci,precision=1]{\pgfplotspointmeta}}
]
\addplot+[blue] coordinates {(0, 100 000)};
\addplot+[teal] coordinates {(0, 200 000)};
\addplot+[orange] coordinates {(0, 2 500 000)};
\end{axis}
\end{tikzpicture}

Best Answer

I'm not sure if you can get the PGF math parser to do this (might be a useful feature request), but you can use siunitx' \num macro, which has a fixed-exponent option:

enter image description here

\documentclass{report}
\usepackage{tikz} 
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{siunitx}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width=6cm,
    xtick=\empty,
    ybar,
    ymin=0,
    ymax=3e6,
    bar width=1.4cm,
    scaled y ticks=base 10:-6,
    ymajorgrids,
    nodes near coords={
        \pgfmathfloattofixed{\pgfplotspointmeta} % Convert floating point to fixed point
        \num[
            scientific-notation = fixed,
            fixed-exponent = 6,
            round-mode = places,
            round-precision = 1,
            exponent-product=\cdot
        ]{\pgfmathresult}}
]
\addplot+[blue] coordinates {(0, 100 000)};
\addplot+[teal] coordinates {(0, 200 000)};
\addplot+[orange] coordinates {(0, 2 500 000)};
\end{axis}
\end{tikzpicture}
\end{document}
Related Question