如何在 TikZ 散点图中放置点标签

如何在 TikZ 散点图中放置点标签

我想要此图中各点的标签:

\documentclass[tikz,margin=2mm]{standalone}

\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}

\begin{tikzpicture}
\begin{axis}[enlargelimits=0.2,
        grid=both, grid style = {lightgray!45},
        % xlabel = {Detection Precision}, % BX
        xlabel = {Punch Detection Accuracy}, % BR
        ylabel = {Clustering Accuracy}, % CB
        scale=1.3, 
        % scale only axis=true,
        % legend style={at={(0.02,0.95)},anchor=north west},
        % legend pos = south east,
%       legend cell align={left},
        % xtick={0.6,0.65,...,1},
        xmax=1,
    ]
    \addplot[
        scatter/classes={a={blue}, b={red}},
        scatter, mark=*, only marks,
        scatter src=explicit symbolic,
        nodes near coords*={\Label},
        visualization depends on={value \thisrow{label} \as \Label} %<- added value
    ] table [meta=class] {
        x y class label
        0.863   1       a   S1
        0.981   0.981   a   S2
        0.932   0.833   a   S3
        0.947   0.947   a   S4
        0.91    0.989   a   S5
        0.875   0.982   a   S6
        0.972   0.921   a   S7
        0.833   0.882   a   S8
        0.649   0.787   a   S9
        0.816   0.816   a   S10
        0.615   0.615   a   S11
        0.942    0.98   a   S12
    };
    
    
    % Linear regression
    \addplot[
        thick,
        red
    ] table[
        x = BR,
        y = {create col/linear regression={y=CB}}
    ] {result.dat};
    

\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

不重叠,例如 S1、S6、S4。

result.dat文件是一个文本文件,内容如下:

BR  CB
0.863   1
0.981   0.981
0.932   0.833
0.947   0.947
0.91    0.989
0.875   0.982
0.972   0.921
0.833   0.882
0.649   0.787
0.816   0.816
0.615   0.615
0.942   0.98

我尝试缩放图表,将点分开。但是,图表变得更小(字体变小),看起来更糟。

我想减小点标签的字体大小或移动点周围的标签,使图形更清晰地出现在双列页面的一列中。

答案1

一种解决方案是在其下方写入标记“S4”和“S6”的节点:

在此处输入图片描述

为了清晰起见,建议将节点 S4 和 S6 及其标记涂成彩色。在实际文档中,可以/应该删除这些颜色。

\RequirePackage{filecontents}
\begin{filecontents}{result.dat}
    BR      CB
    0.863   1
    0.981   0.981
    0.932   0.833
    0.947   0.947
    0.91    0.989
    0.875   0.982
    0.972   0.921
    0.833   0.882
    0.649   0.787
    0.816   0.816
    0.615   0.615
    0.942   0.98
\end{filecontents}

\documentclass[tikz,margin=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage{pgfplotstable}

\begin{document}
    \begin{tikzpicture}
\begin{axis}[
        grid,
        grid style = {lightgray!45},
        enlargelimits={0.1, upper},
        xlabel = {Punch Detection Accuracy},    % BR
        ylabel = {Clustering Accuracy},         % CB
    tick label style = {font=\small},
    xmin=0.6, ymin=0.6,
%
    scatter, mark=*,
    scatter/classes={a={blue}, b={teal}},
    scatter src=explicit symbolic,
    nodes near coords style = {font=\scriptsize, inner xsep=-1pt}
%
    nodes near coords*={\Label},
    visualization depends on={value \thisrow{label} \as \Label},
            ]
\addplot[
    nodes near coords style = {text=blue, anchor=south west},% in real doc. remove text color
        only marks
        ]   table [meta=class] {
        x       y       class   label
        0.863   1       a       S1
        0.981   0.981   a       S2
        0.932   0.833   a       S3
%        0.947   0.947   a       S4
        0.91    0.989   a       S5
%        0.875   0.982   a       S6
        0.972   0.921   a       S7
        0.833   0.882   a       S8
        0.649   0.787   a       S9
        0.816   0.816   a       S10
        0.615   0.615   a       S11
        0.942    0.98   a       S12
        };
\addplot[
    nodes near coords style = {text=teal, anchor=north east},% in real doc. remove text color
    only marks
        ]   table [meta=class] {
        x       y       class   label
        0.947   0.947   b       S4
        0.875   0.982   b       S6
        };
%%%% Linear regression
\addplot[thick, red, no marks] table[
            x = BR,
            y = {create col/linear regression={y=CB}}
                            ] {result.dat};
\end{axis}
    \end{tikzpicture}
\end{document}

相关内容