如何用 tikz 绘制网络流?

如何用 tikz 绘制网络流?

我想要制作与下图完全相同的图片。

在此处输入图片描述

我开始这样做

\documentclass[twocolumn, 12pt]{article}

\usepackage[latin9]{inputenc}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{tikz}
\usepackage[margin=1in]{geometry}
\usetikzlibrary{tikzmark,calc,matrix,positioning}
\usepackage{pgfplots}

\usetikzlibrary{calc,fit,shapes.geometric}
\pgfdeclarelayer{signal}
\pgfsetlayers{signal,main}
\usetikzlibrary{chains,shapes.multipart}
\usetikzlibrary{automata,positioning}
\usetikzlibrary{arrows}% To get more arrow heads
\tikzstyle{printersafe}=[snake=snake,segment amplitude=0 pt]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

\sloppy
\pgfplotsset{compat=1.12}


\begin{document}

\begin{figure}[ht!]
  \centering
  \resizebox{.5\textwidth}{!}{%
    \begin{tikzpicture}%[>=triangle 45]
      \node[circle,draw=black, fill=gray, fill opacity = 0.3, inner
      sep=0pt,minimum size=2pt] (c1) at (0,0) {};
      \node[circle,draw=black, fill=gray, fill opacity = 0.3, inner
      sep=0pt,minimum size=2pt] (c2) at (1,0) {};
      \node[circle,draw=black, fill=gray, fill opacity = 0.3, inner
      sep=0pt,minimum size=2pt] (c3) at (0,1) {};
      \node[circle,draw=black, fill=gray, fill opacity = 0.3, inner
      sep=0pt,minimum size=2pt] (c4) at (1,1) {};
      \node[circle,draw=black, fill=gray, fill opacity = 0.3, inner
      sep=0pt,minimum size=2pt] (c5) at (-0.5,0.5) {$s$};
      \node[circle,draw=black, fill=gray, fill opacity = 0.3, inner
      sep=0pt,minimum size=2pt] (c6) at (1.5,0.5) {$t$};

      \draw [->] (c1) -- (c2);
      \draw [->] (c5) -- (c1);
      \draw [->] (c5) -- (c3);
    \end{tikzpicture}
  }
  \caption{Model}
  \label{fig:sys}
\end{figure}
    \end{document}

但这让我有些奇怪。

在此处输入图片描述

这个数字显然没有产生预期的结果。你能帮助我吗?

答案1

这相当接近了。一些注意事项: - 我从序言中删除了所有不必要的内容,还有figure环境和resizebox。(我个人认为这resizebox应该是缩放图表时要做的最后一件事tikz,即使它非常简单)。

  • 代码中节点文本与节点本身相比“较大”的原因在于您有inner sep=0pt,minimum size=2ptinner sep定义从节点内容到边框的距离,并且节点内容始终大于 2pt。我保留了该inner sep值,但将minimum size增加到20pt

  • 节点是相对于彼此定位的。例如,v_2节点被放置在above right=of c1,其中c1是节点的名称s。这样做的好处是

    1. 您不必担心绝对坐标;
    2. 修改节点之间的距离变得非常容易:只需更改 的值即可node distancenode distance=0.6cm and 1.2cm第一个长度是垂直距离,第二个是水平距离。
  • arrows.meta库是该库的替代品arrows,并为您提供了很大的潜力来定制箭头(如果需要)(参见手册)

  • 也适用fill opacity于文本。您可以删除fill opacity,并设置 ,fill=gray!30而不仅仅是gray。我改为添加了text opacity=1,它将覆盖 中的不透明度值fill

  • 我定义了一种mycircle风格以避免重复。

  • 最后我使用\foreach循环来绘制箭头。你也可以使用多个\draw [myarrow] (<node 1>) node[above] {<number>} (<node 2>);,但使用循环可以减少重复次数。

  • 为了绘制两个平行箭头,我使用了角度锚点语法。例如,nodename.60节点边界上的点与水平方向成 60 度角。

在此处输入图片描述

\documentclass[border=4mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}
\begin{document}
    \begin{tikzpicture}[
      mycircle/.style={
         circle,
         draw=black,
         fill=gray,
         fill opacity = 0.3,
         text opacity=1,
         inner sep=0pt,
         minimum size=20pt,
         font=\small},
      myarrow/.style={-Stealth},
      node distance=0.6cm and 1.2cm
      ]
      \node[mycircle] (c1) {$s$};
      \node[mycircle,below right=of c1] (c2) {$v_2$};
      \node[mycircle,right=of c2] (c3) {$v_4$};
      \node[mycircle,above right=of c1] (c4) {$v_1$};
      \node[mycircle,right=of c4] (c5) {$v_3$};
      \node[mycircle,below right=of c5] (c6) {$t$};

    \foreach \i/\j/\txt/\p in {% start node/end node/text/position
      c1/c2/8/below,
      c1/c4/11/above,
      c2/c3/11/below,
      c3/c6/4/below,
      c4/c5/12/above,
      c5/c6/15/above,
      c5/c2/4/below,
      c3/c5/7/below,
      c2.70/c4.290/1/below}
       \draw [myarrow] (\i) -- node[sloped,font=\small,\p] {\txt} (\j);


     % draw this outside loop to get proper orientation of 10
     \draw [myarrow] (c4.250) -- node[sloped,font=\small,above,rotate=180] {10} (c2.110);
    \end{tikzpicture}
\end{document}

相关内容