Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
Hm, that's going to be difficult. You could plot each bar with its own \addplot command, but that might not be feasible depending on how many data points you have. –  Jake Sep 24 '12 at 6:07
    
Just from personal interest:why does one want to do so? For me the plot presented by @Jake in the answer below looks quite terrible and does not add any obvious advantage or improvement. –  Benedikt Bauer Sep 24 '12 at 11:07
    
@BenediktBauer: I agree, those shadings are definitely out of place in scientific graphs and don't add any information to the visualisation. However, as Martin Tapankov said, "People should be free to shoot themselves in the foot, if they so choose". –  Jake Sep 24 '12 at 11:20
    
@Jake that's absolutely right. My comment also was not thought to offend you or your solution. I just took it as an example that illustrates (or better does not illustrate) the use of shaded bars. Therefore I was asking myself what progress someone would expect from such a feature. –  Benedikt Bauer Sep 24 '12 at 11:41
add comment

2 Answers

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}
share|improve this answer
add comment

ATTENTION: this answer refers to something which is not part of any stable release at the time of this writing.


Recently, I added switches to the bar plot handler of the PGF developer version. It allows to inject custom code at begin bar and at end bar.

This allows to write

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture} 
    \begin{axis}[ybar]
        \addplot[left color=black,right color=white,
            at begin bar={%
                \begin{scope}%
                \shade\pgfextra
            },
            at end bar={%
                \endpgfextra;
                \end{scope}%
            },
        ]
            coordinates {(1,2) (2,3) (3,4)}; 
    \end{axis} 
\end{tikzpicture}

\end{document}

enter image description here

Eventually, there will be styles to simplify such constructs.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.