![如何在 TikZ 散点图中放置点标签](https://linux22.com/image/446377/%E5%A6%82%E4%BD%95%E5%9C%A8%20TikZ%20%E6%95%A3%E7%82%B9%E5%9B%BE%E4%B8%AD%E6%94%BE%E7%BD%AE%E7%82%B9%E6%A0%87%E7%AD%BE.png)
我想要此图中各点的标签:
\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}