라벨로 노드/좌표 표시(PGFPlots)

라벨로 노드/좌표 표시(PGFPlots)

데이터가 테이블에 있고 값에 레이블이 지정된 산점도를 그리고 있습니다. 특정 노드를 직선으로 연결하고 싶고 그렇게 하려면 노드/좌표에 레이블을 지정해야 합니다.

작동하는 솔루션이 있지만 IMO 중복으로 인해 구현이 마음에 들지 않습니다.

산점도에 대한 현재 코드는 다음과 같습니다.

\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}

위 코드의 결과를 보여주는 이미지

관련 정보