如何从数据列中获取节点的文本?

如何从数据列中获取节点的文本?

我有一张包含三列的表格。我正在其中两列上绘制点图,我想从顶部开始签名这些点,并从第一列(索引为 0 的列)中获取数据进行签名。这该怎么做?(最好使用最少的内存)。

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\usepackage{filecontents}
\usepackage{booktabs}

\begin{filecontents*}{data.dat}
Interstellar medium, 1, 1
Ionosphere, 1e11, 5e-2
TOKAMAK, 1e13, 1e3
Smoldering discharge, 1e15, 2
Metals, 1e25, 1e-2
Solar core, 1e26, 1e3
White dwarf core, 1e30, 2e4
\end{filecontents*}


\begin{document}
\begin{tikzpicture}
    \begin{axis}[
            ymode=log,
            xmode=log,
            xlabel={$n$, cm$^{-3}$},
            ylabel={$T$, eV},
            xmin=1e-5,
            xmax=1e30,
            ymin=1e-4,
        ]
        
       \pgfplotstableread[col sep=comma, header=false]{data.dat}\datatable
       \addplot[blue, only marks] table[x index = 1, y index = 2] \datatable node[above, font=\scriptsize] {
       % content of first (index 0) column
       };
% This is how it should work if the data is not taken from the table

%       \addplot[blue, only marks] coordinates { (1e13, 1e3) } node[above, font=\scriptsize] {TOKAMAK};
%
%       \addplot[blue, only marks] coordinates { (1e15, 2) } node[above, font=\scriptsize] {Smoldering discharge};
%
%       \addplot[blue, only marks] coordinates { (1e11, 0.05) } node[above, font=\scriptsize] {Ionosphere};
%
%       \addplot[blue, only marks] coordinates { (1e26, 1e3) } node[above, font=\scriptsize] {Solar core};
%
%       \addplot[blue, only marks] coordinates { (1e25, 1e-2) } node[above, font=\scriptsize] {Metals};
%
%       \addplot[blue, only marks] coordinates { (1e30, 2e4) } node[above left, font=\scriptsize] {White dwarf core};
%
%       \addplot[blue, only marks] coordinates { (1, 1) } node[above, font=\scriptsize, text width=3cm, align=center] {Interstellar medium};
    \end{axis}
\end{tikzpicture}
\end{document}

答案1

我直接使用pgfplots而不使用\pgfplotstableread。您需要包含nodes near coordskey 并将绘图样式设置为scatter。您还需要指定point metaasexplicit symbolic然后将 指定meta index为 等于 0。您可以使用nodes near coords stylekey 调整每个节点样式,也可以使用coordinate style/.conditionkey 更改单个节点样式。

这是一个有效的例子:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18} 
\begin{filecontents*}{data.dat}
Interstellar medium, 1, 1
Ionosphere, 1e11, 5e-2
TOKAMAK, 1e13, 1e3
Smoldering discharge, 1e15, 2
Metals, 1e25, 1e-2
Solar core, 1e26, 1e3
White dwarf core, 1e30, 2e4
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ymode=log,
    xmode=log,
    xlabel={$n$, cm$^{-3}$},
    ylabel={$T$, eV},
    xmin=1e-5,
    xmax=1e30,
    ymin=1e-4,
]
\addplot[
scatter,
nodes near coords,
nodes near coords style={font=\scriptsize},
coordinate style/.condition={\coordindex==0}{xshift=0.3cm},
coordinate style/.condition={\coordindex==6}{xshift=-1.2cm},
blue, only marks, 
point meta=explicit symbolic
] table [header=false,meta index=0,x index = 1, y index = 2,col sep=comma] {data.dat};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容