增强(或其他)

增强(或其他)

我正在尝试根据排队理论绘制出生和死亡过程图,并在图表的顶部和底部添加文本。我正在使用 TikzEdt 软件。当我编译代码时,我收到一条错误消息:

'Couldn't parse code. MismatchedTokenException: Expected token '}'. Instead found "1." which is of type FLOAT_WO_EXP in line 17 at position 125.
Couldn't parse code. MismatchedTokenException: Expected token '}'. Instead found "1." which is of type FLOAT_WO_EXP in line 17 at position 125.
Compilation failed with exit code 1''

我还发现在弧线底部书写很困难。我有以下代码:

\begin{tikzpicture}

 \draw  (-4.5,4) rectangle (10,-3);

[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
 \clip(-2.84,-1.3) rectangle (9.04,3.58);
 \draw(0.,1.) circle (0.5003998401278722cm);
 \draw(2.,1.) circle (0.46173585522460786cm);
 \draw(4.,1.) circle (0.44045431091090476cm);
 \draw(6.,1.) circle (0.44181444068749043cm);
 \draw(8.,1.) circle (0.44045431091090476cm);
 \draw(-2.,1.) circle (0.523450093132096cm);
 \draw (-2.08,1.14) node[anchor=north west] {0};
 \draw (-0.08,1.16) node[anchor=north west] {1};
 \draw (1.92,1.2) node[anchor=north west] {2};
 \draw (3.76,1.16) node[anchor=north west] {n-1};
 \draw (5.92,1.2) node[anchor=north west] {n};
 \draw (7.5,1.16) node[anchor=north west] {n+1};
 \draw [shift={(-0.9815173101890807,1.5112413590134577)}] plot[domain=-0.011986604704742554:3.1296060488850506,variable=\t]({1.*1.018555861321933*cos(\t r)+0.*1.018555861321933*sin(\t r)},{0.*1.018555861321933*cos(\t r)+1.*1.018555861321933*sin(\t r)});
 \draw [shift={(1.0368933040163526,1.4796489900153262)}] plot[domain=-0.019382604449061702:3.1222100491407314,variable=\t]({1.*1.000115782939687*cos(\t r)+0.*1.000115782939687*sin(\t r)},{0.*1.000115782939687*cos(\t r)+1.*1.000115782939687*sin(\t r)});
 \draw [shift={(3.0184106142054334,1.4503598330233691)},line width=2.pt,dash pattern=on 2pt off 2pt]  plot[domain=-0.010090966788127709:3.131501686801665,variable=\t]({1.*0.9816393643659953*cos(\t r)+0.*0.9816393643659953*sin(\t r)},{0.*0.9816393643659953*cos(\t r)+1.*0.9816393643659953*sin(\t r)});
 \draw [shift={(4.990803512086236,1.4409428653857879)}] plot[domain=4.930891234521944E-4:3.1420857427132454,variable=\t]({1.*0.9908036325366876*cos(\t r)+0.*0.9908036325366876*sin(\t r)},{0.*0.9908036325366876*cos(\t r)+1.*0.9908036325366876*sin(\t r)});
 \draw [shift={(6.999605559480942,1.4407668947979961)}] plot[domain=-6.527759571728353E-4:3.1409398776326203,variable=\t]({1.*1.0179987522014697*cos(\t r)+0.*1.0179987522014697*sin(\t r)},{0.*1.0179987522014697*cos(\t r)+1.*1.0179987522014697*sin(\t r)});
 \draw [shift={(6.980403063500899,0.559301340676507)}] plot[domain=-3.1409328928241607:6.597607656324556E-4,variable=\t]({1.*1.0004650415559373*cos(\t r)+0.*1.0004650415559373*sin(\t r)},{0.*1.0004650415559373*cos(\t r)+1.*1.0004650415559373*sin(\t r)});
 \draw [shift={(4.98996911984415,0.5590934811158415)}] plot[domain=3.1411358636427766:6.28272851723257,variable=\t]({1.*0.9899692231261799*cos(\t r)+0.*0.9899692231261799*sin(\t r)},{0.*0.9899692231261799*cos(\t r)+1.*0.9899692231261799*sin(\t r)});
 \draw [shift={(2.990388842416927,0.5491050625508038)},line width=2.pt,dash pattern=on 2pt off 2pt]  plot[domain=-3.131251786904409:0.010340866685384142,variable=\t]({1.*1.0096651406276953*cos(\t r)+0.*1.0096651406276953*sin(\t r)},{0.*1.0096651406276953*cos(\t r)+1.*1.0096651406276953*sin(\t r)});
 \draw [shift={(0.9903888424169273,0.5191322979423201)}] plot[domain=-3.1218735235713657:0.0197191300184274,variable=\t]({1.*0.9905814270424891*cos(\t r)+0.*0.9905814270424891*sin(\t r)},{0.*0.9905814270424891*cos(\t r)+1.*0.9905814270424891*sin(\t r)});
 \draw [shift={(-1.,0.4880750333700159)}] plot[domain=-3.1300680373355956:0.01152461625419754,variable=\t]({1.*1.0000664120651637*cos(\t r)+0.*1.0000664120651637*sin(\t r)},{0.*1.0000664120651637*cos(\t r)+1.*1.0000664120651637*sin(\t r)});
 \draw (-1.24,3) node[anchor=north west] {$\lambda_0$};
 \draw (0.92,3.) node[anchor=north west] {$\lambda_1$};
 \draw (4.74,3.) node[anchor=north west] {$\lambda_{n-1}$};
 \draw (6.84,3.02) node[anchor=north west] {$\lambda_n$};
 \draw (-1.24,-3.5) node[anchor=north west] {$\mu_0$};
  \draw (0.92,-6) node[anchor=north west] {$\2mu$};
  \draw (4.74,-3.) node[anchor=north west] {$\3mu$};
 \draw (6.84,-3.02) node[anchor=north west] {$\4mu$};
\draw (-2.,0.476549906867904)-- (-2.28,0.14);
 \draw (-2.,0.476549906867904)-- (-1.64,0.26);
 \draw (0.,0.4996001598721278)-- (-0.28,0.26);
 \draw (0.,0.4996001598721278)-- (0.28,0.26);
 \draw (1.9807776848338545,0.5386644360125123)-- (1.64,0.28);
 \draw (1.9807776848338545,0.5386644360125123)-- (2.26,0.34);
 \draw (4.,0.5595456890890953)-- (4.3,0.32);
 \draw (4.,0.5595456890890953)-- (3.7,0.36);
 \draw (5.9799382396883,0.5586412731425877)-- (5.66,0.34);
 \draw (5.9799382396883,0.5586412731425877)-- (6.24,0.3);
 \draw (0.03696537962184012,1.4990326248949253)-- (-0.2,1.82);
 \draw (0.03696537962184012,1.4990326248949253)-- (0.42,1.8);
 \draw (2.0369572896787735,1.469470391582537)-- (1.78,1.72);
 \draw (2.0369572896787735,1.469470391582537)-- (2.4,1.72);
 \draw (4.0000498342454085,1.4508921413779512)-- (3.74,1.76);
 \draw (4.0000498342454085,1.4508921413779512)-- (4.42,1.76);
 \draw (5.974507837021575,1.5593389774478226)-- (5.76,1.82);
 \draw (5.974507837021575,1.5593389774478226)-- (6.32,1.74);
 \draw (8.012632698570675,1.5412530876377109)-- (7.78,1.74);
 \draw (8.012632698570675,1.5412530876377109)-- (8.36,1.74);
 \begin{scriptsize}
 \draw[color=black] (3.82,1.14) ;
 \draw[color=black] (-0.02,0.3) ;
 \draw[color=black] (4.,0.36) ;
 \draw[color=black] (6.38,1.6) ;
 \draw[color=black] (8.2,2.12) ;
 \end{scriptsize}
\end{tikzpicture}

有人应该帮我解释一下图表底部的值\mu2\mu3\mu是如何显示的。非常感谢4\mu

答案1

这是你想要的?

birth death diagram

我在截图时不小心把右边缘剪掉了。

如果是的话,您可以使用以下代码获取它:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}


\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
 \draw  (-4.5,4) rectangle (10,-3);
 \clip(-2.84,-1.3) rectangle (9.04,3.58);
 \node (zero) [draw, circle, minimum size=1cm] at (-2,1cm) {$0$};
 \node (one) [draw, circle, minimum size=1cm] at (0,1cm) {$1$};
 \node (two) [draw, circle, minimum size=1cm] at (2,1cm) {$2$};
 \node (enminusone) [draw, circle, minimum size=1cm] at (4,1cm) {$n-1$};
 \node (en) [draw, circle, minimum size=1cm] at (6,1cm) {$n$};
 \node (enplusone) [draw, circle, minimum size=1cm] at (8,1cm) {$n+1$};

 \draw [->] (zero) .. controls +(0,2) and +(0,2) .. node [midway, above] {$\lambda_0$} (one);
 \draw [->] (one) .. controls +(0,2) and +(0,2) .. node [midway, above] {$\lambda_1$} (two);
 \draw [->, very thick, dashed] (two) .. controls +(0,2) and +(0,2) .. (enminusone);
 \draw [->] (enminusone) .. controls +(0,2) and +(0,2) .. node [midway, above] {$\lambda_{n-1}$} (en);
 \draw [->] (en) .. controls +(0,2) and +(0,2) .. node [midway, above] {$\lambda_n$} (enplusone);

 \draw [->] (enplusone) .. controls +(0,-2) and +(0,-2) .. node [midway, below] {$\mu_4$} (en);
 \draw [->] (en) .. controls +(0,-2) and +(0,-2) .. node [midway, below] {$\mu_3$} (enminusone);
 \draw [->, very thick, dashed] (enminusone) .. controls +(0,-2) and +(0,-2) .. (two);
 \draw [->] (two) .. controls +(0,-2) and +(0,-2) .. node [midway, below] {$\mu_2$} (one);
 \draw [->] (one) .. controls +(0,-2) and +(0,-2) .. node [midway, below] {$\mu_0$} (zero);

\end{tikzpicture}
\end{document}

请注意,我如何通过使每个圆圈都成为 来绘制、命名和打印每个圆圈内的文本node,这允许轻松地在每个节点之间创建引用每个圆圈名称的箭头。

可以通过将箭头稍微分开来改善视觉效果,这样可以更清楚地了解哪条线指向哪里。请参阅以下示例:

improved birth death diagram

更改出现在下面的代码中。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}


\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
 \draw  (-4.5,4) rectangle (10,-3);
 \clip(-2.84,-1.3) rectangle (9.04,3.58);
 \node (zero) [draw, circle, minimum size=1cm] at (-2,1cm) {$0$};
 \node (one) [draw, circle, minimum size=1cm] at (0,1cm) {$1$};
 \node (two) [draw, circle, minimum size=1cm] at (2,1cm) {$2$};
 \node (enminusone) [draw, circle, minimum size=1cm] at (4,1cm) {$n-1$};
 \node (en) [draw, circle, minimum size=1cm] at (6,1cm) {$n$};
 \node (enplusone) [draw, circle, minimum size=1cm] at (8,1cm) {$n+1$};

 \draw [->] (zero) .. controls +(0.5,1.5) and +(-0.5,1.5) .. node [midway, above] {$\lambda_0$} (one);
 \draw [->] (one) .. controls +(0.5,1.5) and +(-0.5,1.5) .. node [midway, above] {$\lambda_1$} (two);
 \draw [->, very thick, dashed] (two) .. controls +(0.5,1.5) and +(-0.5,1.5) .. (enminusone);
 \draw [->] (enminusone) .. controls +(0.5,1.5) and +(-0.5,1.5) .. node [midway, above] {$\lambda_{n-1}$} (en);
 \draw [->] (en) .. controls +(0.5,1.5) and +(-0.5,1.5) .. node [midway, above] {$\lambda_n$} (enplusone);

 \draw [->] (enplusone) .. controls +(-0.5,-1.5) and +(0.5,-1.5) .. node [midway, below] {$\mu_4$} (en);
 \draw [->] (en) .. controls +(-0.5,-1.5) and +(0.5,-1.5) .. node [midway, below] {$\mu_3$} (enminusone);
 \draw [->, very thick, dashed] (enminusone) .. controls +(-0.5,-1.5) and +(0.5,-1.5) .. (two);
 \draw [->] (two) .. controls +(-0.5,-1.5) and +(0.5,-1.5) .. node [midway, below] {$\mu_2$} (one);
 \draw [->] (one) .. controls +(-0.5,-1.5) and +(0.5,-1.5) .. node [midway, below] {$\mu_0$} (zero);

\end{tikzpicture}
\end{document}

答案2

我认为您的代码过于复杂。除非这些位置带有小数是有原因的,否则根据您需要用此图做什么,您可以使用\foreach和一些if语句来执行此操作。

输出

figure 1

代码

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}

\usetikzlibrary{arrows.meta}

\tikzset{
    circ/.style={draw, circle,inner sep=0pt,minimum size=8mm, font=\scriptsize},
    triangle/.tip={Computer Modern Rightarrow[open,angle=120:3pt]}
}

\begin{document}
\begin{tikzpicture}[>=triangle]

\foreach \text [count=\xi starting from 0, remember=\text as \lastt (initially 0)] in {0,1,2,n-1,n,n+1}{
    \node[circ] (\text) at (\xi,0) {\text};
    \ifnum\xi=3
        \draw[line width=.5mm, dashed] (2.north) arc (180:0:.5);
        \draw[line width=.5mm, dashed] (2.south) arc (180:360:.5);
        \draw[->] (\lastt.north) arc (180:0:.5);
        \draw[<-] (\lastt.south) arc (180:360:.5);
    \else
        \draw[->] (\lastt.north) arc (180:0:.5) node[above, font=\tiny, midway] {$\lambda_\lastt$};
    \ifnum\xi=2
        \draw[<-] (\lastt.south) arc (180:360:.5) node[below, font=\tiny, midway] {$\xi_\mu$};
    \else
    \ifnum\xi=0
        \draw[<-] (\lastt.south) arc (180:360:.5) node[below, font=\tiny, midway] {$\mu_\lastt$};
    \else
    \ifnum\xi>3       
        \pgfmathtruncatemacro\xy{\xi-1}
        \draw[<-] (\lastt.south) arc (180:360:.5) node[below, font=\tiny, midway] {$\xy_\mu$};
    \else
    \fi
    \fi
    \fi
    \fi
}

\end{tikzpicture}
\end{document}

答案3

这是一个使用的解决方案

  • chains用于放置和连接节点的库;
  • quotes用于创建箭头上方和下方标签的库 ;
  • arrows.meta和一些更漂亮的箭头的库bending

这样可以更简洁地指定代码。\foreach使用单个循环可以避免重复向链中添加节点。循环中使用了 4 个变量:

  • 第一个( \i)指定主循环节点的内容;
  • 第二个(\j)指定箭头的任何特殊属性(例如dashed),但大多数情况下为空;
  • 第三个(\k)指定位于顶部箭头上方的标签;
  • 第四个(\l)标签位于底部箭头下方。

every on chain用于设置主要节点的样式chainmy join保存我们想要用于节点上方和下方箭头的样式属性。

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{chains,arrows.meta,quotes,bending}
\begin{document}
  \begin{tikzpicture}
    [
      every on chain/.style={circle, draw, minimum size=30pt},
      my join/.style={looseness=1.75, font=\scriptsize, >={Stealth[bend]}},
      start chain=main going right,
      node distance=.5,
    ]
    \foreach \i/\j/\k/\l in {0, 1//{$\lambda_0$}/{$\mu_0$}, 2//{$\lambda_1$}/{$\mu_2$}, n-1/dashed//, n//{$\lambda_{n-1}$}/{$\mu_3$}, n+1//{$\lambda_n$}/{$\mu_4$}}
    \node [on chain, join=by {\j, <-, out=-86.5, in=-93.5, my join, "\l"'}, join=by {"\k", \j, ->, out=86.5, in=93.5, my join}] {\i};
  \end{tikzpicture}
\end{document}

可以通过从仅指定每个标签的唯一内容的参数创建标签内容来进一步实现自动化,但在我看来,这只会降低可读性而收效甚微。(在这种情况下,它不会节省太多的打字时间。)

chained nodes in a loop

增强(或其他)

如果您有颜色选择并且符合您的品味,这里有一个增强版。三种颜色在图片之外定义,因此可以根据需要轻松定制。

colours

\documentclass[tikz,border=10pt]{standalone}
\usepackage{kpfonts}
\usetikzlibrary{chains,arrows.meta,quotes,bending,shadows}
\begin{document}
  \colorlet{nodecol}{blue!75!green!75!black}
  \colorlet{goingrightcol}{green!75!black}
  \colorlet{goingleftcol}{red!75!black}
  \begin{tikzpicture}
    [
      every on chain/.style={circle, draw, nodecol, thick, minimum size=30pt, text height=1ex, inner color=white, outer color=nodecol!10, circular drop shadow},
      my join/.style={looseness=1.75, semithick, font=\footnotesize, >={Stealth[bend]}},
      start chain=main going right,
      node distance=.5,
    ]
    \foreach \i/\j/\k/\l in {0, 1//{$\lambda_0$}/{$\mu_0$}, 2//{$\lambda_1$}/{$\mu_2$}, n-1/dashed//, n//{$\lambda_{n-1}$}/{$\mu_3$}, n+1//{$\lambda_n$}/{$\mu_4$}}
    \node [on chain, join=by {\j, <-, goingleftcol, out=-86.5, in=-93.5, my join, "\l"'}, join=by {"\k", goingrightcol, \j, ->, out=86.5, in=93.5, my join}] {\i};
  \end{tikzpicture}
\end{document}

相关内容