3

I am trying to draw a line between two x-values using \draw[|-|], and I want the endpoints to exactly align with the x-axis ticks (e.g., at x = -1 and x = 1). However, in the output below, the bar visually does not align perfectly with the ticks.

enter image description here

\documentclass{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture} \begin{axis}[ axis lines=middle, xmin=-2, xmax=2, ymin=0, ymax=1, enlargelimits, every x tick/.style={color=black, thick}, ] \draw[|-|,thick] (axis cs:-1,0.05) -- (axis cs:1,0.05); \end{axis} \end{tikzpicture} \end{document} 
6
  • 1
    You can shorten the arrow tips by minus half a line width probably. Commented May 30 at 14:28
  • @JasperHabicht What I’m really trying to understand is why axis cs:-1,0.05 and axis cs:1,0.05 don’t align precisely with the ticks. I expected them to match exactly without needing to manually adjust or shorten the arrow tips. Commented May 30 at 14:34
  • This is exactly the case when our vision deceives us. This is demonstrated by these two commands: \draw[thick,red] (axis cs:-1,0.05) -- (axis cs:1,0.05); \draw[thick,red] (axis cs:-1,0.0) -- (axis cs:1,0.0); Try it yourself. Commented May 30 at 14:37
  • @kabenyuk Try this instead \draw[|-|,thick,red] (axis cs:-1,0.0) -- (axis cs:1,0.0); Even here, the vertical bars don’t align exactly with the ticks, which is exactly the issue I’m trying to understand. Commented May 30 at 14:47
  • 1
    What I wanted to explain is that the vertical bars at the end of the line are arrow tips which are designed in such a way that they will end at the exact coordinate. This means that the line width of the left arrow tip only extends to the right (and that of the right arrow tip only to the left). But the ticks are centered in the same coordinate, so half of the line width of the tick line extends before and the other half behind this coordinate. This is why it does not fit. Therefore, you need to shift the arrow tips outward half of the line width. Commented May 30 at 14:56

1 Answer 1

6

The reason for the "issue" is that if you use |-| to draw bars at both ends of a line, these bars are actually arrow tips and these are designed in such a way that the line width will never extend further than the coordinate of the relevant end point of the line.

In contrast, the tick lines are centered at the respective coordinates and the line width extends to both sides of the relevant coordinate. Thus, the tick lines and the lines created as arrow tips are not aligned.

See the following example (zoom in to see exactly what is going on):

\documentclass[border=10pt]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture} \begin{axis}[ axis lines=middle, xmin=-2, xmax=2, ymin=0, ymax=1, enlargelimits, every x tick/.style={color=black, thick}, ] \fill[red, opacity=0.5] (-1,-1) rectangle (-2,1); \draw[|-|, thick] (axis cs:-1,0.05) -- (axis cs:1,0.05); \end{axis} \end{tikzpicture} \end{document} 

zoomed part of the output of above code


To solve this, you could "shorten" the line with the arrow tips by minus half of the line width:

\documentclass[border=10pt]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture} \begin{axis}[ axis lines=middle, xmin=-2, xmax=2, ymin=0, ymax=1, enlargelimits, every x tick/.style={color=black, thick}, ] \fill[red, opacity=0.5] (-1,-1) rectangle (-2,1); \draw[|-|, thick, shorten >=-0.5\pgflinewidth, shorten <=-0.5\pgflinewidth] (axis cs:-1,0.05) -- (axis cs:1,0.05); \end{axis} \end{tikzpicture} \end{document} 

zoomed part of the output of above code

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.