
次のような図をいくつか描いています。
$P_1$
重要な点には、$P_2$
などのラベルを付けたいと思います。問題は、node
すべてに適合する のグローバル位置が存在しないことです。
ラベルが図の線を覆ってしまうのは避けたいです。
- それを避ける賢い方法はあるでしょうか?
- いただけますか4列目(s、F、ラベル、位置) を、たとえば N(北)、W(東)、S(南)、E(東) で表すとどうなりますか?
- 何かいいアイデアはありますか?
これまでのコードは次のとおりです。
\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}
答え1
使用できます
visualization depends on={value \thisrow{anchor}\as\myanchor},,
every node near coord/.append style={font=\small,anchor=\myanchor}
アンカー付きの 4 番目の列を追加します。
\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}
角度をアンカーとして使用することもできます。たとえば、
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\\
アンカーが角度で指定され、完全な柔軟性が与えられる場合に機能します。