使用 TikZ 在 SIRS 圖中繪製箭頭

使用 TikZ 在 SIRS 圖中繪製箭頭

我正在嘗試創建一個 SIRS(易感-感染-恢復-易感)圖,如下所示: 在此輸入影像描述

我快到了;除了頂部箭頭從“恢復”到“易受影響”之外,我還有所有其他內容。這是我目前的程式碼:

\documentclass[tikz, border=10pt]{standalone}
\usetikzlibrary{positioning, arrows.meta}

\begin{document}

\begin{tikzpicture}[
    node distance=2.5cm,
    arrow/.style={->, >=Stealth, thick},
    box/.style={draw, rectangle, rounded corners, minimum width=2cm, minimum height=1cm, align=center}
]

% Nodes
\node[box] (S) {Susceptible};
\node[box, right=of S] (I) {Infectious};
\node[box, right=of I] (R) {Recovered};

% Arrows
\draw[arrow] (S) -- node[above]{$\beta$} (I);
\draw[arrow] (I) -- node[above]{$\gamma$} (R);

\end{tikzpicture}

\end{document}

這是我的程式碼產生的結果: 在此輸入影像描述

我無法將箭頭從“恢復”繪製到“易受影響”。如何繪製箭頭,使其越過方框而不與它們重疊?任何幫助將不勝感激。

答案1

這是我最初的天真的嘗試:

\node (A) [above=0.5cm of I] {$\delta$};
\draw[arrow] (R.north) |- (A.south) -| (S.north); 

用戶 Sandy G 給了一個更好的選擇,那就是使用一種非常難以研究的技術(事實證明谷歌搜尋--++很難);我發現這篇舊的(11 年)帖子談到了它:TiKz 衝刺 衝刺 加加 這引導我進入 TikZ 手冊部分13.4 相對座標和增量座標

我們可以在新路徑座標之前使用指定相對座標++,使其相對於路徑中位於其先前的座標。從手冊中:

您可以在座標前面添加 ++ 以使它們「相對」。諸如 ++(1cm,0pt) 之類的座標表示「前一個位置右側 1cm,使其成為新的當前位置」。相對座標在「本地」上下文中通常很有用:

這使得它變得簡單:

\draw[arrow] (R.north) -- ++(0,0.5) -| (S.north) node[above, pos=0.25] {$\delta$};

這將從節點 R 的北邊繪製一個節點,向上 0.5,然後連接到 S 的北邊,但需要急轉彎才能到達那裡(由 指定-|)。節點放置在路徑上方 0.25 處(恰好是對稱的)並用 填充$\delta$

微量元素

兩個答案都會給出大致相同的圖像,但我認為您會喜歡看到多個視角。

微量元素:

\documentclass[tikz, border=10pt]{standalone}
\usetikzlibrary{positioning, arrows.meta}

\begin{document}

\begin{tikzpicture}[
    node distance=2.5cm,
    arrow/.style={->, >=Stealth, thick},
    box/.style={draw, rectangle, rounded corners, minimum width=2cm, minimum height=1cm, align=center}
    ]

    % Nodes
    \node[box] (S) {Susceptible};
    \node[box, right=of S] (I) {Infectious};
    \node[box, right=of I] (R) {Recovered};

    % Arrows
    \draw[arrow] (S) -- node[above]{$\beta$} (I);
    \draw[arrow] (I) -- node[above]{$\gamma$} (R);

    % My Naive Attempt
    % \node (A) [above=0.5cm of I] {$\delta$};
    % \draw[arrow] (R.north) |- (A.south) -| (S.north);

    % Sandy G's code
    \draw[arrow] (R.north) -- ++(0,.5) -| (S.north) node[above, pos=0.25] {$\delta$};
        
\end{tikzpicture}

\end{document}

相關內容