是否可以使用 tikz 包制作 4 集维恩图,就像下面这样?我知道肯定可以,但我能找到的有关该主题的唯一信息是使用 R 作为语言。
所以,更新一下。我现在想知道如何最好地填充孤立的字段,即仅在 A 中的部分、仅在 B 中的部分等。目前,我只填写了 D 中的部分。有什么建议吗?代码如下:
\documentclass[12pt]{article}
\usepackage{tikz} \usetikzlibrary{positioning,shapes.geometric}
% For drawing
\def\firstellip{(1.6, 0) ellipse [x radius=3cm, y radius=1.5cm, rotate=50]}
\def\secondellip{(0.3, 1cm) ellipse [x radius=3cm, y radius=1.5cm,
rotate=50]} \def\thirdellip{(-1.6, 0) ellipse [x radius=3cm, y radius=1.5cm,
rotate=-50]} \def\fourthellip{(-0.3, 1cm) ellipse [x radius=3cm, y
radius=1.5cm, rotate=-50]} \def\bounding{(-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}
显示如下,我试图让四个“角落”变成红色。非常感谢:
答案1
此示例中有一些带有多个彩色元素的维恩图:http://www.texample.net/tikz/examples/venn-diagram/
我采用了那里使用的原则,并根据要求创建了一个填充所有四个“角”的版本(我使用黄色来区别于原始代码,原始代码中的一个角是红色的)。
\documentclass[12pt]{article}
\usepackage{tikz} \usetikzlibrary{positioning,shapes.geometric}
% For drawing
\def\firstellip{(1.6, 0) ellipse [x radius=3cm, y radius=1.5cm, rotate=50]}
\def\secondellip{(0.3, 1cm) ellipse [x radius=3cm, y radius=1.5cm,
rotate=50]} \def\thirdellip{(-1.6, 0) ellipse [x radius=3cm, y radius=1.5cm,
rotate=-50]} \def\fourthellip{(-0.3, 1cm) ellipse [x radius=3cm, y
radius=1.5cm, rotate=-50]} \def\bounding{(-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}
诀窍是添加一个包含所有内容的大矩形,然后使用奇偶填充规则 - 有关更多详细信息,请参阅 texample 页面。
(顺便说一句 - 我使用 Overleaf(以前称为 WriteLaTeX)来测试代码 - 文档可以在这里找到:https://www.overleaf.com/latex/examples/example-venn-diagram-with-isolated-areas-filled/xjptmqsjfdlc)
答案2
\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}