
我的問題是如何指定圖中節點的位置?這是一個 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}
在此範例中,我的文字按行重疊。所有節點的位置都是這樣設定的:
{xshift=15pt,yshift=8pt}
但這並不適用於所有節點。所以我想修改每個節點的位置。 ChatGPT 建議這樣做,但它不起作用:
您可以透過在每個座標上新增「node[pos]」選項來修改每個節點的位置,其中「pos」是介於0 和1 之間的值,指定節點沿連接座標與其標籤的直線的位置。例如,要將第一個座標的節點向右移動,可以設定“node[pos=0.5]”而不是“靠近座標的節點”,並相應地調整“xshift”和“yshift”值。
先致謝!
答案1
您可以使用該選項coordinate style
根據特定條件修改某些標籤的樣式(當然您也可以使用此機制為特定座標處的標籤設定xshift
或):yshift
\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}