Tx · LaTeX & TikZ

How to Add a Vertical Line to a TikZ Plot

Updated 13 October 2026

At a glance

  1. 1. Choose the x-value

    Decide where the vertical line should appear in the plot's data coordinates.

  2. 2. Set its endpoints

    Choose the lower and upper y-values that the line should span.

  3. 3. Add a coordinate plot

    Use two coordinates with the same x-value, then set the line's color and style.

  4. 4. Check the axis limits

    Make sure the endpoints fit within the visible plotting range.

If your TikZ figure uses PGFPlots, the simplest way to add a vertical line is to plot two points that share the same x-coordinate. PGFPlots joins the points with a straight segment, so the result is vertical.

The example below adds a dashed red line at x=2x=2, spanning from y=0y=0 to y=5y=5. You can change those three values to position and size the line in your own graph.

Add a vertical line with coordinates

Inside an axis environment, write an \addplot command with a coordinates list:

\addplot[red, dashed, thick] coordinates {(2,0) (2,5)};

The first number in each pair is the x-coordinate, and the second is the y-coordinate. Because both points have x-coordinate 2, the segment joining them is vertical. The y-coordinates determine its lower and upper endpoints.

Place this command inside the axis environment, alongside the other \addplot commands. The line’s position uses the same data-coordinate system as the plotted data, so x=2 means the value 2 on the graph’s x-axis—not a fixed distance from the edge of the page.

Complete compilable example

This standalone example plots y=xy=\sqrt{x} and adds a vertical line at x=2x=2:

\documentclass[tikz,border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  width=8cm,
  height=5cm,
  xmin=0, xmax=4,
  ymin=0, ymax=5,
  xlabel={$x$},
  ylabel={$y$},
  grid=major
]
  \addplot[blue, thick, domain=0:4, samples=100] {sqrt(x)};
  \addplot[red, dashed, thick] coordinates {(2,0) (2,5)};
\end{axis}
\end{tikzpicture}
\end{document}

The pgfplots package provides the axis environment and \addplot command; it is built on TikZ. The compat setting selects a PGFPlots compatibility level. If you use an older PGFPlots installation that does not support version 1.18, change the setting to a version supported by that installation. The xmin, xmax, ymin, and ymax options set the visible range. Here, the vertical segment reaches the bottom and top of the plot because its endpoints match the y-axis limits.

Draw a line using axis coordinates

You can also draw a TikZ path within the axis environment using PGFPlots’ axis cs coordinate system:

\draw[red, dashed, thick] (axis cs:2,0) -- (axis cs:2,5);

This is useful when you want to treat the line as a TikZ drawing rather than as a plotted data series. The axis cs prefix makes the coordinates refer to the graph’s data values. A plain path such as \draw (2,0) -- (2,5); uses TikZ’s ordinary picture coordinates instead, which generally will not correspond to data values on a PGFPlots axis.

For a line that should behave like a plotted series, \addplot coordinates is usually the clearer choice. It fits naturally with other plot commands and can be included in plot-related features such as a legend. A \draw path is a convenient alternative for annotations or custom graphics.

Choose endpoints and style

A vertical line does not have to span the full plot. For example, coordinates {(2,1) (2,3)} draws only the segment from y=1y=1 to y=3y=3. To make a line across the full visible height, use the axis’s ymin and ymax values as its endpoints. If those limits are later changed, update the endpoints too, or the line may stop short of the plot boundary.

Change its appearance using standard plot options: blue or red selects a color, dashed changes the line pattern, and thick increases its width. For instance:

\addplot[black, dotted, very thick] coordinates {(1.5,0) (1.5,4)};

This draws a dotted vertical segment at x=1.5x=1.5. The same approach works at any x-value, including a value that marks a threshold, a particular data point, or a suspected vertical asymptote.

Common mistakes

  • Using different x-coordinates: Points such as (2,0) and (3,5) make a slanted line. Keep the x-coordinate identical for a vertical segment.
  • Putting the command outside the axis: An \addplot coordinate list must be inside the relevant axis environment.
  • Using page coordinates by accident: Within PGFPlots, data values belong in an \addplot coordinate list or in coordinates prefixed by axis cs:.
  • Choosing endpoints outside the visible range: A segment may be clipped at the plot boundary. Check the axis limits and choose endpoints that cover the portion you want to see.
  • Confusing a segment with an infinite line: Two coordinates draw a finite segment, not an infinitely long line. To make it look like a full-height line, use the plot’s lower and upper y-limits as endpoints.

References

  1. PGFPlots package documentation

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.