![按標籤標記節點/座標 (PGFPlots)](https://rvso.com/image/309870/%E6%8C%89%E6%A8%99%E7%B1%A4%E6%A8%99%E8%A8%98%E7%AF%80%E9%BB%9E%2F%E5%BA%A7%E6%A8%99%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}