![Отметить узлы/координаты меткой (PGFPlots)](https://rvso.com/image/309870/%D0%9E%D1%82%D0%BC%D0%B5%D1%82%D0%B8%D1%82%D1%8C%20%D1%83%D0%B7%D0%BB%D1%8B%2F%D0%BA%D0%BE%D0%BE%D1%80%D0%B4%D0%B8%D0%BD%D0%B0%D1%82%D1%8B%20%D0%BC%D0%B5%D1%82%D0%BA%D0%BE%D0%B9%20(PGFPlots).png)
Я рисую диаграмму рассеивания, где данные находятся в таблице, а значения помечены. Я хочу соединить определенные узлы прямыми линиями, и для этого нужно пометить узлы/координаты.
Должен сказать, что у меня есть работающее решение, но я недоволен его реализацией, поскольку, по моему мнению, оно избыточно.
Мой текущий код для диаграммы рассеяния выглядит следующим образом:
\begin{tikzpicture}
\begin{axis}[
width=9cm,
]
\addplot[
scatter,
black,
nodes near coords,
only marks,
point meta=explicit symbolic,
mark=o,
] table[
x=cpu,
y=error,
meta=label,
] {
cpu error label
0.45 0.20 A
0.35 0.28 B
0.27 0.30 C
0.33 0.23 Č
0.25 0.40 D
0.33 0.35 E
0.40 0.35 F
}
% pos = (index-1)/(N-1) (index starting from 1)
coordinate [pos=0/6] (A)
coordinate [pos=2/6] (C)
coordinate [pos=3/6] (Č)
coordinate [pos=4/6] (D)
;
\draw (D) -- (C);
\draw (C) -- (Č);
\draw (Č) -- (A);
\end{axis}
\end{tikzpicture}
А сюжет выглядит так:
Часть кода, которую я считаю избыточной, выглядит следующим образом:
coordinate [pos=0/6] (A)
coordinate [pos=2/6] (C)
coordinate [pos=3/6] (Č)
coordinate [pos=4/6] (D)
У меня следующий вопрос: есть ли способ ссылаться на метки напрямую, когда я использую \draw (D) -- (C);
?
решение1
Это можно сделать, но вопрос в том, будет ли это более элегантным, чем ваше уже предложенное решение.
Более подробную информацию о том, как это можно сделать, можно найти в комментариях в коде.
% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
only marks,
% we want to add `nodes near coords', but this time a bit different than
% usual. We want to place the node name *to the "marker"* and not to
% the "normal" `node near coords'. That means the "main" node of the
% `nodes near coords' shall not show anything
nodes near coords={},
% and now we adapt the `nodes near coords style' to our needs
nodes near coords style={
% we only need `coordinate' style for the `nodes near coords'
coordinate,
% and add a label to the "main" node which shows the `point meta' data
label={above:\pgfplotspointmeta},
% in addition we want to name the `nodes near coords' also with
% the `point meta' data
name=\pgfplotspointmeta,
},
% !!! now comes a critical point !!!
% Option a)
% to make that solution work you have set the following option due to
% technical reasons which is roughly:
% normally the markers are first collected and are drawn *after*
% `\end{axis}', in contrast to the `\draw' command. Said that, the
% named `nodes near coords' are not available during the execution of
% the `\draw' command
clip marker paths=true,
point meta=explicit symbolic,
]
\addplot[
scatter,
mark=o,
] table [
x=cpu,
y=error,
meta=label,
] {
cpu error label
0.45 0.20 A
0.35 0.28 B
0.27 0.30 C
0.33 0.23 Č
0.25 0.40 D
0.33 0.35 E
0.40 0.35 F
}
%% the idea is to replace this here ...
% % pos = (index-1)/(N-1) (index starting from 1)
% coordinate [pos=0/6] (A)
% coordinate [pos=2/6] (C)
% coordinate [pos=3/6] (Č)
% coordinate [pos=4/6] (D)
;
% option a)
\draw (D) -- (C) -- (Č) -- (A);
\end{axis}
% option b)
% the other option -- so when `clip marker path=false' (the default) -- is
% to draw the connecting line after `\end{axis}', because now the named
% `nodes near coords' are available
\draw (D) -- (C) -- (Č) -- (A);
\end{tikzpicture}
\end{document}