3

How can I show the percentage improvement in a pgfplot bar plot like this one? enter image description here

Here's the MWE for the base figure above:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
ytick={5000,6000,...,15000},
xtick=data,
xticklabels={$500$,$1000$},
legend style={at={(0.03,0.97)},anchor=north west},
ymin=5000,
ymax=15000
}
\begin{tikzpicture}
    \begin{axis}[ybar,enlarge x limits=0.60,legend entries={a,b}, y tick label style={/pgf/number format/.cd, scaled y ticks = false, set thousands separator={},},]
    \addplot[error bars/.cd, y dir=both, y explicit] table [row sep=crcr, col sep=comma, x index=0, y index=1, y error index=2] {500,7258.67,19.22\\1000,12849.56,27.59\\};
    \addplot[fill=gray!50,error bars/.cd, y dir=both, y explicit] table [row sep=crcr, col sep=comma, x index=0, y index=1, y error index=2] {500,7577.10,20.16\\1000,14092.98,24.79\\};
    \end{axis}
\end{tikzpicture}
\end{document}
5

This is a non-automated solution to add the labels, which could maybe automated, but would need extra effort. For more details on how exactly this can be done, please have a look at the comments in the code.

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        % use this compat level or higher so there is no need to prefix TikZ
        % coordinates by `axis cs:'
        compat=1.11,
    }
\begin{document}
\begin{tikzpicture}[
    % define a style for the brace
    my brace/.style={
        decorate,
        decoration={
            brace,
            raise=\Xshift,
        },
    },
    % define a style for the label
    my label/.style={
        midway,
        auto,
        inner xsep=\Xshift+5pt,
    },
]
    \begin{axis}[
        % (optimized the `axis' options)
        ymin=5000,
        ymax=15000,
        xtick=data,
        ytick distance={1000},
        enlarge x limits=0.70,
        ybar,
        scaled y ticks=false,
        /pgf/number format/set thousands separator={},
        legend style={
            at={(0.03,0.97)},
            anchor=north west,
        },
        legend entries={a,b},
        %
        % moved common options from the `\addplot' commands here
        error bars/y dir=both,
        error bars/y explicit,
        table/row sep=crcr,
        table/col sep=comma,
        table/x index=0,
        table/y index=1,
        table/y error plus index=2,
        table/y error minus index=2,
    ]
        \addplot [
            fill=white,
        ] table {
            500,7258.67,19.22\\
            1000,12849.56,27.59\\
        }
            % added named coordinates to the bars
            coordinate [pos=0] (first a)
            coordinate [pos=1] (last a)
        ;
        \addplot [
            fill=gray!50,
        ] table {
            500,7577.10,20.16\\
            1000,14092.98,24.79\\
        }
            coordinate [pos=0] (first b)
            coordinate [pos=1] (last b)
        ;

        % add some more coordinates left of the "bar nodes" at a given x coordinates
            \pgfmathsetmacro{\FirstX}{400}
            \pgfmathsetmacro{\LastX}{900}
        \coordinate (left of first a) at ( \FirstX,0 |- first a);
        \coordinate (left of first b) at ( \FirstX,0 |- first b);
        \coordinate (left of last a)  at ( \LastX,0  |- last a);
        \coordinate (left of last b)  at ( \LastX,0  |- last b);

        % draw the horizontal lines
        \draw [dashed,gray]
            (left of first a) -- (first a)
            (left of first b) -- (first b)
            (left of last a) -- (last a)
            (left of last b) -- (last b)
        ;

        % draw the curly braces and corresponding labels
            \pgfmathsetmacro{\Xshift}{1pt}
        \draw [my brace] (left of first a) -- (left of first b)
            node [my label] {$+4.39\%$}
        ;
        \draw [my brace] (left of last a) -- (left of last b)
            node [my label] {$+9.67\%$}
        ;
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

|improve this answer|||||
  • Actually I need to add the same to the other couple as well. – Filippo Bistaffa Oct 19 '17 at 19:48
  • @FilippoBistaffa, next time it would be helpful to state that "little detail" as well in the question ... See my edited answer. – Stefan Pinnow Oct 19 '17 at 20:27

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.