在二分图中包含荣誉以将节点分组在一起

在二分图中包含荣誉以将节点分组在一起

我想在二分图中包含赞誉,以显示节点属于某个组。使用以下代码,我创建了以下二分图:

\begin{figure}[H]
\centering
\definecolor{myblue}{RGB}{80,80,160}
\definecolor{mygreen}{RGB}{80,160,80}
\begin{tikzpicture}[thick,
  every node/.style={draw,circle},
  fsnode/.style={fill=myblue},
  ssnode/.style={fill=mygreen},
  every fit/.style={ellipse,draw,inner sep=-2pt,text width=2cm},
  ->,shorten >= 3pt,shorten <= 3pt
]
% the vertices of P
\begin{scope}[start chain=going below,node distance=10mm]
  \node[fsnode,on chain] (f1) [label=left: $p_{1}$] {};
  \node[fsnode,on chain] (f2) [label=left: $p_{2}$] {};
  \node[fsnode,on chain] (f3) [label=left: ] {};
  \node[fsnode,on chain] (f4) [label=left: $p_{n}$] {};
\end{scope}

% the vertices of L
\begin{scope}[xshift=6cm,yshift=0cm,start chain=going below,node distance=10mm]
  \node[ssnode,on chain] (s1) [label=right: $l_{1}$] {};
  \node[ssnode,on chain] (s2) [label=right: $l_{2}$] {};
  \node[ssnode,on chain] (s3) [label=right: ] {};
  \node[ssnode,on chain] (s4) [label=right: $l_{m}$] {};
\end{scope}

% the set P
\node [myblue,fit=(f1) (f4),label=above:$Products$] {};
% the set L
\node [mygreen,fit=(s1) (s4),label=above:$Locations$] {};
% the edges
\draw[ultra thin,dashed,-] (f2) -- (f3);
\draw[ultra thin,dashed,-] (f3) -- (f4);
\draw[ultra thin,dashed,-] (s2) -- (s3);
\draw[ultra thin,dashed,-] (s3) -- (s4);
\draw[-,dotted] (f1) -- (s1);
\draw[-,dotted] (f1) -- (s2);
\draw[-,dotted] (f1) -- (s3);
\draw[-,dotted] (f1) -- (s4);
\draw[-,dotted] (f2) -- (s1);
\draw[-,dotted] (f2) -- (s2);
\draw[-,dotted] (f2) -- (s3);
\draw[-,dotted] (f2) -- (s4);
\draw[-,dotted] (f3) -- (s1);
\draw[-,dotted] (f3) -- (s2);
\draw[-,dotted] (f3) -- (s3);
\draw[-,dotted] (f3) -- (s4);
\draw[-,dotted] (f4) -- (s1);
\draw[-,dotted] (f4) -- (s2);
\draw[-,dotted] (f4) -- (s3);
\draw[-,dotted] (f4) -- (s4);
\end{tikzpicture}
\end{figure}

这引发了如下图像: 在此处输入图片描述

但我想要的是以下图像: 在此处输入图片描述

我必须添加什么代码才能使用荣誉或类似的东西?

答案1

你可以使用图书馆brace里的装饰品decorations.pathreplacing我用的是很多sshift来让它看起来更好一些,但你可以选择省略它们。通过使用@土拨鼠,所有shifts 都可以省略。

这是大括号的代码:

    \node[fit={(f1)(f3)(f3-|set1.west)},subset] (c1-fit) {};
    \node[fit={(f4)(f5)(f5-|set1.west)},subset] (c2-fit) {};
    \node[fit={(f7)(f8)(f8-|set1.west)},subset] (c3-fit) {};
    \node[fit={(s1)(s2)(s2-|set2.east)},subset] (lc1-fit) {};
    \node[fit={(s3)(s5)(s5-|set2.east)},subset] (lc2-fit) {};
    \node[fit={(s8)(s8)(s8-|set2.east)},subset,inner ysep=10pt] (lc3-fit) {};

    \begin{scope}[decoration={brace,amplitude=3mm}]
        \begin{scope}[decoration={mirror},every node/.style={midway,left,xshift=-3mm}]
            \draw[decorate] (c1-fit.north west) -- (c1-fit.south west) node(C1){$ C_1 $};
            \draw[decorate] (c2-fit.north west) -- (c2-fit.south west) node(C2){$ C_2 $};
            \draw[decorate] (c3-fit.north west) -- (c3-fit.south west) node(Cg){$ C_\gamma $};
        \end{scope}
        \begin{scope}[every node/.style={midway,right,xshift=3mm}]
            \draw[decorate] (lc1-fit.north east) -- (lc1-fit.south east) node(LC1){$ L^{C_1} $};
            \draw[decorate] (lc2-fit.north east) -- (lc2-fit.south east) node(LC2){$ L^{C_2} $};
            \draw[decorate] (lc3-fit.north east) -- (lc3-fit.south east) node(LCg){$ L^{C_\gamma} $};
        \end{scope}
    \end{scope}

在此处输入图片描述

我还使用了一些\foreach循环来简化所有类似线条的绘制,并放置没有标签的圆形节点。

我还注意到,您->为整个进行了设置tikzpicture,然后继续-为所有\draw命令设置键,这感觉有点适得其反,所以我删除了它。此外,我将 更改every node/.stylebase node/.style并使用样式的继承来确保 样式fsnodessnode样式 都继承其属性。最后,我更改了两个顶部标签的字体(产品位置) 到\itshape,而不是在数学模式中排版这些单词,因为这样看起来更好。

梅威瑟:

\documentclass[tikz,margin=2mm]{standalone}

\usetikzlibrary{chains,fit,shapes,decorations.pathreplacing}

\begin{document}
%    \begin{figure}[H]
    \centering
    \definecolor{myblue}{RGB}{80,80,160}
    \definecolor{mygreen}{RGB}{80,160,80}
    \begin{tikzpicture}[thick,
        base node/.style={draw,circle},
        fsnode/.style={base node,fill=myblue},
        ssnode/.style={base node,fill=mygreen},
        set/.style={ellipse,draw,inner xsep = 20pt,inner ysep=-20pt},
        subset/.style={inner sep=3pt},
        shorten >= 3pt,shorten <= 3pt
    ]
        % the vertices of P
        \begin{scope}[start chain=going below,node distance=10mm]
            \node[fsnode,on chain] (f1) [label=left: $p_{1}$] {};
            \node[fsnode,on chain] (f2) [label=left: $p_{2}$] {};
            \foreach \i in {3,...,7}{
                \node[fsnode,on chain] (f\i) {};
            }
            \node[fsnode,on chain] (f8) [label=left: $p_{n}$] {};
        \end{scope}

        % the vertices of L
        \begin{scope}[xshift=6cm,yshift=0cm,start chain=going below,node distance=10mm]
            \node[ssnode,on chain] (s1) [label=right: $l_{1}$] {};
            \node[ssnode,on chain] (s2) [label=right: $l_{2}$] {};
            \foreach \i in {3,...,7}{
                \node[ssnode,on chain] (s\i) {};
            }
            \node[ssnode,on chain] (s8) [label=right: $l_{m}$] {};
        \end{scope}

        % the set P
        \node [set,myblue,fit=(f1) (f8),label={[font=\itshape]above:Products}] (set1) {};
        % the set L
        \node [set,mygreen,fit=(s1) (s8),label={[font=\itshape]above:Locations}] (set2) {};
        % the edges
        \foreach \i [count=\j from 1] in {2,...,8}{
            \draw[thin,dashed] (f\j) -- (f\i);
            \draw[thin,dashed] (s\j) -- (s\i);
        }
        \foreach \i in {1,...,8}{
            \foreach \j in {1,...,8}{
                \draw[dotted] (f\i) -- (s\j);
            }
        }

        \node[fit={(f1)(f3)(f3-|set1.west)},subset] (c1-fit) {};
        \node[fit={(f4)(f5)(f5-|set1.west)},subset] (c2-fit) {};
        \node[fit={(f7)(f8)(f8-|set1.west)},subset] (c3-fit) {};
        \node[fit={(s1)(s2)(s2-|set2.east)},subset] (lc1-fit) {};
        \node[fit={(s3)(s5)(s5-|set2.east)},subset] (lc2-fit) {};
        \node[fit={(s8)(s8)(s8-|set2.east)},subset,inner ysep=10pt] (lc3-fit) {};

        \begin{scope}[decoration={brace,amplitude=3mm}]
            \begin{scope}[decoration={mirror},every node/.style={midway,left,xshift=-3mm}]
                \draw[decorate] (c1-fit.north west) -- (c1-fit.south west) node (C1) {$ C_1 $};
                \draw[decorate] (c2-fit.north west) -- (c2-fit.south west) node (C2) {$ C_2 $};
                \draw[decorate] (c3-fit.north west) -- (c3-fit.south west) node (Cg) {$ C_\gamma $};
            \end{scope}
            \begin{scope}[every node/.style={midway,right,xshift=3mm}]
                \draw[decorate] (lc1-fit.north east) -- (lc1-fit.south east) node (LC1) {$ L^{C_1} $};
                \draw[decorate] (lc2-fit.north east) -- (lc2-fit.south east) node (LC2) {$ L^{C_2} $};
                \draw[decorate] (lc3-fit.north east) -- (lc3-fit.south east) node (LCg) {$ L^{C_\gamma} $};
            \end{scope}
        \end{scope}

        \draw[thin,dashed] (C2) -- (Cg);
        \draw[thin,dashed] (LC2) -- (LCg);
    \end{tikzpicture}
%    \end{figure}
\end{document}

附言:我真的很喜欢chains!非常适合这个应用程序。

编辑@塞巴斯蒂亚诺)
有人可能会认为,将集合的标签设置为直立形状在审美上更可接受。我将选择权留给您。

在此处输入图片描述

\documentclass[tikz,margin=2mm]{standalone}

\usetikzlibrary{chains,fit,shapes,decorations.pathreplacing}

\begin{document}
%    \begin{figure}[H]
    \centering
    \definecolor{myblue}{RGB}{80,80,160}
    \definecolor{mygreen}{RGB}{80,160,80}
    \begin{tikzpicture}[thick,
        base node/.style={draw,circle},
        fsnode/.style={base node,fill=myblue},
        ssnode/.style={base node,fill=mygreen},
        set/.style={ellipse,draw,inner xsep = 20pt,inner ysep=-20pt},
        subset/.style={inner sep=3pt},
        shorten >= 3pt,shorten <= 3pt
    ]
        % the vertices of P
        \begin{scope}[start chain=going below,node distance=10mm]
            \node[fsnode,on chain] (f1) [label=left: $p_{1}$] {};
            \node[fsnode,on chain] (f2) [label=left: $p_{2}$] {};
            \foreach \i in {3,...,7}{
                \node[fsnode,on chain] (f\i) {};
            }
            \node[fsnode,on chain] (f8) [label=left: $p_{n}$] {};
        \end{scope}

        % the vertices of L
        \begin{scope}[xshift=6cm,yshift=0cm,start chain=going below,node distance=10mm]
            \node[ssnode,on chain] (s1) [label=right: $l_{1}$] {};
            \node[ssnode,on chain] (s2) [label=right: $l_{2}$] {};
            \foreach \i in {3,...,7}{
                \node[ssnode,on chain] (s\i) {};
            }
            \node[ssnode,on chain] (s8) [label=right: $l_{m}$] {};
        \end{scope}

        % the set P
        \node [set,myblue,fit=(f1) (f8),label=above:Products] (set1) {};
        % the set L
        \node [set,mygreen,fit=(s1) (s8),label=above:Locations] (set2) {};
        % the edges
        \foreach \i [count=\j from 1] in {2,...,8}{
            \draw[thin,dashed] (f\j) -- (f\i);
            \draw[thin,dashed] (s\j) -- (s\i);
        }
        \foreach \i in {1,...,8}{
            \foreach \j in {1,...,8}{
                \draw[dotted] (f\i) -- (s\j);
            }
        }

        \node[fit={(f1)(f3)(f3-|set1.west)},subset] (c1-fit) {};
        \node[fit={(f4)(f5)(f5-|set1.west)},subset] (c2-fit) {};
        \node[fit={(f7)(f8)(f8-|set1.west)},subset] (c3-fit) {};
        \node[fit={(s1)(s2)(s2-|set2.east)},subset] (lc1-fit) {};
        \node[fit={(s3)(s5)(s5-|set2.east)},subset] (lc2-fit) {};
        \node[fit={(s8)(s8)(s8-|set2.east)},subset,inner ysep=10pt] (lc3-fit) {};

        \begin{scope}[decoration={brace,amplitude=3mm}]
            \begin{scope}[decoration={mirror},every node/.style={midway,left,xshift=-3mm}]
                \draw[decorate] (c1-fit.north west) -- (c1-fit.south west) node (C1) {$ C_1 $};
                \draw[decorate] (c2-fit.north west) -- (c2-fit.south west) node (C2) {$ C_2 $};
                \draw[decorate] (c3-fit.north west) -- (c3-fit.south west) node (Cg) {$ C_\gamma $};
            \end{scope}
            \begin{scope}[every node/.style={midway,right,xshift=3mm}]
                \draw[decorate] (lc1-fit.north east) -- (lc1-fit.south east) node (LC1) {$ L^{C_1} $};
                \draw[decorate] (lc2-fit.north east) -- (lc2-fit.south east) node (LC2) {$ L^{C_2} $};
                \draw[decorate] (lc3-fit.north east) -- (lc3-fit.south east) node (LCg) {$ L^{C_\gamma} $};
            \end{scope}
        \end{scope}

        \draw[thin,dashed] (C2) -- (Cg);
        \draw[thin,dashed] (LC2) -- (LCg);
    \end{tikzpicture}
%    \end{figure}
\end{document}

相关内容