如何改进具有大量边的 TikZ 图?

如何改进具有大量边的 TikZ 图?

我有一张有 5 个节点和相互连接的边的图,这导致了块状角:

\documentclass[parskip=half]{scrreprt}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes}
\tikzset{subjekt/.style={rectangle,draw,minimum height=.7cm, minimum width=2.5cm, align=center},
         strom/.style={line width=3pt, shorten >=2pt,shorten <=2pt},
         gueterstrom/.style={strom,Goldenrod,->},
         geldstrom/.style={strom,SlateGrey,->}}
\begin{document}
  \begin{tikzpicture}[node distance=5cm]
    \node[subjekt] (H) at (0,0) {Haushalt};
    \node[subjekt] (U) [below right= of H] {Unternehmen};
    \node[subjekt] (S) [above right= of U] {Staat};
    \node[subjekt] (B) [below left= of U]  {Banken};
    \node[subjekt] (A) [below right= of U] {Ausland};
    \draw (H) edge[bend right=10,geldstrom] node[left] {3} (B);
    \draw (B) edge[bend right=10,geldstrom] node[right] {9} (H);
    \draw (H) edge[bend right=10,geldstrom] node[below] {1} (S);
    \draw (S) edge[bend right=10,geldstrom] node[above] {13} (H);
    \draw (H) edge[bend right=10,geldstrom] node[below left] {2} (U);
    \draw (U) edge[bend right=10,geldstrom] node[above right] {6} (H);
    \draw (H) edge[bend right=22,geldstrom, near end] node[below left] {4} (A);
    \draw (A) edge[bend right=22,geldstrom, near end] node[above right] {20} (H);
    \draw (B) edge[bend right=10,geldstrom] node[below right] {10} (U);
    \draw (U) edge[bend right=10,geldstrom] node[above left] {7} (B);
    \draw (B) edge[bend right=10,geldstrom] node[below] {12} (A);
    \draw (A) edge[bend right=10,geldstrom] node[above] {19} (B);
    \draw (B) edge[bend right=22,geldstrom, near end] node[below right] {11} (S);
    \draw (S) edge[bend right=22,geldstrom, near end] node[above left] {16} (B);
    \draw (A) edge[bend right=10,geldstrom] node[above right] {18} (U);
    \draw (U) edge[bend right=10,geldstrom] node[below left] {8} (A);
    \draw (A) edge[bend right=10,geldstrom] node[right] {17} (S);
    \draw (S) edge[bend right=10,geldstrom] node[left] {14} (A);
    \draw (S) edge[bend right=10,geldstrom] node[above left] {15} (U);
    \draw (U) edge[bend right=10,geldstrom] node[below right] {5} (S);
  \end{tikzpicture}
\end{document}

姆韦

有什么想法可以改善角落的情况吗?

答案1

像这样?

在此处输入图片描述

编辑

与您的代码相比,绘制上述图像的 mwe 代码有以下变化/改进:

  • 通过引入库,quotes边缘标签的编写变得简单且简短。例如,

\draw (H) edge[bend right=10,geldstrom] node[left] {3} (B);

有可能获得相同的结果

\draw (H) edge ["3"] (B)

其中我为边缘定义样式,它决定边缘颜色厚度短促弯曲角度

every edge/.style = {draw=SlateGrey, -Stealth, line width=2pt,
                     shorten <=5pt, shorten > = 5pt,
                     bend right=8},
  • 弯曲的角度基本上是通过反复试验的方法确定的。然而,我利用了绘制类似图表的经验和几何知识(我的第一次尝试已经成功)。在边缘样式中,我确定最常见的角度,四个边缘的例外是在本地确定的。
  • 由于边样式已经定义,因此不再需要\draw ... :为每条边进行编写。只需编写它们然后列出所有边(参见下面的 mwe)。
  • 通过样式edge quotes来确定字体的大小和边缘标签的位置。
  • 节点形状的大小也发生了变化。使用更大的高度(并且缩短边缘)防止边缘重叠。
  • 与图书馆arrows.meta定义在体面的方式箭头
  • 的总结tikzpicture如下:

    • 风格subjekt焕然一新
    • 样式stromgueterstromgeldstrom被替换为every edgeevery edge quotes

\documentclass[parskip=half]{scrreprt}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                positioning,
                quotes,
                shapes}

\begin{document}
  \begin{tikzpicture}[auto=right,
    node distance = 44mm,
   subjekt/.style = {draw, semithick, minimum height=12mm, minimum width=24mm},
every edge/.style = {draw=SlateGrey, -Stealth, line width=2pt,
                     shorten <=5pt, shorten > = 5pt,
                     bend right=8},
every edge quotes/.append style = {inner sep=2pt, font=\small, pos=0.6},
                    ]
\node (H) [subjekt]                     {Haushalt};
\node (U) [subjekt,below right=of H]    {Unternehmen};
\node (S) [subjekt,above right=of U]    {Staat};
\node (B) [subjekt,below left =of U]    {Banken};
\node (A) [subjekt,below right=of U]    {Ausland};
%
\draw   (H) edge ["3"]  (B)
        (B) edge ["9"]  (H)
        (H) edge ["1"]  (S)
        (S) edge ["13"] (H)
        (H) edge ["2"]  (U)
        (U) edge ["6"]  (H)
%
        (H) edge [bend right=24, "4"]  (A)
        (A) edge [bend right=24, "20"] (H)
%
        (B) edge ["10"] (U)
        (U) edge ["7"]  (B)
        (B) edge ["12"] (A)
        (A) edge ["19"] (B)
%
        (B) edge [bend right=24, "11"] (S)
        (S) edge [bend right=24, "16"] (B)
%
        (A) edge ["18"] (U)
        (U) edge ["8"]  (A)
        (A) edge ["17"] (S)
        (S) edge ["14"] (A)
        (S) edge ["15"] (U)
        (U) edge ["5"]  (S);
  \end{tikzpicture}
\end{document}

答案2

您选择的排列方式对于全连接图来说非常不理想。最好以圆形布局排列。

TikZgraphdrawing需要 LuaTeX。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphs,graphdrawing,quotes}
\usegdlibrary{circular}
\begin{document}

\begin{tikzpicture}
  \graph[
  simple necklace layout,
  edges={bend right=10},
  edge quotes={fill=white,inner sep=1pt,font=\tiny},
  nodes={draw},
  node sep=5em,
  ] {
    Haushalt -> {Staat [>"1"], Unternehmen [>"2"], Banken [>"3"], Ausland [>"4"]},
    Unternehmen -> {Haushalt [>"6"], Staat [>"5"], Banken [>"7"], Ausland [>"8"]},
    Banken -> {Haushalt [>"9"], Staat [>"11"], Unternehmen [>"10"], Ausland [>"12"]},
    Staat -> {Haushalt [>"13"], Unternehmen [>"15"], Banken [>"16"], Ausland [>"14"]},
    Ausland -> {Haushalt [>"20"], Staat [>"17"], Unternehmen [>"18"], Banken [>"19"]}
  };
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

如何改善角落的情况?去掉角落!我还改变了箭头的尖端,并稍微收紧了一点。

\documentclass[parskip=half]{scrreprt}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes}
\tikzset{subjekt/.style={circle,draw,minimum height=.7cm, minimum width=2.2cm, align=center},
         strom/.style={line width=2pt, shorten >=2pt,shorten <=2pt},
         gueterstrom/.style={strom,Goldenrod,->},
         geldstrom/.style={strom,SlateGrey,->}}
\begin{document}
  \begin{tikzpicture}[node distance=4cm,>=stealth]
    \node[subjekt] (H) at (0,0) {Haushalt};
    \node[subjekt] (U) [below right= of H] {Unternehmen};
    \node[subjekt] (S) [above right= of U] {Staat};
    \node[subjekt] (B) [below left= of U]  {Banken};
    \node[subjekt] (A) [below right= of U] {Ausland};
    \draw (H) edge[bend right=10,geldstrom] node[left] {3} (B);
    \draw (B) edge[bend right=10,geldstrom] node[right] {9} (H);
    \draw (H) edge[bend right=10,geldstrom] node[below] {1} (S);
    \draw (S) edge[bend right=10,geldstrom] node[above] {13} (H);
    \draw (H) edge[bend right=10,geldstrom] node[below left] {2} (U);
    \draw (U) edge[bend right=10,geldstrom] node[above right] {6} (H);
    \draw (H) edge[bend right=22,geldstrom, near end] node[below left] {4} (A);
    \draw (A) edge[bend right=22,geldstrom, near end] node[above right] {20} (H);
    \draw (B) edge[bend right=10,geldstrom] node[below right] {10} (U);
    \draw (U) edge[bend right=10,geldstrom] node[above left] {7} (B);
    \draw (B) edge[bend right=10,geldstrom] node[below] {12} (A);
    \draw (A) edge[bend right=10,geldstrom] node[above] {19} (B);
    \draw (B) edge[bend right=22,geldstrom, near end] node[below right] {11} (S);
    \draw (S) edge[bend right=22,geldstrom, near end] node[above left] {16} (B);
    \draw (A) edge[bend right=10,geldstrom] node[above right] {18} (U);
    \draw (U) edge[bend right=10,geldstrom] node[below left] {8} (A);
    \draw (A) edge[bend right=10,geldstrom] node[right] {17} (S);
    \draw (S) edge[bend right=10,geldstrom] node[left] {14} (A);
    \draw (S) edge[bend right=10,geldstrom] node[above left] {15} (U);
    \draw (U) edge[bend right=10,geldstrom] node[below right] {5} (S);
  \end{tikzpicture}
\end{document}

在此处输入图片描述

答案4

根据@marmot 的想法,为了避免最大交叉:

在此处输入图片描述

\documentclass[parskip=half]{scrreprt}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}

\pagestyle{empty}
\usetikzlibrary{arrows.meta,positioning,shapes,decorations.markings}
\tikzset{subjekt/.style={rectangle,draw,minimum height=.7cm, minimum width=2.5cm, align=center},
         strom/.style={line width=3pt, shorten >=2pt,shorten <=2pt},
         gueterstrom/.style={strom,Goldenrod,->},
         geldstrom/.style={strom,SlateGrey,-,
         postaction={decoration={markings,
            mark=at position 0.7 with {\arrow[shorten >=0pt,shorten <=0pt,->]
            {>}}},decorate}},
         bendstrom/.style={strom,SlateGrey,-,
         postaction={decoration={markings,
            mark=at position 0.72 with {\arrow[shorten >=0pt,shorten <=0pt,->]
            {>}}},decorate}}}
\begin{document}
  \begin{tikzpicture}[node distance=5cm,>=Stealth]
    \node[subjekt] (H) at (-1.5,3.5) {Haushalt};
    \node[subjekt] (U) at (2,0) {Unternehmen};
    \node[subjekt] (S) at (5,5) {Staat};
    \node[subjekt] (B) at (-1.5,-3.5)  {Banken};
    \node[subjekt] (A) at (5,-5) {Ausland};
    \begin{scope}[inner sep=6pt,pos=0.68]
    \draw (H) edge[geldstrom] node[left] {3} (B);
    \draw (B) edge[geldstrom] node[left] {9} (H);
    \draw (H) edge[geldstrom] node[above] {1} (S);
    \draw (S) edge[geldstrom] node[above] {13} (H);
    \draw (H) edge[geldstrom] node[above right] {2} (U);
    \draw (U) edge[geldstrom] node[above right] {6} (H);
    \draw (B) edge[geldstrom] node[below right] {10} (U);
    \draw (U) edge[geldstrom] node[below right] {7} (B);
    \draw (B) edge[geldstrom] node[below] {12} (A);
    \draw (A) edge[geldstrom] node[below] {19} (B);
    \draw (A) edge[geldstrom] node[above right] {18} (U);
    \draw (U) edge[geldstrom] node[above right] {8} (A);
    \draw (A) edge[geldstrom] node[right] {17} (S);
    \draw (S) edge[geldstrom] node[right] {14} (A);
    \draw (S) edge[geldstrom] node[below right] {15} (U);
    \draw (U) edge[geldstrom] node[below right] {5} (S);
    \end{scope}
    \begin{scope}[inner sep=6pt,pos=0.78]
    \draw (H) edge[bend right=80,bendstrom,looseness=1.7] node[below left] {4} (A);
    \draw (A) edge[bend left=80,bendstrom,looseness=1.7] node[below left] {20} (H);
    \draw (B) edge[bend left=80,bendstrom,looseness=1.7] node[above left] {11} (S);
    \draw (S) edge[bend right=80,bendstrom,looseness=1.7] node[above left] {16} (B);
    \end{scope}
  \end{tikzpicture}
\end{document}

相关内容