如何在 pgfplots 中标记/标记文件中的第 n 个数据点?

如何在 pgfplots 中标记/标记文件中的第 n 个数据点?
\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.10}

\pgfplotstableread{
0.01    1.00
0.02    2.00
0.03    3.00
0.04    4.00
0.05    5.00
}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis}[ymin=0, ymax=6]
\addplot table  {\datatable}
node[pos=0.0, pin=above:"Unidentified peak"]{} % pos only allows for fractional value
node[pos=0.2, pin=above:"Bragg peak"]{} % pos only allows for fractional value
node[pos=1.0, pin=above:"my custom label"]{} % pos only allows for fractional value
;

\end{axis}
\end{tikzpicture}
\end{document}

更新: @Matthew Leingang 的回答中第二个解决方案的稍微简短的版本:

\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable} % For \pgfplotstableread
\pgfplotsset{compat=1.10}

\pgfplotstableread{
0.01    1.00
0.011   2.00
0.012   3.00
0.013    4.00
0.05    5.00
}\datatable
% \pgfplotstableread{results.dat}\datatable

\pgfplotstablegetrowsof{\datatable}
\pgfmathsetmacro{\N}{\pgfplotsretval}  

\begin{document}
\begin{tikzpicture}
\begin{axis}[ymin=0, ymax=6]
  \addplot+[mark=none, forget plot] table {\datatable};
  \addplot+[only marks] table  {\datatable}
    % pos = (index-1)/(N-1)  (index starting from 1)
    node[pos=(1-1)/(\N-1), pin=right:first point (1/\N)]{}
    node[pos=(3-1)/(\N-1), pin=-45:third point (3/\N)]{}
    node[pos=(4-1)/(\N-1), pin=-45:end of linear behaviour (4/\N)]{}
;
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

我认为最简单的方法就是像你那样做,记住,如果有等距点,pos-ition 的n第点是 (n-1)/(N-1)。

在您的例子中,您有 5 个点,并且想要标记第二个点,因此它应该位于位置 0.25:

\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable} % For \pgfplotstableread

\pgfplotstableread{
0.01    1.00
0.02    2.00
0.03    3.00
0.04    4.00
0.05    5.00
}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis}[ymin=0, ymax=6]
\addplot table  {\datatable}
node[pos=0.0, pin=right:``first point'']{} 
node[pos=0.25, pin=above:``second point'']{} 
node[pos=1.0, pin=left:``last point'']{} 
;

\end{axis}
\end{tikzpicture}
\end{document}

示例代码输出

只要您的点间距均匀,此方法即可奏效。如果不是,您有两种选择:

  1. 根据点坐标计算相对位置。如果它们被标记x_1, x_2, ..., x_N,则n第 个点位于位置(x_n - x_1)/(x_N - x_1)

  2. 使用只绘制点而不进行插值的绘图处理程序,例如only marks。然后您可以使用分数位置,处理程序将“捕捉到”由该分数索引的点。因此,如果有五个点,则pos=0.25无论它是否真的是从左到右的 1/4 处,都会捕捉到第二个点。

如果你有间距不均匀的点,并且你想标记它们,对它们进行插值,标记它们,您可以使用两步方法。首先,用 绘制不带标记的图,\addplot+[mark=none,forget plot] ... 然后仅绘制标记并标记\addplot+[only marks] ... node[pos=0.25] ...。以下是一个例子:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}

\begin{tikzpicture}[
    declare function={
        f(\x) = 2*(\x)^3 - 3*(\x)^2 - 12 *\x;
    }]
    \begin{axis}[
        width=\linewidth,
        xmin=-3.3,xmax=4,
        ymin=-21,ymax=21,
        xtick={-1,0.5,2},
        xticklabels={$-1$,$\frac12$,$2$},
        ytick=\empty,
        samples=50
    ]
        \addplot+[domain=-3:4,mark=none,forget plot] {f(x)};
        \addplot+[samples at={-1.8117,-1,0,0.5,2,3.3117},only marks] {f(x)}
            node[pos=0,
                pin={-88:$\left(\frac{1}{4}\left(3 - \sqrt{105}\right),0\right)$},
                ] {}
            node[pos=0.2,pin={$(-1,7)$}] {}
            node[pos=0.4,pin={15:$(0,0)$}] {}
            node[pos=0.6,pin={0:$\left(\frac12,-6\frac12\right)$}] {}
            node[pos=0.8,pin={90:$(2,-20)$}] {}
            node[pos=1.0,pin={95:$\left(\frac{1}{4}\left(3 + \sqrt{105}\right),0\right)$}] {}
        ;
    \end{axis}
\end{tikzpicture}
\end{document}

第二个示例代码输出

forget plot命令使绘图周期保持不变,因此第二个绘图的样式与第一个绘图完全相同。第二个\addplot标记特定(间距不均匀)的点。由于有六个点,因此第二个绘图的位置n-这0.2n

答案2

我会使用nodes near coords此功能,它允许您将标签直接与坐标关联,而不管间距如何。使用一些 TikZ 风格的技巧,您可以定义一个键

nodes near some coords={1,3}

它采用单个坐标索引或索引列表,并仅为这些坐标激活标签打印:

\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable} % For \pgfplotstableread
\pgfplotsset{compat=1.10}

\pgfplotsset{
    node near coord/.style={ % Style for activating the label for a single coordinate
        nodes near coords*={
            \ifnum\coordindex=#1\pgfmathprintnumber{\pgfplotspointmeta}\fi
        }
    },
    nodes near some coords/.style={ % Style for activating the label for a list of coordinates
        scatter/@pre marker code/.code={},% Reset the default scatter style, so we don't get coloured markers
        scatter/@post marker code/.code={},% 
        node near coord/.list={#1} % Run "node near coord" once for every element in the list
    }
}

\pgfplotstableread{
0.01    1.00
0.02    2.00
0.024    3.00
0.04    4.00
0.05    5.00
}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis}[ymin=0, ymax=6, every node near coord/.style=above left]
\addplot +[
    nodes near some coords=0
] table {\datatable};

\end{axis}
\end{tikzpicture}
\end{document}

相同的方法也可用于提供要打印的每个坐标的标签以及所需的对齐。

呼唤

\addplot +[
    nodes near some coords={1/Bragg valley/below,3/Bragg peak/above}
] table {\datatable};

将产生以下结果:

\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable} % For \pgfplotstableread

\pgfplotsset{
    node near coord/.style args={#1/#2/#3}{% Style for activating the label for a single coordinate
        nodes near coords*={
            \ifnum\coordindex=#1 #2\fi
        },
        scatter/@pre marker code/.append code={
            \ifnum\coordindex=#1 \pgfplotsset{every node near coord/.append style=#3}\fi
        }
    },
    nodes near some coords/.style={ % Style for activating the label for a list of coordinates
        scatter/@pre marker code/.code={},% Reset the default scatter style, so we don't get coloured markers
        scatter/@post marker code/.code={},% 
        node near coord/.list={#1} % Run "node near coord" once for every element in the list
    }
}

\pgfplotstableread{
0.01    3.00
0.02    2.00
0.024    3.00
0.04    4.00
0.05    1.00
}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis}[ymin=0, ymax=6, every node near coord/.style=above left]
\addplot +[
    nodes near some coords={1/Bragg valley/below,3/Bragg peak/above}
] table {\datatable};

\end{axis}
\end{tikzpicture}
\end{document}

如果您想使用pins,您可以使用稍微改变(并且稍微更优雅)的版本的方法:

\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable} % For \pgfplotstableread

\pgfplotsset{
    pin near coord/.style args={#1/#2}{% Style for activating the label for a single coordinate
        scatter/@pre marker code/.append code={
            \ifnum\coordindex=#1 \node[pin=#2]{};\fi
        }
    },
    pins near some coords/.style={ % Style for activating the label for a list of coordinates
        scatter,
        scatter/@pre marker code/.code={},% Reset the default scatter style, so we don't get coloured markers
        scatter/@post marker code/.code={},% 
        pin near coord/.list={#1} % Run "pin near coord" once for every element in the list
    }
}

\pgfplotstableread{
0.01    3.00
0.02    2.00
0.024    3.00
0.04    4.00
0.05    1.00
}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis}[ymin=0, ymax=6, every node near coord/.style=above left]
\addplot +[
    pins near some coords={1/south:Bragg valley,3/Bragg peak}
] table {\datatable};

\end{axis}
\end{tikzpicture}
\end{document}

相关内容