我希望复制这篇文章中的图像。但是除了我的代码执行的基本操作外,我无法执行任何其他操作。感谢您的帮助。
具体的颜色并不重要。另外,我实际上并不想要那些围绕着颜色的黑线。也许在浅蓝色周围有一条深蓝色的线,在浅绿色周围有一条深绿色的线。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,fit,backgrounds,calc}
\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [%
matrix of nodes,
minimum size=1cm,
column sep=3.5cm,
row sep=0.6cm,
]
{
& |(top-left)| & |(top-right)| & $ $ \\
& & $_0S_{2\Delta t}$ & \\
& $_0S_{\Delta t}$ & & $ $ \\
$_0S_0$ & & $_1S_{2\Delta t}$ & \\
& $_1S_{\Delta t}$ & & $ $ \\
& & $_2S_{2\Delta t}$ & \\
& |(bottom-left)| & |(bottom-right)| & $ $ \\
};
\draw[->] (tree-4-1) -- (tree-3-2) node [midway,above] {$p$};
\draw[->] (tree-4-1) -- (tree-5-2) node [midway,below] {$1-p$};
\draw[->] (tree-3-2) -- (tree-2-3) node [midway,above](a) {};
\draw[->] (tree-3-2) -- (tree-4-3) node [midway,below] {};
\draw[->] (tree-5-2) -- (tree-4-3) node [midway,above] {};
\draw[->] (tree-5-2) -- (tree-6-3) node [midway,below] {};
\draw[->,dashed,shorten >= 17mm] (tree-2-3) -- (tree-1-4) node [midway,above] {};
\draw[->,dashed,shorten >= 17mm] (tree-2-3) -- (tree-3-4) node [midway,below] {};
\draw[->,dashed,shorten >= 17mm] (tree-4-3) -- (tree-3-4) node [midway,above] {};
\draw[->,dashed,shorten >= 17mm] (tree-4-3) -- (tree-5-4) node [midway,below] {};
\draw[->,dashed,shorten >= 17mm] (tree-6-3) -- (tree-5-4) node [midway,above] {};
\draw[->,dashed,shorten >= 17mm] (tree-6-3) -- (tree-7-4) node [midway,below] {};
% t-line
\draw[dashed] ($(top-left.north)!.5!(top-right.north)$) -- ($(bottom-left.south)!.5!(bottom-right.south)$);
\node at ($(bottom-left.south)!.5!(bottom-right.south)-(0,1em)$) {$\tau$};
\end{tikzpicture}
\end{document}
答案1
实际上,您已经加载了所有正确的库:
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{matrix,fit,backgrounds,calc}
\pgfdeclarelayer{background layer}
\pgfsetlayers{background layer,main}
\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree)
[ matrix of nodes,
minimum size=1cm,
column sep=3.5cm,
row sep=0.6cm,
]
{ & |(top-left)| & |(top-right)| & $ $ \\
& & $_0S_{2\Delta t}$ & \\
& $_0S_{\Delta t}$ & & $ $ \\
$_0S_0$ & & $_1S_{2\Delta t}$ & \\
& $_1S_{\Delta t}$ & & $ $ \\
& & $_2S_{2\Delta t}$ & \\
& |(bottom-left)| & |(bottom-right)| & $ $ \\
};
\draw[->] (tree-4-1) -- (tree-3-2) node [midway,above] {$p$};
\draw[->] (tree-4-1) -- (tree-5-2) node [midway,below] {$1-p$};
\draw[->] (tree-3-2) -- (tree-2-3) node [midway,above](a) {};
\draw[->] (tree-3-2) -- (tree-4-3) node [midway,below] {};
\draw[->] (tree-5-2) -- (tree-4-3) node [midway,above] {};
\draw[->] (tree-5-2) -- (tree-6-3) node [midway,below] {};
\draw[->,dashed,shorten >= 17mm] (tree-2-3) -- (tree-1-4) node [midway,above] {};
\draw[->,dashed,shorten >= 17mm] (tree-2-3) -- (tree-3-4) node [midway,below] {};
\draw[->,dashed,shorten >= 17mm] (tree-4-3) -- (tree-3-4) node [midway,above] {};
\draw[->,dashed,shorten >= 17mm] (tree-4-3) -- (tree-5-4) node [midway,below] {};
\draw[->,dashed,shorten >= 17mm] (tree-6-3) -- (tree-5-4) node [midway,above] {};
\draw[->,dashed,shorten >= 17mm] (tree-6-3) -- (tree-7-4) node [midway,below] {};
% t-line
\draw[dashed] ($(top-left.north)!.5!(top-right.north)$) -- ($(bottom-left.south)!.5!(bottom-right.south)$);
% color backgrounds
\node at ($(bottom-left.south)!.5!(bottom-right.south)-(0,1em)$) {$\tau$};
\begin{pgfonlayer}{background layer}
\node[fit=(tree-2-3)(tree-4-3), fill=blue!50!cyan, rounded corners=0.2cm, inner sep=0, opacity=0.5] {};
\node[fit=(tree-4-3)(tree-6-3), fill=green!50!lime, rounded corners=0.2cm, inner sep=0, opacity=0.5] {};
\node[fit=(tree-3-2), fill=blue!50!cyan, rounded corners=0.2cm, inner sep=0, opacity=0.5] {};
\node[fit=(tree-5-2), fill=green!50!lime, rounded corners=0.2cm, inner sep=0, opacity=0.5] {};
\end{pgfonlayer}
\draw[->,blue!50!cyan, shorten >=5mm, shorten <=6mm] ($(tree-2-3)+(0,0.1)$) -- ($(tree-3-2)+(0,0.1)$) node[pos=0.3,above,sloped] {x=k};
\node[blue!50!cyan] at ($0.33*(tree-3-2)+0.33*(tree-2-3)+0.33*(tree-4-3)$) {Step 1};
\node[green!50!lime] at ($0.33*(tree-5-2)+0.33*(tree-4-3)+0.33*(tree-6-3)$) {Step 2};
\end{tikzpicture}
\end{document}
结果
答案2
另一种着色方法:
\documentclass[a4paper,11pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix,fit,backgrounds,calc}
\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree)
[ matrix of nodes,
minimum size=1cm,
column sep=3.5cm,
row sep=0.6cm,
]
{ & |(top-left)| & |(top-right)| & $ $ \\
& & $_0S_{2\Delta t}$ & \\
& $_0S_{\Delta t}$ & & $ $ \\
$_0S_0$ & & $_1S_{2\Delta t}$ & \\
& $_1S_{\Delta t}$ & & $ $ \\
& & $_2S_{2\Delta t}$ & \\
& |(bottom-left)| & |(bottom-right)| & $ $ \\
};
\draw[->] (tree-4-1) -- (tree-3-2) node [midway,above] {$p$};
\draw[->] (tree-4-1) -- (tree-5-2) node [midway,below] {$1-p$};
\draw[->] (tree-3-2) -- (tree-2-3) node [midway,above](a) {};
\draw[->] (tree-3-2) -- (tree-4-3) node [midway,below] {};
\draw[->] (tree-5-2) -- (tree-4-3) node [midway,above] {};
\draw[->] (tree-5-2) -- (tree-6-3) node [midway,below] {};
\draw[->,dashed,shorten >= 17mm] (tree-2-3) -- (tree-1-4) node [midway,above] {};
\draw[->,dashed,shorten >= 17mm] (tree-2-3) -- (tree-3-4) node [midway,below] {};
\draw[->,dashed,shorten >= 17mm] (tree-4-3) -- (tree-3-4) node [midway,above] {};
\draw[->,dashed,shorten >= 17mm] (tree-4-3) -- (tree-5-4) node [midway,below] {};
\draw[->,dashed,shorten >= 17mm] (tree-6-3) -- (tree-5-4) node [midway,above] {};
\draw[->,dashed,shorten >= 17mm] (tree-6-3) -- (tree-7-4) node [midway,below] {};
% t-line
\draw[dashed] ($(top-left.north)!.5!(top-right.north)$) -- ($(bottom-left.south)!.5!(bottom-right.south)$);
\node at ($(bottom-left.south)!.5!(bottom-right.south)-(0,1em)$) {$\tau$};
% background
\begin{pgfonlayer}{background}
\draw[rounded corners,draw=none,top color=blue!40!cyan!60, bottom color=green!60!lime!50](tree-2-3.north west)rectangle(tree-6-3.south east);
\draw[rounded corners,draw=none,fill=blue!40!cyan!60](tree-3-2.north west)rectangle(tree-3-2.south east);
\draw[rounded corners,draw=none,fill=green!60!lime!50](tree-5-2.north west)rectangle(tree-5-2.south east);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
结果: