2

I am using this code, taken from my question here. The problem arises when two x values have the same y value. Then the label I want to write near the coordinate overlaps with the other one. How can I resolve this issue, even specifically for this particular input?

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepackage{filecontents}
\begin{filecontents*}{mydata.dat}
  x_0 f(x) label
  %some comment
  0.5 0.24 0
  0.5 0.21 0.1
  0.5 0.18 0.5
  0.5 0.21 0.9
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        title = My title,
        xlabel = {$x$},
        ylabel = {$y$},
        legend entries = {A},
        every node near coord/.append style={anchor=south west},
    ]
    \addplot [nodes near coords, mark = *, blue, point meta = explicit symbolic] %table {Measurements/A.dat};
    table[meta = label] {mydata.dat};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here


Relevant question lies here, however, I would like the labels I write near the coordinates to be separated by a comma, leaving the rest of the plot untouched.

  • I don't see them overlap. – user11232 Apr 17 '15 at 1:26
  • @HarishKumar my apologies, I had wrong data posted. Edited, thanks! – gsamaras Apr 17 '15 at 1:36
3

You can use this answer by percusse and adjust the anchors

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepackage{filecontents}
\begin{filecontents*}{mydata.dat}
  x_0 f(x) label anchor
  %some comment
  0.5 0.24 0  east
  0.5 0.21 0.1 west
  0.5 0.18 0.5 east
  0.5 0.21 0.9 east
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        title = My title,
        xlabel = {$x$},
        ylabel = {$y$},
        legend entries = {A},
        point meta=explicit symbolic,
    ]
    \addplot [nodes near coords, mark = *, blue, point meta = explicit symbolic,visualization depends on={value \thisrow{anchor}\as\myanchor},
       every node near coord/.append style={anchor=\myanchor}] %table {Measurements/A.dat};
    table[meta = label] {mydata.dat};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You have to add an extra column anchor in your data with the anchor values though.

While using north west etc use an extra pair of braces like

\begin{filecontents*}{mydata.dat}
  x_0 f(x) label anchor
  %some comment
  0.5 0.24 0  west
  0.5 0.21 0.1 {north west}
  0.5 0.18 0.5 west
  0.5 0.21 0.9 {south west}
\end{filecontents*}

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.