按照手册和其他地方的示例,我可以使用 Tikz 在 pgfplots 轴上标记交叉点,甚至显示坐标(参见下面 MWE 中注释掉的部分),但我宁愿使用 pgfplots 来\addplot
标记交叉点,以实现样式一致性和轻松的图例集成。如何以编程方式存储和调用交叉点的坐标以与 一起使用\addplot
?尽管我尽力了,但我无法在命令\pgfplotspointgetcoordinates
之外工作\path[name intersections...]
。
在下面的例子中,我们有“未排名”数据点的纵坐标(即失败率),但我们不知道横坐标。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{intersections}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
width=6cm,
xlabel=Quartile,
xtick distance=1,
ylabel=Fail rate (\%),
every axis plot/.append style={very thick, mark=*},
cycle list={[samples of colormap=4 of viridis]},
]
\addplot+ [name path=main, forget plot] table [x=quartile, y=rate] {
quartile rate
1 4.5
2 2
3 1
4 0.5
};
\def\yextra{1.7}
\path [name path=helperline] (0,\yextra -| current axis.west) -- (0,\yextra -| current axis.east);
% \path [name intersections={of=main and helperline, name=int}]
% node [red, circle, fill, minimum size=2pt,
% pin={
% \pgfplotspointgetcoordinates{(int-1)}
% $(\pgfmathprintnumber[fixed]{
% \pgfkeysvalueof{/data point/x}},
% \pgfmathprintnumber[fixed]{
% \pgfkeysvalueof{/data point/y}})$
% },
% ] (A) at (int-1) {}; % This works, but is not what I want
% \pgfplotspointgetcoordinates{(int-1)} % doesn't work
% \pgfplotspointgetcoordinates{(A)} % doesn't work
% % Below is what I want, but programmatic instead of manual
\addplot+ [mark options={fill=white}, only marks] coordinates {(2.3,1.7)};
\addlegendentry{unranked}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}