TransWikia.com

4 set venn diagram in TikZ

TeX - LaTeX Asked on November 30, 2020

Is it possible to make a 4-set venn diagram using the tikz packages, like the following one? I know it must be, but the only information I can find on the subject uses R as the language.

enter image description here

So, an update. I am now wondering how to best fill just the isolated fields, that is, the part only in A, the part only in B, etc. For now, I have just the part in D filled. Any suggestions? Code below:

documentclass[12pt]{article}

usepackage{tikz} usetikzlibrary{positioning,shapes.geometric}

% For drawing
deffirstellip{(1.6, 0) ellipse [x radius=3cm, y radius=1.5cm, rotate=50]}
defsecondellip{(0.3, 1cm) ellipse [x radius=3cm, y radius=1.5cm,
rotate=50]} defthirdellip{(-1.6, 0) ellipse [x radius=3cm, y radius=1.5cm,
rotate=-50]} deffourthellip{(-0.3, 1cm) ellipse [x radius=3cm, y
radius=1.5cm, rotate=-50]} defbounding{(-5,-3) rectangle (5,4)}

begin{document}

begin{tikzpicture} filldraw[fill=black, opacity=0.2] bounding;

    scope fill[white] fourthellip; endscope filldraw[fill=red,
    opacity=0.2] fourthellip;

    %single colors
    scope fill[white] secondellip; fill[white] thirdellip; fill[white]
    firstellip; endscope

    draw firstellip node [label={[xshift=2.0cm, yshift=-0.9cm]$A$}] {};
    draw secondellip node [label={[xshift=2.2cm, yshift=2.1cm]$B$}] {};
    draw thirdellip node [label={[xshift=-2.0cm, yshift=-0.9cm]$C$}] {};
    draw fourthellip node [label={[xshift=-2.2cm, yshift=2.1cm]$D$}] {};
    draw bounding node [label=above left:$U$] {};

end{tikzpicture}  end{document}

And displays as follows, I’m trying to get red in the four “corners”. Thanks so much:

enter image description here

2 Answers

There are some Venn diagrams with multiple elements coloured in this example: http://www.texample.net/tikz/examples/venn-diagram/

I've taken the principles used there and created a version with all four "corners" filled, as requested (I used yellow to distinguish from the original code which had one corner coloured red).

Venn with corners coloured

documentclass[12pt]{article}

usepackage{tikz} usetikzlibrary{positioning,shapes.geometric}

% For drawing
deffirstellip{(1.6, 0) ellipse [x radius=3cm, y radius=1.5cm, rotate=50]}
defsecondellip{(0.3, 1cm) ellipse [x radius=3cm, y radius=1.5cm,
rotate=50]} defthirdellip{(-1.6, 0) ellipse [x radius=3cm, y radius=1.5cm,
rotate=-50]} deffourthellip{(-0.3, 1cm) ellipse [x radius=3cm, y
radius=1.5cm, rotate=-50]} defbounding{(-5,-3) rectangle (5,4)}

begin{document}

begin{tikzpicture} filldraw[fill=black, opacity=0.2] bounding;

    scope fill[white] fourthellip; endscope filldraw[fill=red,
    opacity=0.2] fourthellip;

    %single colors
    scope fill[white] secondellip; fill[white] thirdellip; fill[white]
    firstellip; endscope

    draw firstellip node [label={[xshift=2.0cm, yshift=-0.9cm]$A$}] {};
    draw secondellip node [label={[xshift=2.2cm, yshift=2.1cm]$B$}] {};
    draw thirdellip node [label={[xshift=-2.0cm, yshift=-0.9cm]$C$}] {};
    draw fourthellip node [label={[xshift=-2.2cm, yshift=2.1cm]$D$}] {};
    draw bounding node [label=above left:$U$] {};

    begin{scope}
        begin{scope}[even odd rule]% first ellipse corner
            clip secondellip (-5,-5) rectangle (5,5);
            clip thirdellip (-5,-5) rectangle (5,5);
            clip fourthellip (-5,-5) rectangle (5,5);
        fill[yellow] firstellip;
        end{scope}
    end{scope}
    
    begin{scope}
        begin{scope}[even odd rule]% second ellipse corner
            clip firstellip (-5,-5) rectangle (5,5);
            clip thirdellip (-5,-5) rectangle (5,5);
            clip fourthellip (-5,-5) rectangle (5,5);
        fill[yellow] secondellip;
        end{scope}
    end{scope}
    
    begin{scope}
        begin{scope}[even odd rule]% third ellipse corner
            clip secondellip (-5,-5) rectangle (5,5);
            clip firstellip (-5,-5) rectangle (5,5);
            clip fourthellip (-5,-5) rectangle (5,5);
        fill[yellow] thirdellip;
        end{scope}
    end{scope}
    

end{tikzpicture}  end{document}

The trick is to add a large rectangle that encompasses everything and then use the even-odd filling rule - see the texample page for more details.

(Btw - I used Overleaf (formerly WriteLaTeX) to test the code - the document can be found here: https://www.overleaf.com/latex/examples/example-venn-diagram-with-isolated-areas-filled/xjptmqsjfdlc )

Correct answer by John Hammersley on November 30, 2020

Result

documentclass{article}
usepackage{tikz}
usetikzlibrary{positioning,shapes.geometric}
begin{document}
tikzset{
  set/.style ={ 
    ellipse, 
    minimum width=3.5cm, 
    minimum height=2cm,
    draw,
}}
begin{tikzpicture}
foreach x/y/a in {.7/0/60,.3/1/60,-.7/0/-60,-.3/1/-60} 
  node[set, rotate=a] at (x,y) {};
end{tikzpicture}
end{document}

Answered by JLDiaz on November 30, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP