表格中坐标附近的节点

表格中坐标附近的节点

我想在直方图上添加一个非数值作为节点。标签来自表中的一列,非常类似于其他问题,即使用value \thisrow{<column>} as <macro>。我发现的问题是标签没有正确呈现,而是显示为thisrow("<column>")

在此处输入图片描述

这是我的代码

\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}

提前致谢

答案1

加载的表和从文件中读取的表的处理并不完全对称,后者有效。

\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}

在此处输入图片描述

答案2

我改编了手册中的一个例子(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}

导致

\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}

在此处输入图片描述

相关内容