1

My question is how to specify the position of node in a plot? Here's a MWE:

\begin{figure}
\begin{tikzpicture}
    \begin{axis}[
        xlabel=X (\%),
        ylabel=Y (\%),
            legend style={
                draw=none, fill=none, 
                font=\tiny,
                at={(0.5,0.17)},
                anchor=north,
                legend columns=4,
                legend cell align={right},
            },
            xmajorgrids,
        ]
            %CORING (2023)
        \addplot+ [red, mark=square*, nodes near coords,every node near coord/.append style=
                        {xshift=15pt,yshift=8pt,anchor=east,font=\footnotesize}, ultra thick]
                coordinates {
                    (88.25, 93.07)
                    (78.66, 93.83)
                    (66.60, 94.20)
                    (58.19, 94.42)
                    (40.00, 94.67)
                    (19.16, 94.75)
                    (00.00, 93.96)};
            \legend{
                    Method A
                    }
    \end{axis}
\end{tikzpicture}
\caption{My method. %in terms of accuracy versus FLOPs reduction. 
}
\end{figure}

enter image description here

In this example, my text is overlapped by line. All nodes's position are set by this:

{xshift=15pt,yshift=8pt}

But this doesn't work well for all nodes. So I want to modify the position of each node. ChatGPT suggests this but it doesn't work:

You can modify the position of each node by adding the "node[pos]" option to each coordinate, where "pos" is a value between 0 and 1 that specifies the position of the node along the line connecting the coordinate to its label. For example, to move the node for the first coordinate to the right, you can set "node[pos=0.5]" instead of "nodes near coords" and adjust the "xshift" and "yshift" values accordingly.

Thanks in advance!

1
  • 1
    ChatGPT is a bot that is primarily designed to chat. It has no connection to the internet (as far as I know) and therefore its output is only based on what it has been fed during training. Therefore it is (so far at least) limited in giving advice as to how to solve certain problems in TeX. In this case, you want to style a label to a coordinate that has been placed by PGFplots. The option pos does not work here. Commented Mar 6, 2023 at 12:47

1 Answer 1

1

You can use the option coordinate style to modify the style of certain labels depending on a specific condition (of course you could also set xshift or yshift for labels at specific coordinates using this mechanism):

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xlabel=X (\%),
        ylabel=Y (\%),
        legend style={
            draw=none, fill=none, 
            font=\tiny,
            at={(0.5,0.17)},
            anchor=north,
            legend columns=4,
            legend cell align={right},
        },
        xmajorgrids,
    ]
        %CORING (2023)
        \addplot+ [
            red, 
            mark=square*, 
            nodes near coords,
            every node near coord/.append style={
                font=\footnotesize
            }, 
            ultra thick, 
            coordinate style/.condition={x < 10 || x > 40}{
                anchor=west,
            },
            coordinate style/.condition={x > 60}{
                anchor=east,
            }
        ]
        coordinates {
            (88.25, 93.07)
            (78.66, 93.83)
            (66.60, 94.20)
            (58.19, 94.42)
            (40.00, 94.67)
            (19.16, 94.75)
            (00.00, 93.96)
        };
        \legend{
            Method A
        }
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .