pgfplots:将自定义注释与刻度线精确对齐

pgfplots:将自定义注释与刻度线精确对齐

考虑以下 pgfplots 图骨架:

\documentclass[10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width=10cm, height=6cm,
    xmin=0,     xmax=780,
    ymin=.89,   ymax=1.01,
    xtick={52,183,425},
    ytick={.95,.975,.9875},
    axis x line=bottom,
    axis y line=left,
    axis line style={draw=none},
    tick align=outside
]
\begin{scope}[style=/pgfplots/every tick]
\draw (52,  0) -- (52,  .95)    -- (0, .95)    ;
\draw (183, 0) -- (183, .975)   -- (0, .975)   ;
\draw (425, 0) -- (425, .9875)  -- (0, .9875)  ;
\end{scope}
% \addplot ... ;
\end{axis}
\end{tikzpicture}
\end{document}

目标是用参考线(而不是网格)来吸引人们的注意力,让其看到实际数据与特定 (x,y) 阈值相交的位置。这种方法很有效,但有一个视觉缺陷:参考线与刻度线略有错位:

上述 MWE 的渲染

我需要做什么才能使\draw命令绘制出与刻度线精确对齐的线条?

答案1

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\newcommand{\drawtickgrid}[2]{
\draw[/pgfplots/every tick, shorten >=-0.15cm, shorten <=-0.15cm, draw] 
  (#1,\pgfkeysvalueof{/pgfplots/ymin}) |- (\pgfkeysvalueof{/pgfplots/xmin},#2);
}
\begin{axis}[
width=10cm, height=6cm,
xmin=0, xmax=780,
ymin=.89, ymax=1.01,
xtick={52,183,425},
ytick={0.95,0.975,0.9875},
axis x line=bottom,
axis y line=left,
axis line style={draw=none},
tick align=outside,
tick style={draw=none},
clip=false,
]
\drawtickgrid{52}{0.95}
\drawtickgrid{183}{0.975}
\drawtickgrid{425}{0.9875}
\end{axis}
\end{tikzpicture}
\end{document}

轴之间有三条斜线的图表

相关内容