[Tex/LaTex] Color gradient for bars in pgfplots

gradientpgfplotstikz-pgf

I want that each bar in my diagram has a color gradient from black to white (from left to the right). However, for the following code, the gradient exists for the entire diagram such that the left bar is black, the middle one is gray, and the right one is white.

\documentclass{report}

\usepackage{pgfplots}


\begin{document}

\begin{tikzpicture} 
    \begin{axis}[ybar] 
        \addplot[left color=black,right color=white] coordinates {(1,2) (2,3) (3,4)}; 
    \end{axis} 
\end{tikzpicture}

\end{document}

enter image description here

Best Answer

One workaround is be to use error bars for the shading. This works because each error bar uses a separate path, so the shading applies to the bars individually:

\documentclass{report}

\usepackage{pgfplots}


\begin{document}

\begin{tikzpicture} 
    \begin{axis}[ybar, ymin=0] 
        \addplot[
            draw=none, fill=none,
            error bars/y dir=minus,
            error bars/y fixed relative=1,
            error bars/draw error bar/.code 2 args={
                \fill[xshift=-6pt, left color=black, right color=white, draw=black] ##1 ++(12pt,0pt) rectangle ##2;}
         ] coordinates {(1,2) (2,3) (3,4)}; 
    \end{axis} 
\end{tikzpicture}

\end{document}