[Tex/LaTex] Tikz: How to shade a node

shadingtikz-pgf

I have made a graph with TikZ and now I would like to apply a shading to some of the nodes but I can't seem to find out how to do that.

This doesn't work:

\documentclass{article}
\usepackage{tikz}

\tikzset{every node/.append style={minimum size=.7cm, draw,circle,font=\sffamily\Large\bfseries,inner sep=0.05cm}}

\begin{document}

\begin{tikzpicture}[sh/.style={shade,shading=axis,shading angle=45,left color=red,right color=green}]
  \node                                 (x1) {$x_1$};
  \node[node distance=3cm,right of=x1]  (x2) {$x_2$};
  \node[node distance=3cm,right of=x2]  (x3) {$x_3$};

  \node[node distance=1cm,below of=x1]  (1) [sh] {};
  \node[node distance=1cm,below of=x2]  (2) [sh] {};
  \node[node distance=1cm,below of=x3]  (3) [sh] {};
  \node[node distance=1.5cm,below of=1] (4) {};
  \node[node distance=1.5cm,below of=2] (5) {};
  \node[node distance=1.5cm,below of=3] (6) {};
  \foreach \from/\to in {x1/1,x2/2,x3/3,1/4,2/5,3/6,4/5,5/6} \draw (\from) -- (\to);
\end{tikzpicture}

\end{document}

Best Answer

As noted in the comments, the shading angle needs to be the last definition.

In the example I've used a matrix of math nodes just to show a different way to achieve the same goal:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\tikzset{
  mymx/.style={ matrix of math nodes, nodes in empty cells,
    row sep=3mm, column sep=2cm,
    nodes={ minimum size=.7cm, draw, circle,
      font=\sffamily\Large\bfseries, inner sep=0.05cm } },
  sh/.style={ shade, shading=axis, left color=red, right color=green,
    shading angle=45 } }

\begin{document}

\begin{tikzpicture}
  \matrix[mymx,row 2/.style={every node/.append style=sh}] (mx) {
    x_1 & x_2 & x_3 \\
    &&\\[5mm]
    &&\\
  };
  \draw (mx-1-1) -- (mx-2-1) -- (mx-3-1) -- (mx-3-2) -- (mx-3-3) -- (mx-2-3) -- (mx-1-3)
    (mx-3-2) -- (mx-2-2) -- (mx-1-2) ;
\end{tikzpicture}

\end{document}

enter image description here