我有以下 tex 文档,其中有两张tikz
图片,它们发生冲突。
该文档的当前版本适用于单张tikz
图片,但当我尝试加载另一张tikz
图片时,我得到了奇怪的结果。
也就是说,运行下面的文档应该给出正确的输出(如下所示 - 以及另一个图)。
\iffalse
但是,如果我在第 46 行和第 162 行进行注释,\fi
我会得到不同的原始图片。
我想要的是(在同一个文档中同时包含两个图而不是只有这个图):
\iffalse
离开并\fi
注释掉部分内容后我得到了什么:
第一张图片tikz
没有受到影响,只有树tikz
受到了影响,我看不出这两张图片在哪里发生了冲突。
文档:
\documentclass[]{article}
%%%%%%% Packages to force the plots to work %%%%
\usepackage{tikz}
%\usetikzlibrary{arrows}
%\usetikzlibrary{shapes.geometric}
\usetikzlibrary{arrows.meta,arrows}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% for the supervised vs unsupervised plots %%%%%%%%%%
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.11}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% Random Forest %%%%%%%%%%%%%%%%
\usepackage{
forest,
% showframe
}
\usetikzlibrary{fit,positioning}
\tikzset{
font=\large\sffamily\bfseries,
red arrow/.style={
midway,red,sloped,fill, minimum height=3cm, single arrow, single arrow head extend=.5cm, single arrow head indent=.25cm,xscale=0.3,yscale=0.15,
allow upside down
},
black arrow/.style 2 args={-stealth, shorten >=#1, shorten <=#2},
black arrow/.default={1mm}{1mm},
tree box/.style={draw, rounded corners, inner sep=1em},
node box/.style={white, draw=black, text=black, rectangle, rounded corners},
}
%%%%%%%%%% END: Random Forest %%%%%%%%%%%%%%%%
\begin{document}
%%%%%%%%%% supservises/unspuervised learning %%%%%%%%%%%%
%%% NOTE: There is a clash between this section and the random forest section %%%%
\iffalse % add a comment around the block of text - chec the end \fi
\begin{figure}
\centering
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=2 by 1,
horizontal sep=1.5cm
},
axis lines = left,
xlabel = $x$,
ylabel = $y$,
width=5cm, % <-- set size of axes
clip mode=individual, % to avoid \draws being cut off
title style={yshift=1mm, font=\bfseries\sffamily}
]
\nextgroupplot[title=Supervised learning]
\addplot [blue, only marks, mark=*, mark size=3, draw = black, fill = blue] table [%
x = x,
y = y,
col sep = comma]{
x, y
%cluster 1
2, 3
3, 5
4, 5
3, 8
5, 9
3, 2
5, 6
6, 6
7, 9
10, 4
11, 5
9, 4
};
\addplot+[red, only marks, mark=*, mark size=3, draw = black, fill = blue] table [%
x = x,
y = y,
col sep = comma]{
x, y
20, 10
21, 12
24, 12
25, 13
27, 14
22, 13
23, 15
25, 10
15, 14
};
% to be able to use axis coordinates with \draw directly you need
% \pgfplotsset{compat=1.11} or a higher version
% if that is not present, use (axis cs:4,14) instead of (4,14),
% to specify that the values should be interpreted as axis coordinates
\draw [dashed] (4,14) -- (25,2);
\nextgroupplot[title=Unsupervised learning]
\addplot [blue, only marks, mark=*, mark size=3, draw = black, fill = blue] table [%
x = x,
y = y,
col sep = comma]{
x, y
%cluster 1
2, 3
3, 5
4, 5
3, 8
5, 9
3, 2
5, 6
6, 6
7, 9
10, 4
11, 5
9, 4
};
\addplot+[red, only marks, mark=*, mark size=3, draw = black, fill = blue] table [%
x = x,
y = y,
col sep = comma]{
x, y
20, 10
21, 12
24, 12
25, 13
27, 14
22, 13
23, 15
25, 10
15, 14
};
% save a coordinate for use later
\coordinate (c2) at (23,12);
% the blue circle is drawn inside the axis environment, and in axis coordinates
% hence it becomes an ellipse
\draw [blue, dashed] (6,6) circle[radius=5];
\end{groupplot}
% the red circle is drawn outside the axis, so actually looks like a circle,
% but the radius has no relation to the axis coordinates
\draw [red, dashed] (c2) circle[radius=1cm];
\end{tikzpicture}
\caption{Supervised, Unsupervised and Reinforcement Learning}
\label{fig:supervisedUnsupervisedReinforcementLearning}
\end{figure}
\fi
%%%%%%%%%%%%%%%%
\begin{center}
\begin{forest}
for tree={
l sep=2em,
s sep=2mm,
anchor=center,
inner sep=0,
minimum width=1em,
minimum height=0.5em,
fill=blue!50,
rectangle,
where level=2{no edge}{}}
[
Training Data, node box
[sample and feature bagging, node box, alias=bagging, above=4em,s sep=1.1cm
[,alias=a1[[,alias=a2][]][,edge label={node[above=1ex,red arrow]{}}[[][]]
[,edge label={node[above=1ex,red arrow]{}}[,red!70,edge label={node[below=1ex,red arrow]{}}][,alias=a3]]]]
[,alias=b1[,edge label={node[below=1ex,red arrow]{}}[[,alias=b2][]][,red!70,edge label={node[above=1ex,red arrow]{}}]][[][[][,alias=b3]]]]
[,phantom]
[,phantom]
[,alias=c1[[,alias=c2][]][,edge label={node[above=1ex,red arrow]{}}[,edge label={node[above=1ex,red arrow]{}}[,alias=c3][,red!70,edge label={node[above=1ex,red arrow]{}}]][,alias=c4]]]]
]
\node[tree box, fit=(a1)(a2)(a3)](t1){};
\node[tree box, fit=(b1)(b2)(b3)](t2){};
\node[tree box, fit=(c1)(c2)(c3)(c4)](tn){};
\begin{scope}[every node/.append style={below right=0.5em, inner sep=0pt, font=\normalsize\sffamily\bfseries}]
\node at (t1.north west) {Tree 1};
\node at (t2.north west) {Tree 2};
\node at (tn.north west) {Tree $n$};
\end{scope}
\path (t1.south west)--(tn.south east) node[midway,below=4em, node box] (mean) {mean in regression or majority vote in classification};
\node[below=3em of mean, node box] (pred) {prediction};
\draw[black arrow={5mm}{4mm}] (bagging) -- (t1.north);
\draw[black arrow] (bagging) -- (t2.north);
\draw[black arrow={5mm}{4mm}] (bagging) -- (tn.north);
\draw[black arrow={5mm}{5mm}] (t1.south) -- (mean);
\draw[black arrow] (t2.south) -- (mean);
\draw[black arrow={5mm}{5mm}] (tn.south) -- (mean);
\draw[black arrow] (mean) -- (pred);
\path (t2) -- node {\dots} (tn); % <-- new node
\end{forest}
\end{center}
\end{document}
答案1
我无法评论细节,但这似乎与 TikZ 中的节点名称是全局的事实有关(例如有没有办法忘记 TikZ 图片之间的节点名称?c2
)。第一个中有一个命名的坐标tikzpicture
,alias=c2
第二个中有一个节点,在fit
计算节点时显然存在一些混淆。
解决方法很简单,首先更改坐标名称tikzpicture
,例如
\coordinate (cc2) at (23,12);
...
\draw [red, dashed] (cc2) circle[radius=1cm];
\documentclass[]{article}
%%%%%%% Packages to force the plots to work %%%%
\usepackage{tikz}
%\usetikzlibrary{arrows}
%\usetikzlibrary{shapes.geometric}
\usetikzlibrary{arrows.meta,arrows}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% for the supervised vs unsupervised plots %%%%%%%%%%
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.11}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% Random Forest %%%%%%%%%%%%%%%%
\usepackage{
forest,
% showframe
}
\usetikzlibrary{fit,positioning}
\tikzset{
font=\large\sffamily\bfseries,
red arrow/.style={
midway,red,sloped,fill, minimum height=3cm, single arrow, single arrow head extend=.5cm, single arrow head indent=.25cm,xscale=0.3,yscale=0.15,
allow upside down
},
black arrow/.style 2 args={-stealth, shorten >=#1, shorten <=#2},
black arrow/.default={1mm}{1mm},
tree box/.style={draw, rounded corners, inner sep=1em},
node box/.style={white, draw=black, text=black, rectangle, rounded corners},
}
%%%%%%%%%% END: Random Forest %%%%%%%%%%%%%%%%
\begin{document}
%%%%%%%%%% supservises/unspuervised learning %%%%%%%%%%%%
%%% NOTE: There is a clash between this section and the random forest section %%%%
%\iffalse % add a comment around the block of text - chec the end \fi
\begin{figure}
\centering
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=2 by 1,
horizontal sep=1.5cm
},
axis lines = left,
xlabel = $x$,
ylabel = $y$,
width=5cm, % <-- set size of axes
clip mode=individual, % to avoid \draws being cut off
title style={yshift=1mm, font=\bfseries\sffamily}
]
\nextgroupplot[title=Supervised learning]
\addplot [blue, only marks, mark=*, mark size=3, draw = black, fill = blue] table [%
x = x,
y = y,
col sep = comma]{
x, y
%cluster 1
2, 3
3, 5
4, 5
3, 8
5, 9
3, 2
5, 6
6, 6
7, 9
10, 4
11, 5
9, 4
};
\addplot+[red, only marks, mark=*, mark size=3, draw = black, fill = blue] table [%
x = x,
y = y,
col sep = comma]{
x, y
20, 10
21, 12
24, 12
25, 13
27, 14
22, 13
23, 15
25, 10
15, 14
};
% to be able to use axis coordinates with \draw directly you need
% \pgfplotsset{compat=1.11} or a higher version
% if that is not present, use (axis cs:4,14) instead of (4,14),
% to specify that the values should be interpreted as axis coordinates
\draw [dashed] (4,14) -- (25,2);
\nextgroupplot[title=Unsupervised learning]
\addplot [blue, only marks, mark=*, mark size=3, draw = black, fill = blue] table [%
x = x,
y = y,
col sep = comma]{
x, y
%cluster 1
2, 3
3, 5
4, 5
3, 8
5, 9
3, 2
5, 6
6, 6
7, 9
10, 4
11, 5
9, 4
};
\addplot+[red, only marks, mark=*, mark size=3, draw = black, fill = blue] table [%
x = x,
y = y,
col sep = comma]{
x, y
20, 10
21, 12
24, 12
25, 13
27, 14
22, 13
23, 15
25, 10
15, 14
};
% save a coordinate for use later
\coordinate (cc2) at (23,12);
% the blue circle is drawn inside the axis environment, and in axis coordinates
% hence it becomes an ellipse
\draw [blue, dashed] (6,6) circle[radius=5];
\end{groupplot}
% the red circle is drawn outside the axis, so actually looks like a circle,
% but the radius has no relation to the axis coordinates
\draw [red, dashed] (cc2) circle[radius=1cm];
\end{tikzpicture}
\caption{Supervised, Unsupervised and Reinforcement Learning}
\label{fig:supervisedUnsupervisedReinforcementLearning}
\end{figure}
%\fi
%%%%%%%%%%%%%%%%
\begin{center}
\begin{forest}
for tree={
l sep=2em,
s sep=2mm,
anchor=center,
inner sep=0,
minimum width=1em,
minimum height=0.5em,
fill=blue!50,
rectangle,
where level=2{no edge}{}}
[
Training Data, node box
[sample and feature bagging, node box, alias=bagging, above=4em,s sep=1.1cm
[,alias=a1[[,alias=a2][]][,edge label={node[above=1ex,red arrow]{}}[[][]]
[,edge label={node[above=1ex,red arrow]{}}[,red!70,edge label={node[below=1ex,red arrow]{}}][,alias=a3]]]]
[,alias=b1[,edge label={node[below=1ex,red arrow]{}}[[,alias=b2][]][,red!70,edge label={node[above=1ex,red arrow]{}}]][[][[][,alias=b3]]]]
[,phantom]
[,phantom]
[,alias=c1[[,alias=c2][]][,edge label={node[above=1ex,red arrow]{}}[,edge label={node[above=1ex,red arrow]{}}[,alias=c3][,red!70,edge label={node[above=1ex,red arrow]{}}]][,alias=c4]]]]
]
\node[tree box, fit=(a1)(a2)(a3)](t1){};
\node[tree box, fit=(b1)(b2)(b3)](t2){};
\node[tree box, fit=(c1)(c2)(c3)(c4)](tn){};
\begin{scope}[every node/.append style={below right=0.5em, inner sep=0pt, font=\normalsize\sffamily\bfseries}]
\node at (t1.north west) {Tree 1};
\node at (t2.north west) {Tree 2};
\node at (tn.north west) {Tree $n$};
\end{scope}
\path (t1.south west)--(tn.south east) node[midway,below=4em, node box] (mean) {mean in regression or majority vote in classification};
\node[below=3em of mean, node box] (pred) {prediction};
\draw[black arrow={5mm}{4mm}] (bagging) -- (t1.north);
\draw[black arrow] (bagging) -- (t2.north);
\draw[black arrow={5mm}{4mm}] (bagging) -- (tn.north);
\draw[black arrow={5mm}{5mm}] (t1.south) -- (mean);
\draw[black arrow] (t2.south) -- (mean);
\draw[black arrow={5mm}{5mm}] (tn.south) -- (mean);
\draw[black arrow] (mean) -- (pred);
\path (t2) -- node {\dots} (tn); % <-- new node
\end{forest}
\end{center}
\end{document}