2

I have the following tex document where I have two tikz pictures and they clash.

The current version of the document works for a single tikz picture but when I try to load in another tikz picture I get a weird result.

That is, running the document below should give the correct output (as below - along with another plot).

However if I comment out \iffalse on line 46 and then \fi on line 162 I obtain a different original picture.

What I want (along with having both plots in the same document and not just this single plot):

enter image description here

What I get after leaving \iffalse and \fi section commented out:

enter image description here

The first tikz is unaffected and its just the tree tikz that is affected and I cannot seem where the two pictures clash.

Document:

\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]]]] ] \nodetree box, fit=(a1)(a2)(a3){}; \nodetree box, fit=(b1)(b2)(b3){}; \nodetree box, fit=(c1)(c2)(c3)(c4){}; \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 Answers1

3

I can't comment on details, but it seems to be related to the fact that node names in TikZ are global (see e.g. Is there a way to forget node names between TikZ pictures?). There is a named coordinate c2 in the first tikzpicture, and a node with alias=c2 in the second, and when the fit node is computed there is some confusion apparently.

So easy fix, change the coordinate name in the first tikzpicture, for example

\coordinate (cc2) at (23,12);

...

\draw [red, dashed] (cc2) circle[radius=1cm];

enter image description here

\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]]]] ] \nodetree box, fit=(a1)(a2)(a3){}; \nodetree box, fit=(b1)(b2)(b3){}; \nodetree box, fit=(c1)(c2)(c3)(c4){}; \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}

Torbjørn T.
  • 206,688