9.6 Drawing a Venn diagram

A Venn diagram displays several sets with their relationships. Commonly, these are overlapping circles. Such sets can stand for certain properties. If an element has two such properties, it would belong to an overlapping area—the intersection of the two relevant sets.

In this recipe, we will draw a Venn diagram of three sets.

How to do it…

We will draw colored circles and apply blending to their intersections. Go through these steps:

  1. Choose a document class:
    \documentclass{article}

  2. Load the TikZ package:
    \usepackage{tikz}

  3. Begin the document:
    \begin{document}

  4. Begin a TikZ picture environment:
    \begin{tikzpicture}

  5. Use a scope environment to apply a style to a part of the drawing. Here, we apply color blending:
      \begin{scope}[blend group=soft light]

  6. Draw the diagram parts, which in our case are simply filled circles:
        \fill[red!30!white]   ( 90:1.2) circle (2);
    \fill[green!30!white] (210:1.2) circle (2);
    \fill[blue!30!white] (330:1.2) circle (2);

  7. End the scope environment. At the end of the environment, the blending effect will end, because environments keep the settings local:
     \end{scope}

  8. Add nodes with text for the descriptions:
      \node at ( 90:2)    {Typography};
    \node at (210:2) {Design};
    \node at (330:2) {Coding};
    \node [font=\Large] {\LaTeX};

  9. End the TikZ picture and the document:
    \end{tikzpicture}
    \end{document}

  10. Compile, and take a look at the output:

Venn diagram

How it works…

We created three filled circles. The center of each circle is specified in polar coordinates, with a given angle and distance from the origin. This makes radial placement easier. For example, the first circle has its center at (90:1.2). This means that the center is at 90 degrees, which is above the origin, and the distance is 1.2. The radius of each circle is 2. So, they are overlapping.

Normally, overlapping would simply mean that the final circle overrides what is below it. We still wish to look behind the circles to see the intersections. A classical approach is to use transparency, like this for example:

    \begin{scope}[opacity=0.5]
...
\end{scope}

This lets the background shine through. We used a scope environment to keep the setting local. The opacity value can be between 0, which means totally transparent, and 1, which means totally opaque.

Another pleasing way is by using the blend mode feature of the PDF standard; that’s what we did. It’s mixing colors in a certain way. The possible ways are as follows, in short:

  • normal: Simply drawn over the background.
  • multiply: Multiplying mixed color values. A black factor always produces black and a white factor causes no change. Generally, we get darker colors as the product.
  • screen: This complements the color values, multiplies, and complements again. A white factor always produces white and a black factor causes no change. Generally, we get lighter colors in such a mix.
  • overlay: Like multiply or screen, depending on the background color.
  • darken: The darker of the mixed colors is chosen.
  • lighten: The lighter color of the mix is chosen.
  • colordodge: The background is brightened to reflect the foreground.
  • colorburn: The background is darkened to reflect the foreground.
  • hardlight: Like multiply or screen, depending on the foreground color, like a hard spotlight.
  • softlight: This darkens or lightens the color, depending on the foreground color, like a softened spotlight.
  • difference: This subtracts the darker color from the lighter color.
  • exclusion: This is like difference, but with lower contrast.
  • hue: The resulting color has the hue of the foreground and the saturation and luminosity of the background.
  • saturation: The resulting color has the saturation of the foreground and the hue and luminosity of the background.
  • color: The resulting color has the hue of the foreground and the saturation and luminosity of the background.
  • luminosity: This is the inverse of color; the resulting color has the luminosity of the foreground and the hue and saturation of the background.

These modes are described in the PDF standard, which is cited in the TikZ manual, in the Blend modes section.

What seems a bit ambitious just for overlapping colors in a Venn diagram could be generally useful in overlapping drawings. You may try out those modes to see what fits your needs best.

Navigate: