# Colouring graphs tikz

I want to colour the complete graphs. However, the filling in of the areas covers the edges and the other colours. The best thing would be if there was a way to have the colours "blend" when the regions overlap, as well as showing the edges. Is this possible? Is it possible to at least show the edges through such clutzy colouring? Code follows (I can't get any good pictures up here - advice appreciated).

\begin{center}
\begin{eqnarray*}
\begin{tikzpicture}
\tikzstyle{vertex} = [draw,circle,fill=black,inner sep = 1.5pt]
\node[vertex] (1) [label = above: $a$] at (0,2) {};
\node[vertex] (2) [label = below: $b$] at (1,-1/2) {};
\node[vertex] (3) [label = below: $c$] at (-1,-1/2) {};
\node[vertex] (4) [label = above: $f$] at (2,5) {};
\node[vertex] (5) [label = below: $g$] at (-4,3/2) {};
\node[vertex] (6) [label = below: $h$] at (4,-3) {};

\draw [] (0,2) -- (1,-1/2) -- (-1,-1/2) -- cycle;
\draw [fill = yellow] (0,2) -- (-1,-1/2) -- (2,5) -- cycle
(0,2) -- (1,-1/2) -- (2,5) -- cycle
(1,-1/2) -- (-1,-1/2) -- (2,5) -- cycle;

\draw [fill = lime] (0,2) -- (1,-1/2) -- (-4,3/2) -- cycle
(0,2) -- (-1,-1/2) -- (-4,3/2) -- cycle
(1,-1/2) -- (-1,-1/2) -- (-4,3/2) -- cycle;

\draw (6) to [bend left] (5);
\end{tikzpicture}
\end{eqnarray*}
\end{center}


• Just curious, what is the function of the eqnarray* in this case? – Raoul Kessels Jun 18 '16 at 21:27
• Please make your code compilable - i.e. a Minimal Working Example (MWE). – cfr Jun 18 '16 at 21:43

The simplest method is, as says, to just specify opacity=<degree> where opacity ranges from zero (transparent) to 1 (opaque). fill opacity applies only to fillings, draw opacity only to drawings and text opacity only to text.

However, I would not apply fill opacity to the entire picture as it will also apply to the filled black circles, which you probably don't want. Instead, apply it only to the commands colouring the graphs.

For example (changing the colours to make the effects more obvious):

...
\draw  (0,2) -- (1,-1/2) -- (-1,-1/2) -- cycle;
\draw [fill = magenta, fill opacity=.5] (0,2) -- (-1,-1/2) -- (2,5) -- cycle (0,2) -- (1,-1/2) -- (2,5) -- cycle (1,-1/2) -- (-1,-1/2) -- (2,5) -- cycle;
\draw [fill = cyan, fill opacity=.5] (0,2) -- (1,-1/2) -- (-4,3/2) -- cycle (0,2) -- (-1,-1/2) -- (-4,3/2) -- cycle (1,-1/2) -- (-1,-1/2) -- (-4,3/2) -- cycle;
\draw (6) to [bend left] (5);
...


This may be what you want or it may not. If it is, you can stop reading and play with the cat*.

If you don't want opacity but just want to blend the colours, you can use a blend mode other than normal. By default, when you paint a new full opacity pixel over an existing, coloured one, the new colour just displaces the old. However, that's not the only possibility. For example, another option is to use overlay blending.

...
[
vertex/.style = {% \tikzstyle is deprecated
draw,
circle,
fill=black,
inner sep = 1.5pt,
},
blend group=overlay,
]
\draw [fill = magenta] (0,2) -- (-1,-1/2) -- (2,5) -- cycle (0,2) -- (1,-1/2) -- (2,5) -- cycle (1,-1/2) -- (-1,-1/2) -- (2,5) -- cycle;
\draw [fill = cyan] (0,2) -- (1,-1/2) -- (-4,3/2) -- cycle (0,2) -- (-1,-1/2) -- (-4,3/2) -- cycle (1,-1/2) -- (-1,-1/2) -- (-4,3/2) -- cycle;
\draw (6) to [bend left] (5);
...


There are a couple of things you might find unsatisfactory. One is that the black lines underneath, while visible, are still partially obscured. The other is that some fillings are getting double (or more) opacity or colouring. Sometimes this is what you want. Sometimes it isn't.

What you might well be looking for is something like this:

1. In this case, the nodes are first drawn and labelled on the main layer of the picture at the same time as coordinates define the relevant points for reasons which I think Heiko explained while I was asking a question about it, which percusse was kind enough to answer, even though it was probably a stupid question.

2. The colours are then filled on the background layer, using the backgrounds library, so that they do not obscure the nodes.

3. Finally, the lines are drawn on the main layer using the coordinates.

To keep track more easily, the relevant coordinates are named using the corresponding vertices' labels.

\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
[
vertex/.style = {%
draw,
circle,
fill=black,
inner sep = 1.5pt,
},
]
\foreach \i/\k/\m [count=\j] in {(0,2)/a/above, (1,-.5)/b/below, (-1,-2)/c/below, (2,5)/f/above, (-4,-1.5)/g/below, (4,-3)/h/below}
{
\node [coordinate] (\k) at \i {} ;
\node [vertex, label=\m:$\k$] at (\k) {};
}
\draw  (a) -- (b) -- (c) -- cycle;
\begin{scope}[on background layer]
\begin{scope}[blend group=overlay]
\fill [magenta] (a) -- (c) -- (b) -- (f) -- cycle;
\fill [cyan] (a) -- (b) -- (c) -- (g) -- cycle;
\end{scope}
\end{scope}
\draw (a) -- (c) -- (f) -- cycle (a) -- (b) -- (f) -- cycle (b) -- (c) -- (f) -- cycle (a) -- (b) -- (g) -- cycle (a) -- (c) -- (g) -- cycle (b) -- (c) -- (g) -- cycle (h) to [bend left] (g);
\end{tikzpicture}
\end{document}


I like this version because it gives colours which look almost translucent and blends them automatically in an intuitive way. (I.e. I don't have to mix the colours by hand and I don't get the dulling and not-quite-expected effect I get with transparency.) Your kilometres may, of course, vary considerably.

• Cat not included.
• The overlay blending is neat. I'm glad you asked about blending because I'd never actually tried this before and didn't realise this was even possible! There are actually a whole bunch of other modes which seem quite similar to what something like GIMP offers. – cfr Jun 20 '16 at 15:05

The order of drawing and filling matters. First the areas should be filled, then the lines can be drawn on top of them.

The yellow area is not filled completely because of the nonzero filling rule, see the pgf manual for details. In short, the corners of the first yellow triangles are in counterclockwise order, the corners of the third triangle in clockwise order. Use the same order, if you want to have the union of the areas.

The following example simplifies the filling and drawing. First the coordinates are defined (and later used instead of explicit numbers). then the filled triangles are merged to three areas: the yellow area, the lime area and the intersection area, where the color is composed of both colors. Then the lines are drawn, first the outer lines, then the inner connection lines, without drawing a line twice and avoiding sharp angles.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}

\path
( 0,  2)   coordinate (a)
( 1, -0.5) coordinate (b)
(-1, -0.5) coordinate (c)
( 2,  5)   coordinate (f)
(-4,  1.5) coordinate (g)
( 4, -3)   coordinate (h)
;

\fill [yellow]         (a) -- (b) -- (f) -- cycle;
\fill [lime]           (a) -- (g) -- (c) -- cycle;
\fill [yellow!50!lime] (a) -- (b) -- (c) -- cycle;

\draw
(a) -- (f) -- (b) -- (c) -- (g) -- cycle
(a) -- (c)
(a) -- (b)
(c) -- (f)
(b) -- (g)
(h) to [bend left] (g);

\tikzstyle{vertex} = [draw,circle,fill=black,inner sep = 1.5pt]
\node[vertex] [label = above: $a$] at (a) {};
\node[vertex] [label = below: $b$] at (b) {};
\node[vertex] [label = below: $c$] at (c) {};
\node[vertex] [label = above: $f$] at (f) {};
\node[vertex] [label = below: $g$] at (g) {};
\node[vertex] [label = below: $h$] at (h) {};

\end{tikzpicture}
\end{document}


To blend colours you can use transparency. Something like opacity=0.5 for the whole picture but there are more possibilities. However, lime and yellow are very similar and do not blend too well.

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{center}
\begin{tikzpicture}[fill opacity=0.5]
\tikzstyle{vertex} = [draw,circle,fill=black,inner sep = 1.5pt]
\node[vertex] (1) [label = above: $a$] at (0,2) {};
\node[vertex] (2) [label = below: $b$] at (1,-1/2) {};
\node[vertex] (3) [label = below: $c$] at (-1,-1/2) {};
\node[vertex] (4) [label = above: $f$] at (2,5) {};
\node[vertex] (5) [label = below: $g$] at (-4,3/2) {};
\node[vertex] (6) [label = below: $h$] at (4,-3) {};

\draw [] (0,2) -- (1,-1/2) -- (-1,-1/2) -- cycle;
\draw [fill = yellow] (0,2) -- (-1,-1/2) -- (2,5) -- cycle
(0,2) -- (1,-1/2) -- (2,5) -- cycle
(1,-1/2) -- (-1,-1/2) -- (2,5) -- cycle;

\draw [fill = lime] (0,2) -- (1,-1/2) -- (-4,3/2) -- cycle
(0,2) -- (-1,-1/2) -- (-4,3/2) -- cycle
(1,-1/2) -- (-1,-1/2) -- (-4,3/2) -- cycle;

\draw (6) to [bend left] (5);
\end{tikzpicture}
\end{center}

\end{document}