我正在尝试在 tikz 中标记散点图上的点。例如对于蓝色方块点,我想在绘图本身上在其旁边添加一个标签。类似于在蓝色方块旁边添加一个标签,上面写着“3x5”,而在红色方块旁边添加一个标签,上面写着“3x10”。我可以用我的代码做到这一点吗?谢谢
代码:
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
set layers,% using layers
mark layer=axis tick labels% defines the layer of the marks
}
\begin{loglogaxis}[
xmin=0, xmax=1e7,
ymin=1e-4, ymax=1e3,
xlabel={Number of Epochs},
ylabel={Normalized MSE: $\log_{10} (e)$ },
label style={font=\bfseries\boldmath},
tick label style={font=\bfseries\boldmath},
scatter/classes={ a={mark=square*, blue}, b={mark=square*, red}, c={mark=square, black}, d={mark=triangle*, blue}, e={mark=triangle*, red},f={mark=triangle*, black},g={mark=x, black}, h={mark= diamond*, pink} },
]
\addplot[scatter, only marks,
scatter src=explicit symbolic]
table[meta=label] {
x y label
320 0.608216725 a
500 0.522425565 b
1100 0.362002313 c
3260 0.03403538 a
11420 0.002463198 b
43100 0.000725737 c
};
\end{loglogaxis}
\end{tikzpicture}
\end{document}
答案1
如果您想为每个节点添加标签,最好在表中添加另一列,其中包含这些标签的值。然后,您可以使用visualization depends on
和nodes near coords*
选项的组合来访问这些值,如以下示例所示。
\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{loglogaxis}[
%xmin=0,
xmax=1e7,
ymin=1e-4,
ymax=1e3,
xlabel={Number of Epochs},
ylabel={Normalized MSE: $\log_{10} (e)$},
label style={font=\bfseries\boldmath},
tick label style={font=\bfseries\boldmath},
scatter/classes={
a={mark=square*, blue},
b={mark=square*, red},
c={mark=square, black},
d={mark=triangle*, blue},
e={mark=triangle*, red},
f={mark=triangle*, black},
g={mark=x, black},
h={mark= diamond*, pink}
},
]
\addplot[
scatter,
only marks,
scatter src=explicit symbolic,
nodes near coords*={\annotvalue},
node near coord style={rotate=45, anchor=south west, font=\scriptsize},
visualization depends on={value \thisrow{annotation} \as \annotvalue},
]
table[meta=label] {
x y label annotation
320 0.608216725 a $3\times5$
500 0.522425565 b $3\times10$
1100 0.362002313 c $3\times10$
3260 0.03403538 a $3\times5$
11420 0.002463198 b $3\times10$
43100 0.000725737 c $3\times5$
};
\end{loglogaxis}
\end{tikzpicture}
\end{document}
请注意,使用时visualization depends on
,只有当表中存在相应值时,相关宏才会更新。因此,如果您想省略标签,则需要将\null
相关条目添加到表中。
另一种方法是添加图例:
\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{loglogaxis}[
%xmin=0,
xmax=1e7,
ymin=1e-4,
ymax=1e3,
xlabel={Number of Epochs},
ylabel={Normalized MSE: $\log_{10} (e)$},
label style={font=\bfseries\boldmath},
tick label style={font=\bfseries\boldmath},
scatter/classes={
a={mark=square*, blue},
b={mark=square*, red},
c={mark=square, black},
d={mark=triangle*, blue},
e={mark=triangle*, red},
f={mark=triangle*, black},
g={mark=x, black},
h={mark= diamond*, pink}
},
]
\addplot[
scatter,
only marks,
scatter src=explicit symbolic
]
table[meta=label] {
x y label
320 0.608216725 a
500 0.522425565 b
1100 0.362002313 c
3260 0.03403538 a
11420 0.002463198 b
43100 0.000725737 c
};
\legend{$3\times5$, $3\times10$, $3\times15$}
\end{loglogaxis}
\end{tikzpicture}
\end{document}