Question
Recombining tree solution with pgfplots (not pure TikZ).
Context
There are multiple posts on trees (here, here, here and some more) and solutions are declined in multiple ways (matrix of nodes, trees, etc).
However I don't see solutions with pgfplots.
In MWE trying to adapt here,I am stuck with the drawing of arrows in 2 ways.
- first set of arrows going from left to right
- second set of arrows going from right to left
\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\begin{document}
\def\mallevel{5} % a number of levels we wish to get
\tikzset{
inner sep=0pt, outer sep=2pt, % some node reserve
malarrow/.style={->, shorten >=0pt, shorten <=-2pt, -{Stealth[length=5pt, width=3pt, inset=1pt]},
}, % a style for arrows, -2 is a shift back (an experiment)
malnode/.style={text=white,draw=blue!50, minimum width=5mm, circle, inner sep=1pt,font=\tiny,fill=blue,opacity=0.5,text opacity=1}, % a style for nodes
}
\begin{tikzpicture}
\begin{axis}[
xmin = 0,
xmax = \mallevel,
ymin = -\mallevel,
ymax = \mallevel,
xlabel = $x$,
yticklabel=\empty,
y axis line style={draw=none},
clip=false,
]
\foreach \x in {0,...,\mallevel}
{
\foreach \y in {0,...,\x}
{
\pgfmathparse{-\x/2+\y} % move up by a half of a tree (vertical direction)
\let\movey=\pgfmathresult
\edef\temp{\noexpand
\node[malnode] (\x-\y) at (axis cs:\x,\movey) {{\x}-{\y}};
}
\temp
%%%%%%%% draw the arrows %%%%%%%%
\ifnum\x>0
\pgfmathparse{int(\x-1)}
\let\previousx=\pgfmathresult % previous level (horizontal direction)
\ifnum\y>0
\pgfmathparse{int(\y-1)}
\let\previousy=\pgfmathresult % previous level (vertical direction)
% \node[malnode] (\previousx-\previousy) at (axis cs:\previousx,\previousy) {check};
% \draw[malarrow] (\previousx-\previousy) -- (\x-\y);
\fi
\fi
}
}
\end{axis}
\end{tikzpicture}
\end{document}


pgfplots. I have to say I don't see why you would want to usepgfplotsfor this in the first place, it's not what it's intended for. – Torbjørn T. May 21 '20 at 14:42pgfplotsovertikzhere ? By the way, your question is a bit confusing since bothpgfplotsandtikzlayers come on top ofpgf(they both use thepgfmacros to actually plot, but provide a user-friendly interface) – BambOo May 21 '20 at 14:45pgfplotswould work better than with TikZ. Both integrate quite seamlesly – BambOo May 21 '20 at 14:54