pgfplots 中的通用点标签

pgfplots 中的通用点标签

我有很多pgfplots,它们有一个共同点,它们都包含一个或两个addplot带有四个点的点,我想用相同的标签来标记它们,即: 。为简单起见,\tau_c= n \theta我们假设n12和。34在 pgfplots 中使用 foreach 循环变量作为节点标签看起来非常像可以解决我的问题的东西。但是,当给出的坐标与下面的 MWE 中给出的坐标一样时,我该如何附加此(或类似的)解决方案?我的pgfplots 是用 生成的,matlab2tikz并且如上所述,数量相当多,所以我想避免过多地操作现有文件。

数学家

\documentclass{memoir}
\usepackage{tikz, pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
% [spy using outlines={rectangle,black,magnification=3,connect spies}]
\begin{axis}[%
width=3in,
height=1in,
unbounded coords=jump,
scale only axis,
xmin=1.25,
xmax=3,
xlabel={That},
ymin=0,
ymax=8,
ylabel={This},
title={Case 11},
% legend style={draw=black,fill=white,legend cell align=left}
]
\addplot [
color=red,
only marks,
mark=o,
mark options={solid},
forget plot
]
table[row sep=crcr]{
2.17534491233797 1.76417509678952\\
1.70352168717505 2.47466020520172\\
1.50385056450878 3.36056987643388\\
1.39330071637349 4.41283273476325\\
};
\end{axis}
\end{tikzpicture}%
\end{document}

答案1

你可以使用nodes near coords函数来实现这一点。如果你添加

\pgfplotsset{
    nodes near coords={
        \pgfmathtruncatemacro\nValue{\coordindex+1}%
        $\nValue \theta$%
    }
}

到您的文档,节点标签将被打印,而您matlab2tikz根本不需要触摸生成的代码:


如果您希望能够提供不直接依赖于坐标索引的任意标签,则可以使用存储标签字符串的 PGF 数学数组并在其中访问该数组nodes near coords

\def\labellist{{"$\sfrac{1}{2}\theta$","$\theta$","$\sfrac{3}{2}\theta$","$2\theta$"}}
\pgfplotsset{
    nodes near coords={
        \pgfmathparse{\labellist[\coordindex]}%
        \pgfmathresult
    }
}

相关内容