2

I want to highlight a point (i.e., (3, 4.8)) in the following line graph by drawing a red circle around it. However, as you can see, the circle is not drawn at the expected position.

Why? And how to fix this?

score-mark

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmin = 0, xmax = 6,
    ymin = 4.5, ymax = 5,
    xtick = {1,2,3,4,5},
    ytick = {},
    ymajorgrids = true,
    grid style = dashed,
]
\addplot[color = blue, mark = square, nodes near coords, ]
    coordinates {
      (1, 5.0)
      (2, 4.90)
      (3, 4.80)
      (4, 4.70)
      (5, 4.60)
    };
  % first try: failed
  \node (mark) [draw, red, circle, minimum size = 5pt] at (3, 4.80) {(1)};
\end{axis}
% second try: failed
\node (mark) [draw, red, circle, minimum size = 5pt] at (3, 4.80) {(2)};
\end{tikzpicture}
\end{document}
3

It works if you specify axis cs:

enter image description here

As Schrödinger's cat mentioned, axis cs: became the default in pgfplots 1.11, but as this change was not backward compatible, to activate this requires one to explicitly specify the compatibility mode to some version greater than 1.11. The latest version is accessed by specifying

\pgfplotsset{compat=1.16}

To access the newest version, one could specify \pgfplotsset{compat=newest}. However, use of this option is discouraged as a future compile may not behave as desired.

Code:

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmin = 0, xmax = 6,
    ymin = 4.5, ymax = 5,
    xtick = {1,2,3,4,5},
    ytick = {},
    ymajorgrids = true,
    grid style = dashed,
]
\addplot[color = blue, mark = square, nodes near coords, ]
    coordinates {
      (1, 5.0)
      (2, 4.90)
      (3, 4.80)
      (4, 4.70)
      (5, 4.60)
    };
  % first try: now works
  \node (mark) [draw, red, circle, minimum size = 5pt, inner sep=2pt, thick] 
      at (axis cs: 3, 4.80) {};
\end{axis}
\end{tikzpicture}
\end{document}
  • 2
    @Schrödinger'scat: Good point. Have included that in the text now. – Peter Grill Sep 21 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.