3

I want to add a non-numerical value as a node on a histogram. The labels come from a column in a table, fairly similar to this other question, i.e. using value \thisrow{<column>} as <macro>. The problem I'm finding is that the labels are note properly rendered, instead they show up as thisrow("<column>")

enter image description here

This is my code

\documentclass[border = 5pt]{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable}

\pgfplotsset{compat = 1.15}


\begin{filecontents*}{my.csv}
key,value,label
k1,1,foo
k2,2,bar
k3,3,baz
k4,4,qux
\end{filecontents*}


\begin{document}

\pgfplotstableread[col sep = comma]{my.csv}{\mytable};
\begin{tikzpicture}
\begin{axis}[
  xtick = {0,...,3},
  xticklabel = {
    \pgfmathparse{int(round(\tick))}
    \pgfplotstablegetelem{\pgfmathresult}{key}\of\mytable\pgfplotsretval
  },
  ]
  \addplot[
  ybar,
  nodes near coords = {\thelabel{}},
  visualization depends on = {value \thisrow{label} \as \thelabel},
  ] table [
    x expr = \coordindex,
    y = value
  ]\mytable;
\end{axis}
\end{tikzpicture}
\end{document}

Thanks in advance

3

The treatment of loaded tables and tables that you read from a file is not completely symmetric, and the latter works.

\documentclass[border = 5pt]{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable}

\pgfplotsset{compat = 1.15}


\begin{filecontents*}{my.csv}
key,value,label
k1,1,foo
k2,2,bar
k3,3,baz
k4,4,qux
\end{filecontents*}


\begin{document}

\pgfplotstableread[col sep = comma]{my.csv}{\mytable}
\begin{tikzpicture}
\begin{axis}[
  xtick = {0,...,3},
  xticklabel = {
    \pgfmathparse{int(round(\tick))}
    \pgfplotstablegetelem{\pgfmathresult}{key}\of\mytable\pgfplotsretval
  },
  ]
  \addplot[
  ybar,
  visualization depends on={value \thisrow{label}\as\mylabel},
  nodes near coords = {\mylabel{}},
  ] table [col sep = comma,
    x expr = \coordindex,
    y = value
  ]{my.csv};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Related discussion on the asymmetric treatment: tex.stackexchange.com/a/356790/121799 – user121799 May 25 at 14:44
  • +1: I added the old code so that the change is clearer (if that's ok). – Dr. Manuel Kuehner May 25 at 14:50
  • Is the solution the exchange of \mylabel and \thelabel? – Dr. Manuel Kuehner May 25 at 14:52
  • @Dr.ManuelKuehner This is not the important change. The important change is that \mytable, i.e. the macro that contained the table, got replaced by {my.csv}, i.e. the file that contained the table. May I ask you to revert the changes? (+1 to you, too.) – user121799 May 25 at 14:53
  • 1
    @Dr.ManuelKuehner No, the difference is that I plot the file, not the loaded table. – user121799 May 25 at 15:07
3

I adapted an example from the manual (3.4.3 Scatter Plot Use Case C):

% Preamble: \pgfplotsset{width=7cm,compat=1.16}
\begin{tikzpicture}
\begin{axis}[
enlargelimits=0.2,
]
\addplot+ [nodes near coords,only marks,
point meta=explicit symbolic]
table [meta=label] {
x y label
0.5 0.2 1
0.2 0.1 t2
0.7 0.6 3
0.35 0.4 Y4
0.65 0.1 5
};
\end{axis}
\end{tikzpicture}

Leading to

\documentclass[border = 5pt]{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{filecontents*}{my.csv}
key,value,label
k1,1,foo
k2,2,bar
k3,3,baz
k4,4,qux
\end{filecontents*}


\begin{document}

\pgfplotstableread[col sep = comma]{my.csv}{\mytable};

\begin{tikzpicture}
\begin{axis}[
  xtick = {0, ..., 3},
  ]
  \addplot
  [
    ybar,
    point meta = explicit symbolic, % <-- added 
    nodes near coords,
  ] 
  table 
  [
    meta = label, % <-- added 
    x expr = \coordindex,
    y = value,
  ]
  \mytable;
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

  • Thanks a lot :) – caverac May 25 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.