2

Consider the following pgfplots plot skeleton:

\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}

The goal is to have, instead of a grid, guide lines drawing the eye to where the actual data crosses certain (x,y) thresholds. It works, except there's a visual glitch: the guide lines are slightly misaligned with the tick lines:

rendering of above MWE

What do I need to do to make the \draw commands draw lines that are precisely aligned with the tick lines?

6
  • 1
    I can not reproduce your problem. The picture you show is not from your code. The axis:cs is not default in old PGFPlots versions, so you must correctly be setting compat level. Feb 29 at 17:37
  • @hpekristiansen Whoops, sorry, deleted a few too many things from the preamble. Should be fixed now.
    – zwol
    Feb 29 at 18:02
  • Your code does not compile. -and I can still not reproduce your result. It could be a rounding error only visible at a specific zoom level and viewer. Feb 29 at 18:06
  • Compile error should be fixed. The effect is visible for me at any zoom level in two different PDF viewers (Firefox's built-in PDF renderer, and Inkscape).
    – zwol
    Feb 29 at 18:21
  • It is never a good idea to have breaks in lines or fills because of rendering errors - either rounding or artefacts because of antialiasing. -dependent on zoom level and viewer. You can draw everything in one line, if you use clip=false to show the bit outside the axis. You do not show axis, so maybe you do not even need that!? Feb 29 at 18:36

1 Answer 1

4
\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}

Graph with three angled lines between axis

2
  • This looks promising, especially the \drawtickgrid command. I will have to see if I can find another way of clipping out an undesired piece of the actual data than by setting the axis limits to exclude it and relying on clip=true ... but I need to do that anyway for something else, so.
    – zwol
    Feb 29 at 18:46
  • You could also just drop the part that sticks out of the (non shown) axis. Remove shorten >, shorten < and clip=true. If needed, you could move the tick labels closer to the axis. Feb 29 at 18:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .