使用 TikZ 绘制基于图形的维恩式图表

使用 TikZ 绘制基于图形的维恩式图表

我想绘制一个图形(数据结构),使得某些子图形成一个向日葵(组合学的概念)。

在此处输入图片描述

换句话说,我想表明这些集合在中间重叠(1、2、3、4)。这个想法是所有颜色组合的结果都是白色(这够清楚了吗?)。问题是两种颜色恰好重叠的空间。例如,我可以使用黄色!20!绿色!20 作为底部形状,但我不知道如何绘制这样的形状。

从数学意义上讲,绿色和黄色集合没有重叠,也不包含蓝色和红色,但看起来有点丑。有什么想法吗?也许可以用不同的方式描述它?

    \begin{tikzpicture}[scale=1]
    \tikzstyle{rect}=[rectangle, thick, 
    draw=black, rounded corners, node distance = 2cm]
    \tikzstyle{diedge}=[->,thick,shorten <=2pt, shorten >=2pt]
    \tikzstyle{udiedge}=[thick,shorten <=2pt, shorten >=2pt]
    \tikzstyle{circ}=[circle, thick, 
    draw=black, rounded corners, node distance = 1cm,
    font=\tiny]

    \fill[blue!20] (-2.4,2.4) circle (1.2cm);
    \fill[red!20] (2.4,2.4) circle (1.2cm);
    \fill[green!20] (2.4,-2.4) circle (1.2cm);
    \fill[yellow!20] (-2.4,-2.4) circle (1.2cm);

    \draw[blue!20,line width=2.4cm] (-2.4,2.4) -- (0,0);
    \draw[red!20,line width=2.4cm] (2.4,2.4) -- (0,0);
    \draw[green!20,line width=2.4cm] (2.4,-2.4) -- (0,0);
    \draw[yellow!20,line width=2.4cm] (-2.4,-2.4) -- (0,0);

    \fill[white] (0,0) circle (1.2cm);

    \node[circ] at (-0.5,0.5) (1) {$1$};
    \node[circ, below of = 1] (2) {$2$};
    \node[circ, right of = 1] (3) {$3$};
    \node[circ, right of = 2] (4) {$4$};

    \path (1) edge[udiedge] (2);
    \path (1) edge[udiedge] (3);
    \path (2) edge[udiedge] (4);
    \path (3) edge[udiedge] (4);

    \node[circ, node distance = 2cm, above right of = 3] (5) {$5$};
    \node[circ, right of = 5] (6) {$6$};
    \node[circ, above of = 5] (7) {$7$};

    \path (5) edge[udiedge] (7);
    \path (5) edge[udiedge] (6);
    \path (7) edge[udiedge] (6);    


    \node[circ, node distance = 2cm, above left of = 1] (8) {$8$};
    \node[circ, left of = 8] (9) {$9$};
    \node[circ, above of = 8] (10) {$10$};

    \path (9) edge[udiedge] (10);
    \path (10) edge[udiedge] (8);
    \path (9) edge[udiedge] (8);    


    \node[circ, node distance = 2cm, below right of = 4] (11) {$11$};
    \node[circ, below of = 11] (12) {$12$};
    \node[circ, right of = 11] (13) {$13$};
    \node[circ, right of = 12] (14) {$14$};

    \path (11) edge[udiedge] (12);
    \path (11) edge[udiedge] (13);
    \path (12) edge[udiedge] (14);
    \path (13) edge[udiedge] (14);

    \node[circ, node distance = 2cm, below left of = 2] (15)
    {$15$};

    \path (3) edge[udiedge] (5);
    \path (1) edge[udiedge] (8);
    \path (4) edge[udiedge] (11);
    \path (2) edge[udiedge] (15);
\end{tikzpicture}

答案1

另一个选项是使用clip命令填充特定区域,如下所示。需要scope环境限制填充操作,否则它将从此生效。

编辑:其实,更简单的方法是直接填充形状。将范围代码替换为以下代码。坐标 1.7 由 \sqrt(1.2^2+1.2^2) 确定。

\fill[blue!20!red!20]     (0,1.7)  -- (45:1.2)   arc (45:135:1.2)    -- cycle;
\fill[green!20!red!20]    (1.7,0)  -- (-45:1.2)  arc (-45:45:1.2)    -- cycle;
\fill[green!20!yellow!20] (0,-1.7) -- (-45:1.2)  arc (-45:-135:1.2)  -- cycle;
\fill[black]              (-1.7,0) -- (-135:1.2) arc (-135:-225:1.2) -- cycle;



\clip (-1.7,0) --  (-135:1.2) arc (-135:-225:1.2)-- cycle;  % shown by the black area

在此处输入图片描述

代码

\documentclass[a4paper]{article}

\usepackage{tikz}

\begin{document}

    \begin{tikzpicture}[scale=1]
    \tikzstyle{rect}=[rectangle, thick, 
    draw=black, rounded corners, node distance = 2cm]
    \tikzstyle{diedge}=[->,thick,shorten <=2pt, shorten >=2pt]
    \tikzstyle{udiedge}=[thick,shorten <=2pt, shorten >=2pt]
    \tikzstyle{circ}=[circle, thick, 
    draw=black, rounded corners, node distance = 1cm,
    font=\tiny]

    \fill[blue!20] (-2.4,2.4) circle (1.2cm);
    \fill[red!20] (2.4,2.4) circle (1.2cm);
    \fill[green!20] (2.4,-2.4) circle (1.2cm);
    \fill[yellow!20] (-2.4,-2.4) circle (1.2cm);

    \draw[blue!20,line width=2.4cm] (-2.4,2.4) -- (0,0);
    \draw[red!20,line width=2.4cm] (2.4,2.4) -- (0,0);
    \draw[green!20,line width=2.4cm] (2.4,-2.4) -- (0,0);
    \draw[yellow!20,line width=2.4cm] (-2.4,-2.4) -- (0,0);
    \fill[white] (0,0) circle (1.2cm);

\begin{scope}
\clip (0,1.7) --  (45:1.2) arc (45:135:1.2)-- cycle;
\fill[blue!20!red!20]  (0,0) circle [radius=2cm];
\end{scope}

\begin{scope}
\clip (1.7,0) --  (-45:1.2) arc (-45:45:1.2)-- cycle;
\fill[green!20!red!20]  (0,0) circle [radius=2cm];
\end{scope}

\begin{scope}
\clip (0,-1.7) --  (-45:1.2) arc (-45:-135:1.2)-- cycle;
\fill[green!20!yellow!20]  (0,0) circle [radius=2cm];
\end{scope}

\begin{scope}
\clip (-1.7,0) --  (-135:1.2) arc (-135:-225:1.2)-- cycle;
\fill[black]  (0,0) circle [radius=2cm];
\end{scope}

    \node[circ] at (-0.5,0.5) (1) {$1$};
    \node[circ, below of = 1] (2) {$2$};
    \node[circ, right of = 1] (3) {$3$};
    \node[circ, right of = 2] (4) {$4$};

    \path (1) edge[udiedge] (2);
    \path (1) edge[udiedge] (3);
    \path (2) edge[udiedge] (4);
    \path (3) edge[udiedge] (4);

    \node[circ, node distance = 2cm, above right of = 3] (5) {$5$};
    \node[circ, right of = 5] (6) {$6$};
    \node[circ, above of = 5] (7) {$7$};

    \path (5) edge[udiedge] (7);
    \path (5) edge[udiedge] (6);
    \path (7) edge[udiedge] (6);    


    \node[circ, node distance = 2cm, above left of = 1] (8) {$8$};
    \node[circ, left of = 8] (9) {$9$};
    \node[circ, above of = 8] (10) {$10$};

    \path (9) edge[udiedge] (10);
    \path (10) edge[udiedge] (8);
    \path (9) edge[udiedge] (8);    


    \node[circ, node distance = 2cm, below right of = 4] (11) {$11$};
    \node[circ, below of = 11] (12) {$12$};
    \node[circ, right of = 11] (13) {$13$};
    \node[circ, right of = 12] (14) {$14$};

    \path (11) edge[udiedge] (12);
    \path (11) edge[udiedge] (13);
    \path (12) edge[udiedge] (14);
    \path (13) edge[udiedge] (14);

    \node[circ, node distance = 2cm, below left of = 2] (15)
    {$15$};

    \path (3) edge[udiedge] (5);
    \path (1) edge[udiedge] (8);
    \path (4) edge[udiedge] (11);
    \path (2) edge[udiedge] (15);
\end{tikzpicture}

\end{document}

答案2

您可以使用blend group(参见第 23.3 节pgf手动的v3.0.0),它根据指定的混合模式混合颜色。手册中概述了可能的模式,但screenlighten可能最适合您想要的输出。

这种方法使组内绘制的项目变得平整,因此我使用了略有不同的绘制程序,因为我们不需要隐藏图像中心附近的交叉点。

基本设置如下:

\documentclass[tikz,border=12pt]{standalone}
\begin{document}
\begin{tikzpicture}
    \begin{scope}[%
      blend group=screen, % see Section 23.3 of PGF Manual (v3.0.0)
      radius=1.2, delta angle=180, % common settings for arc commands
    ]
    \path[fill=  blue!50] (225:1.2) arc[start angle=225] -- ++(-2.4,+2.4) arc[start angle= 45] -- cycle;
    \path[fill=   red!50] (135:1.2) arc[start angle=135] -- ++(+2.4,+2.4) arc[start angle=315] -- cycle;
    \path[fill= green!50] ( 45:1.2) arc[start angle= 45] -- ++(+2.4,-2.4) arc[start angle=225] -- cycle;
    \path[fill=yellow!50] (315:1.2) arc[start angle=315] -- ++(-2.4,-2.4) arc[start angle=135] -- cycle;
    \end{scope}
\end{tikzpicture}
\end{document}

以及渲染的输出(请注意,并非所有 PDF 查看器/打印机都能正确处理所有混合模式):

在此处输入图片描述

结果看起来有点“奇怪”,因为包含黄色意味着混合并不是真正“对称”的(如果这有任何意义的话)。仅对于 RGB,您将获得以下结果:

在此处输入图片描述

相关内容