答案1
我们将问题分为两部分:
- 放置节点并连接它们(无需森林或其他包)
- 在节点内插入图像
为了保持一切整洁,我们还创建一些style
s,这样我们就不需要一遍又一遍地重复自己。
样式figNode
负责在节点中心插入图片。此外,样式mySimpleArrow
定义箭头和样式myBlock
定义节点的最小尺寸,两者都有默认设置,因此您可以通过 定义样式的全局行为方式,或在特定节点内进行本地定义mySimpleArrow={yellow}{red}
。
第一个\begin{scope}
块放置我们所有的节点,第二个块定义节点之间的路径。
我给出了两个图。第一个图有标签,节点被移位right=<distance> of <node>
。它解决了我们的第一个问题。现在,为了解决第二个问题,我们只需添加figNode
样式。不幸的是,right=...
语法会干扰figNode
,所以我创建了第二个图,其中节点被放置在at (x,y)
坐标中。
最后,为了获得您的结果,只需将里面的内容替换\includegraphics
为您的数字即可。
\documentclass{article}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
\tikzset{figNode/.style={
path picture={
\node at (path picture bounding box.center) {#1};}}
}
\tikzset{mySimpleArrow/.style n args={2}{
>={latex[#1]},
every path/.style={draw=#2}
},
mySimpleArrow/.default={black}{black}
}
\tikzset{myBlock/.style n args={2}{
every node/.style={rectangle,draw, text=black,
minimum width=#1, minimum height=#2,}
},
myBlock/.default={1cm}{1cm}
}
\begin{document}
\begin{tikzpicture}[very thick]
\begin{scope}[shift={(3.0,-0.5)},myBlock]
\node [label=above:Road] (A) at (-6,6) {$A$};
\node[right=0.5 of A] (B) [label=above:Metal]{$B$};
\node[right=0.5 of B] (C) [label=above:Dirt]{$C$};
\node[right=0.5 of C] (D) [label=above:Roof]{$D$};
\node[right=0.5 of D] (E) [label=above:Grass 1]{$E$};
\node[right=0.5 of E] (F) [label=above:Grass 2]{$F$};
\node[right=0.5 of F] (G) [label=above:Trees]{$G$};
\node (H) at (-5,3.5) [label=below left:Road + Metal]{$H$};
\node (I) at (-3.5,2) [label=below left:Road + Metal + Dirt]{$I$};
\node[right=3 of I] (J) [label=below:Grass]{$J$};
\node (K) at (-2,0.5) [label=below:Non-vegetation]{$K$};
\node[right=3 of K] (L) [label=below:Vegetation]{$L$};
\end{scope}
\begin{scope}[mySimpleArrow]
\path[->] (K) -- (I);
\path[->] (K) -- (D);
\path[->] (I) -- (H);
\path[->] (I) -- (C);
\path[->] (H) -- (A);
\path[->] (H) -- (B);
\path[->] (L) -- (J);
\path[->] (L) -- (G);
\path[->] (J) -- (E);
\path[->] (J) -- (F);
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}[very thick]
\begin{scope}[shift={(3.0,-0.5)},myBlock]
% \draw [figNode={\includegraphics{example-image-a}},label=above:Road] coordinate(A) (-6,6) rectangle ++(1,1);
\node [label=above:Road,figNode={\includegraphics[width=1cm]{example-image-a}}] (A) at (-6,6) {};
\node[figNode={\includegraphics[width=1cm]{example-image-b}}](B) at (-4.5,6) [label=above:Metal]{};
\node[figNode={\includegraphics[width=1cm]{example-image-c}}] (C) at (-3,6) [label=above:Dirt]{};
\node[figNode={\includegraphics[width=1cm]{example-image-duck}}] (D) at (-1.5,6) [label=above:Roof]{};
\node[figNode={\includegraphics[width=1cm]{example-image-1x1}}] (E) at (0,6) [label=above:Grass 1]{};
\node[figNode={\includegraphics[width=1cm]{example-image-a}}] (F) at (1.5,6) [label=above:Grass 2]{};
\node[figNode={\includegraphics[width=1cm]{example-image-b}}] (G) at (3,6) [label=above:Trees]{};
\node[figNode={\includegraphics[width=1cm]{example-image-c}}] (H) at (-5,3.5) [label=below left:Road + Metal]{};
\node[figNode={\includegraphics[width=1cm]{example-image-duck}}] (I) at (-3.5,2) [label=below left:Road + Metal + Dirt]{};
\node[figNode={\includegraphics[width=1cm]{example-image-1x1}}] (J) at (0.5,2) [label=below:Grass]{};
\node[figNode={\includegraphics[width=1cm]{example-image-a}}] (K) at (-2,0.5) [label=below:Non-vegetation]{};
\node[figNode={\includegraphics[width=1cm]{example-image-b}}] (L) at (2,0.5) [label=below:Vegetation]{};
\end{scope}
\begin{scope}[mySimpleArrow]
\path[->] (K) -- (I);
\path[->] (K) -- (D);
\path[->] (I) -- (H);
\path[->] (I) -- (C);
\path[->] (H) -- (A);
\path[->] (H) -- (B);
\path[->] (L) -- (J);
\path[->] (L) -- (G);
\path[->] (J) -- (E);
\path[->] (J) -- (F);
\end{scope}
\end{tikzpicture}
\end{document}
第一个带有字母标记节点的图。
第二个图形与节点内的图形。