我从两个文件中读取数据并绘制图表ycomb
。我想计算值之间的差值并将其放在较大值的上方,如图所示。我该怎么做?
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
enlarge y limits=0.5,
]
\addplot+[ycomb,scatter] table[x=x,y=y]{
y x
3 3
};
\addplot+[ycomb,scatter] table[x=x,y=y]{
y x
5 3
};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
也许有点绕弯子,可能还有更简单的方法,但这里有一个建议。代码中有一些注释表明会发生什么,如果有什么不清楚的地方,我可以添加更多细节。
开始时的内容filecontents
只是为了使示例独立,当您已经有文件时,您不需要它。
\begin{filecontents*}{data1.dat}
y x
3 3
6 4
\end{filecontents*}
\begin{filecontents*}{data2.dat}
y x
5 3
2 4
\end{filecontents*}
\documentclass{standalone}
\usepackage{pgfplotstable} % loads pgfplots which loads tikz
\pgfplotsset{compat=newest}
% read data files to tables
\pgfplotstableread{data1.dat}{\dataA}
\pgfplotstableread{data2.dat}{\dataB}
% make two new columns to \dataB
% first column has absolute value of the difference between the y-values in the two files
\pgfplotstablecreatecol[
create col/assign/.code={
\pgfplotstablegetelem{\pgfplotstablerow}{y}\of\dataA
\pgfmathparse{abs(\thisrow{y}-\pgfplotsretval)}
\edef\entry{\pgfmathresult}
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}
]{diff}{\dataB}
% the second column has the maximum of the two y-values
\pgfplotstablecreatecol[
create col/assign/.code={
\pgfplotstablegetelem{\pgfplotstablerow}{y}\of\dataA
\pgfmathparse{max(\thisrow{y},\pgfplotsretval)}
\edef\entry{\pgfmathresult}
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}
]{max}{\dataB}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
ymin=0
]
\addplot+[ycomb] table[x=x,y=y]{\dataA};
\addplot+[ycomb] table[x=x,y=y]{\dataB};
% use third \addplot to add labels
\addplot [
only marks,mark=none, % don't actually plot anything visible
nodes near coords, % add the labels to points
nodes near coords align=above, % place labels above points
point meta=explicit % so it reads from the meta column, instead of using the y-value
] table[x=x,y=max,meta=diff] {\dataB};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
当你将数据表合并为一个表时,很容易nodes near coords
使用第三个表添加数据\addplot
(如Torbjørn T.在他的回答中做了)。
(不添加第三个“虚拟”\addplot
命令也可以实现相同的结果,但会更加复杂。)
有关详细信息,请查看代码中的注释。
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
% combine the data in one table
\pgfplotstableread{
x y1 y2
3 3 5
}{\data}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
enlarge y limits=0.5,
ycomb,
]
% plot the
\addplot table [x=x,y=y1] {\data};
\addplot table [x=x,y=y2] {\data};
% add the nodes near coords
\addplot [
% the plot itself should not be visible
draw=none,
% but we want to have the `nodes near coords' ...
nodes near coords,
% ... for which the values are explicitly given/calculated
point meta=explicit,
% only needed in case you want to add another `\addplot to not
% change the cycle list index etc.
forget plot,
] table [
x=x,
% add the `nodes near coords' at the larger of the two values ...
y expr={max(\thisrow{y1},\thisrow{y2})},
% ... and show the difference of the two values
meta expr={abs(\thisrow{y1}-\thisrow{y2})},
] {\data};
\end{axis}
\end{tikzpicture}
\end{document}