I have a Venn diagram with some overlapping ellipses that I segmented into sections using lines.

I want to use this to make a few figures, and I am wondering wether there is a clever / clean way to do this.

I have the following questions:

  1. Segment Firstly I want to segment each of the spaces created by the intersections of lines in the figure (that are labeled a trough h).

  2. Merge Next, I want to make a few plots where I highlight different merges. E.g. I have a plot where a is colored red, b is colored green, and d+c are colored yellow. I plan to have the outlines of all segments still visible (d and c still have a separate line) but with low alpha.

  3. Label It would be nice to use some tikz magic for automatic labeling. Specifically, it would be nice to have labels automatically in the centre of each section or to be able to specify some text exactly centred above some of the sections.

  4. Approach To me, the most sanity-preserving solution seems to be to "declare" the segments once, and then use the segment names to shade/outline/label them.

    I do not have a lot of experience with tikz, so I do not know if this is really the best way. My only experience with tikz was in making graphs, but I do not fully understand the concept of nodes and other types of objects in the figure.

Venn diagram created by LaTeX code below

\begin{tikzpicture}[line/.style={line width = 2pt}]
\draw[line, red] (0,0) ellipse (6 and 4);
\draw[line, blue] (-2,0) ellipse (2 and 3);
\draw[line, green] (1.5,-1.7) ellipse (3 and 1.5); 

\node at (-2, 2.5) {a};
\draw[line, blue] (-3.5,2) -- (-0.5,2);

\node at (-2, 1.2) {b};
\draw[line, blue] (-4,0.6) -- (0,0.6);

\node at (-2, -1.2) {c};
\node at (-3.4, -1.2) {d};
\draw[line, blue] (-3,0.6) -- (-3,-2.6);

\node at (-0.5, -0.9) {e};
\node at (-1, -1.6) {f};
\draw[line, orange] (-1.2,-1) -- (-0.2,-1.3);

\node at (1, -1.6) {g};
\node at (3.2, -1.6) {h};
\draw[line, green] (2.5, -0.3) -- (2.5, -3.2);

\end{tikzpicture}

Solutions found

  • 1. This does not really seem to create nodes, it just fills a section.
  • 2, 3 this one seems to be quite close to what I want, there circles are "defined" as I would like them to be. I probably need to convert my lines to rectanges to do the intersections but I am not sure about the cleanest way do do so.

I realise this is quite a specific problem, and I do not expect a fully coded solution, but any directions on how to go about this would be great! :)

Hopefully someone can help me! :)

Edit: All the merges I want to make could also be done by doing clips (e.g. for d+c just clip the blue circle with by the bottom horizontal line) so if merging is not possible, that is alright.

up vote 6 down vote accepted

With path picture you can fill a path with whatever you want and keep it clipped to the original path. Does it help?

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{positioning, shapes.geometric}

\begin{document}
\begin{tikzpicture}

\draw[blue] (0,0) ellipse(2cm and 3cm)
    [path picture={
         \node[draw=blue, minimum width=4cm, minimum height=1.5cm, fill=blue!30, anchor=north] at (path picture bounding box.north) (a) {a};
         \node[draw=blue, minimum width=4cm, minimum height=1.5cm, fill=green!30, below=0pt of a] (b) {b};
         \node[draw=blue, minimum width=3cm, minimum height=3cm, fill=red!30, below right=0pt and -1cm of b.south] (c) {c};
         \node[draw=blue, minimum width=1cm, minimum height=3cm, fill=orange!30, left=0pt of c] (d) {d};
         }  
    ];
\end{tikzpicture}
\end{document}

enter image description here

Update:

In previous example, the ellipse was drawn with a path command and rectangular nodes where drawn inside with path picture option.

In following code I've changed the path for ellipse nodes. This way is possible to use coordinates on node's border to easily define coordinates to draw and fill rectangles, but still using a path picture option to keep all these rectangles clipped by node's border.

Once both ellipses have been drawn and sectors defined and filled we go for intersection area. First we find two intersection points with intersection library help. But for filling ellipses intersection, we need to define and intersection area. This can be done with two clipping paths, each one defined like the original node. Inside this scope we fill both sectors. But as this filling covers part of previously drawn ellipses, we need to redraw them.

Finally, envolving ellipse can be drawn with specific dimensions or using a fit node. As this node is also filled, it has been drawn on background layer.

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{positioning, shapes.geometric, intersections, fit, backgrounds}

\begin{document}

\begin{tikzpicture}

\node[draw=blue, ellipse, minimum width=4cm, minimum height=6cm,
    name path=A,
    path picture={
        \draw[blue, fill=blue!30] (A.40) rectangle (A.140|-A.north) node[pos=0.5] {a};
        \draw[blue, fill=red!30] (A.5) rectangle (A.175|-A.140) node[pos=0.5] {b};
        \draw[blue, fill=green!30] ([xshift=1cm]A.175) coordinate (aux) rectangle (A.south-|A.west) node[pos=0.5, yshift=5mm] {d};
        \draw[blue, fill=orange!30] (aux) rectangle (A.south-|A.east) node[pos=0.3] {c};
         }  
    ] at (-2,0) (A) {};

\node[draw=green!80!black, ellipse, minimum width=6cm, minimum height=3cm,
    name path=B,
    path picture={
        \draw[green!80!black, fill=green!10] (B.80) rectangle (B.-80-|B.east) node[pos=0.5] {h};
        \draw[green!80!black, fill=brown!30] (B.80) rectangle (B.south-|B.west) node[pos=0.4] {g};
         }  
    ] at (1.5,-1.7) (B) {};

\path [name intersections={of=A and B}];

\begin{scope}
\clip (-2,0) ellipse (2 and 3);
\clip (1.5,-1.7) ellipse (3 and 1.5);

\fill[cyan!50] (intersection-1) |- (A.-30) coordinate (auxA) -- (B.160) coordinate (auxB) |- cycle;
\node[blue] at (barycentric cs:intersection-1=1,auxA=1,auxB=1) {e};

\fill[cyan!30] (intersection-2) -| (auxA) -- (auxB) -| (B.west) |- cycle;
\node[blue] at (barycentric cs:intersection-2=1,auxA=1,auxB=1) {f};

\draw[brown!70!black, name path=C] (auxB)--(auxA);
\end{scope}

\draw[blue] (-2,0) ellipse (2 and 3);
\draw[green] (1.5,-1.7) ellipse (3 and 1.5);


\begin{scope}[on background layer]
\node[ellipse, draw=red, fit=(A) (B), fill=red!5, inner sep=0pt] {};
\end{scope}

\end{tikzpicture}
\end{document}

enter image description here

  • Thanks that is looking pretty good! Is there a way to combine this with the second elipse to get segments e,f and g? – Tivaro Jun 13 at 14:52
  • @Tivaro Try to understand the new code. – Ignasi Jun 13 at 17:21

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.