[Tex/LaTex] pgfplots: Pointwise Positioning of Explicit Point Meta

nodespgfplots

I am drawing several diagrams like the following one:

enter image description here

I would like to label the important points with $P_1$, $P_2$ and so on. The problem is that there is no global position for the nodes that fits for all.

I don't want that the label to cover the diagram line.

enter image description here

  • Is there a clever way to avoid that?
  • Can I have a fourth column (s, F, Label and Position) with N(orth), W(est), S(outh) and E(ast) for example?
  • Have you a good idea?

Here's my code so far:

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    axis lines = middle,
    enlargelimits = true,
    xlabel = {Travel $s$ in mm},
    ylabel = {Force $F$ in N},
    width =120mm,
    height= 80mm,
    title = {Force-Travel-Diagram},
]
\addplot[
    line width=1pt,
    mark=*,
    x=s,
    y=F,
    nodes near coords,
    point meta=explicit symbolic,
    nodes={font=\small},
    nodes near coords align={anchor=west},
] table
[
row sep=\\,
meta=Label
] 
{
s F Label\\
0 0 {$P_0$}\\
0.03 2 {$P_1$}\\
0.7 6 {$P_2$}\\
0.71 5 {$P_3$}\\
1.4 12 {$P_4$}\\
};
\end{axis}
\end{tikzpicture}

\end{document}

Best Answer

You can use

visualization depends on={value \thisrow{anchor}\as\myanchor},,
every node near coord/.append style={font=\small,anchor=\myanchor}

and add a fourth column with anchors.

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    axis lines = middle,
    enlargelimits = true,
    xlabel = {Travel $s$ in mm},
    ylabel = {Force $F$ in N},
    width =120mm,
    height= 80mm,
    title = {Force-Travel-Diagram},
]
\addplot[
    line width=1pt,
    mark=*,
    x=s,
    y=F,
    nodes near coords,
    point meta=explicit symbolic,
    visualization depends on={value \thisrow{anchor}\as\myanchor},,
    every node near coord/.append style={font=\small,anchor=\myanchor}
    %nodes={font=\small},
%    nodes near coords align={anchor=west},
] table
[
row sep=\\,
meta=Label
]
{
s F Label anchor\\
0 0 {$P_0$} {south west}\\
0.03 2 {$P_1$} south\\
0.7 6 {$P_2$} south\\
0.71 5 {$P_3$} north\\
1.4 12 {$P_4$} west\\
};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Remember that you can also use angles as anchors. For example,

s F Label anchor\\
0 0 {$P_0$} 120\\
0.03 2 {$P_1$} 270\\
0.7 6 {$P_2$} 270\\
0.71 5 {$P_3$} 90\\
1.4 12 {$P_4$} 180\\

works, where anchor is specified with angles giving total flexibility.