[Tex/LaTex] Custom data labels for each plot in bar chart

nodes-near-coordspgfplotstikz-pgf

I have a simple bar chart, but I want the data labels to have a custom string value. For example, see the attached picture. I have found plenty of examples with plots, but nothing for bar charts. Note that every single co-ordinate has a different value associated with it. Is there a way I can enter a custom value, as some sort of additional argument to the coordinates?

\begin{figure}
\begin{tikzpicture}
\begin{axis}[
    ybar,
    enlargelimits=0.15,
    legend style={at={(0.5,-0.15)},
    anchor=north,legend columns=-1},
    ylabel={Speedup},
    xlabel={\# of Model Elements (millions)},
    symbolic x coords={1m,1.5m,2m,4m},
    xtick=data,
    nodes near coords,
    nodes near coords align={vertical},
    ]
\addplot coordinates {(1m,92.021) (1.5m,235.809) (2m,276.824) (4m,340.847)};
\end{axis}
\end{tikzpicture}
\caption{Results}
\label{fig:mycaption}
\end{figure}

desired image

Best Answer

This is very easy: just build up a list of strings and access them with \coordindex. To this end we define a comma-separated list of strings,

\edef\mylst{"An arbitrary string","String","Custom label","Not this data"}

where the first entry (which has internally index 0) will be used for the first node, the second entry for the second node, and so on. Make sure that the list as at least as many entries as nodes that exist.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{figure}
\begin{tikzpicture}
\edef\mylst{"An arbitrary string","String","Custom label","Not this data"}
\begin{axis}[width=12cm,
    ybar,
    enlargelimits=0.15,
    legend style={at={(0.5,-0.15)},
    anchor=north,legend columns=-1},
    ylabel={Speedup},
    xlabel={\# of Model Elements (millions)},
    symbolic x coords={1m,1.5m,2m,4m},
    xtick=data,
    nodes near coords=\pgfmathsetmacro{\mystring}{{\mylst}[\coordindex]}\mystring,
    nodes near coords align={vertical},
    ]
\addplot coordinates {(1m,92.021) (1.5m,235.809) (2m,276.824) (4m,340.847)};
\end{axis}
\end{tikzpicture}
\caption{Results}
\label{fig:mycaption}
\end{figure}
\end{document}

The strings are a bit too long. Are you OK with using multiple lines for them?

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{figure}[htb]
\centering
\begin{tikzpicture}
\edef\mylst{"An arbitrary string","String","Custom label","Not this data"}
\begin{axis}[ymax=370,
    ybar,
    enlargelimits=0.15,
    legend style={at={(0.5,-0.15)},
    anchor=north,legend columns=-1},
    ylabel={Speedup},
    xlabel={\# of Model Elements (millions)},
    symbolic x coords={1m,1.5m,2m,4m},
    xtick=data,
    nodes near coords style={font=\sffamily,align=center,text width=4em},
    nodes near coords=\pgfmathsetmacro{\mystring}{{\mylst}[\coordindex]}\mystring,
    nodes near coords align={vertical},
    ]
\addplot coordinates {(1m,92.021) (1.5m,235.809) (2m,276.824) (4m,340.847)};
\end{axis}
\end{tikzpicture}
\caption{Results}
\label{fig:mycaption}
\end{figure}
\end{document}

enter image description here

Related Question