This is a follow up question to: PGFPLOTS: Drawings inside a bar of a barplot
I created a stack bar plot with coordinates at the bars such that I can draw arrows and other stuff inside the bar arranged at this coordinates. The following solution works fine as long as I only have one row:
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\pgfplotsset{
compat=1.16,
% define a style to use for the connecting lines
/tikz/my line style/.style={
black,
thick,
->,
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xbar stacked,
width=8cm,
height=2cm,
symbolic y coords={naive},
ytick=data,
yticklabels={Naive},
xlabel={Runtime [s]},
legend columns=3,
legend style={
at={(0.5,1.05)},
anchor=south,
legend cell align=left,
align=left,
legend plot pos=left,
draw=black,
font=\footnotesize,
},
% !!! this is the critical point !!!
% to make that solution work you have set the following option due to
% technical reasons which is roughly:
% normally the markers are first collected and are drawn *after*
% `\end{axis}', in contrast to the `\draw' command. Said that, the
% named `nodes near coords' are not available during the execution of
% the `\draw' command
clip marker paths=true,
]
% draw the connecting lines using the predefined style
\draw [my line style] (b-1-0) edge (a-0-1)
edge (a-1-1)
;
\addplot+[green] coordinates {(100,naive)}
coordinate (a-\plotnum-\coordindex)
;
\addlegendentry{Pre computations}
\addplot+[
yellow,
nodes near coords,
node near coords style={
black,
font=\small\bfseries,
% also add names to the `nodes near coords`
name=b-\plotnum-\coordindex,
},
] coordinates {(900,naive)}
% add coordinates to the points/bars
coordinate (a-\plotnum-\coordindex)
;
\addlegendentry{Iteration}
\addplot+[red] coordinates {(150,naive)};
\addlegendentry{Post computations}
\end{axis}
\end{tikzpicture}
\end{document}
Now I want to add more rows. Therefore, I added more symbolic y coordinates:
symbolic y coords={naive, opt1, opt2}
to the axis options and then tried:
\addplot+[green] coordinates {
(100,naive) (120,opt1) (125,opt2)
}
coordinate (a-\plotnum-\coordindex)
;
\addlegendentry{Pre computations}
\addplot+[
yellow,
nodes near coords,
node near coords style={
black,
font=\small\bfseries,
% also add names to the `nodes near coords`
name=b-\plotnum-\coordindex,
},
] coordinates {(900,naive)(800,opt1) (725,opt2)}
% add coordinates to the points/bars
coordinate (a-\plotnum-\coordindex)
;
\addlegendentry{Iteration}
\addplot+[red] coordinates {(150,naive)(120,opt1) (90,opt2)};
\addlegendentry{Post computations}
This fails with the error that my (a-0-1)
so no longer found. How can I automatically define a tikz coordinate for each key-value pair in the coordinates-list?