Example: Venn diagram

Published 2006-11-08 | Author: Till Tantau

This example shows how to create Venn diagrams by clipping paths.

Authors: Till Tantau. Sligtly modified by Kjell Magne Fauske
Source: pgf-users mailing list

Download as: [PDF] [TEX]  •  [Open in writeLaTeX]

Venn diagram

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes,backgrounds}
\begin{document}
\pagestyle{empty}
% Suppose we have three circles or ellipses or whatever. Let us define
% commands for their paths since we will need them repeatedly in the
% following:

\def\firstcircle{(0,0) circle (1.5cm)}
\def\secondcircle{(45:2cm) circle (1.5cm)}
\def\thirdcircle{(0:2cm) circle (1.5cm)}

% Now we can draw the sets:
\begin{tikzpicture}
    \draw \firstcircle node[below] {$A$};
    \draw \secondcircle node [above] {$B$};
    \draw \thirdcircle node [below] {$C$};

    % Now we want to highlight the intersection of the first and the
    % second circle:

    \begin{scope}
      \clip \firstcircle;
      \fill[red] \secondcircle;
    \end{scope}

    % Next, we want the highlight the intersection of all three circles:

    \begin{scope}
      \clip \firstcircle;
      \clip \secondcircle;
      \fill[green] \thirdcircle;
    \end{scope}

    % The intersection trick works pretty well for intersections. If you need
    % the set-theoretic difference between two sets, things are a little more
    % complicated:

    % Suppose we want to highlight the part of the first circle that is not 
    % also part of the second circle. For this, we need to clip against the 
    % "complement" of the second circle. The trick is to add a large rectangle
    % that encompasses everything and then use the even-odd filling rule 
    % (see the manual again):

    \begin{scope}[shift={(6cm,0cm)}]
        \begin{scope}[even odd rule]% first circle without the second
            \clip \secondcircle (-3,-3) rectangle (3,3);
        \fill[yellow] \firstcircle;
        \end{scope}
        \draw \firstcircle node {$A$};
        \draw \secondcircle node {$B$};
    \end{scope}
    
    % When using the above, you will notice that the border lines of the
    % original circles are erased by the intersection parts. To solve this
    % problem, either use a background layer (see the manual) or simply draw
    % the border lines after everything else has been drawn.
    
    % The last trick is to cheat and use transparency
    \begin{scope}[shift={(3cm,-5cm)}, fill opacity=0.5]
        \fill[red] \firstcircle;
        \fill[green] \secondcircle;
        \fill[blue] \thirdcircle;
        \draw \firstcircle node[below] {$A$};
        \draw \secondcircle node [above] {$B$};
        \draw \thirdcircle node [below] {$C$};
    \end{scope}


\end{tikzpicture}

% Naturally, all of this could be bundled into nicer macros, but the above 
% should give the idea.

\end{document}

Comments

  • #1 lungile velemini, November 6, 2008 at 3:34 p.m.

    will you please send more venn diagrams.

  • #2 Kjell Magne Fauske, November 6, 2008 at 4:23 p.m.

    There are currently no venn diagram-related examples in the pipeline. Drawing venn diagrams usually requires clipping. You may find some useful tricks in the examples tagged clipping

  • #3 Kjell Magne Fauske, May 7, 2009 at 5:51 p.m.

    I'm not sure what you are trying to say kenneth. This example shows how to draw a simple venn diagram using TikZ. It is purely technical and not meant for learning what a Venn diagram is.

  • #4 lovely, June 28, 2009 at 4:01 p.m.

    i am now in college we have an assignment about Venn diagram so i search on it in internet..but some web have no examples..but only a definition..but now i found it..thankz.

  • #5 lovely, June 28, 2009 at 4:19 p.m.

    that diagrams show only about set diagram..

  • #6 veni, July 28, 2009 at 3:38 p.m.

    pls send more examples of Venn Diagram pls,pls

  • #7 rambo, August 4, 2009 at 8:02 a.m.

    could you please send practical word problems where venn diagrams can be used as a solution? thank you!

  • #8 Nathan, August 27, 2009 at 6:23 a.m.

    Just wanted to say thanks for posting this example, it's a good start for the Venn diagram I need to do.

  • #9 Wouter Beek, August 31, 2009 at 1:24 p.m.

    Is it also possible to draw a venn diagram in which one circle completely encompasses another circle?

  • #10 Philip Huffman, October 22, 2009 at 8:57 a.m.

    Thanks. These examples saved me a lot of time.

  • #11 Shifo, February 26, 2010 at 6:46 p.m.

    Thanks for the guide. Also, a cheating way of drawing the difference between two sets is filling the first circle with the color, and then fill the second one with white

  • #12 vans, March 12, 2010 at 10:19 a.m.

    you dont have an example of how to make one

  • #13 Nicolas, September 4, 2010 at 7:59 p.m.

    Great example, thanks

  • #14 leahstercarolino, June 12, 2012 at 8:03 a.m.

    w0w ang galing ng com. haha ^^ alam ko na ang venn diagram

Adding comments is currently not enabled.