How to Make a PGFPlots Function Touch the Plot Edges
At a glance
- 1. Set axis limits
Specify xmin, xmax, ymin, and ymax so the plot rectangle has the boundaries you intend.
- 2. Set the function domain
Make the addplot domain match the x-axis limits to draw the curve up to both vertical edges.
- 3. Match function values
For the curve to touch a horizontal edge, the function must attain that y limit somewhere in the domain.
- 4. Check clipping and samples
Disable limit enlargement and use enough samples for a smooth curve.
To make a PGFPlots curve reach the boundary of the plotting area, explicitly choose the axis limits and the range over which the function is plotted. Matching the function’s domain to the x-axis limits makes the curve extend to the left and right edges. To touch the top or bottom edge too, the function must attain the corresponding y-axis limit.
Set the axis limits and domain
PGFPlots distinguishes the range of x-values used to calculate a curve from the visible range of the axes. Set both deliberately: use domain in \addplot for the function’s x-range, and xmin, xmax, ymin, and ymax in the axis options for the plot rectangle.
Here is a complete example. The curve runs from to , and the visible y-range is to . It therefore meets the left and right edges at and , the top edge at those same points, and the bottom edge at .
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-2, xmax=2,
ymin=0, ymax=4,
enlargelimits=false,
axis lines=box,
]
\addplot[blue, thick, domain=-2:2, samples=101] {x^2};
\end{axis}
\end{tikzpicture}
\end{document}
The standalone class and pgfplots package are the only required packages. The compat setting selects a defined PGFPlots behavior; use a version supported by your installation if it predates 1.18. enlargelimits=false prevents automatic expansion of the specified axis limits. samples=101 gives the function enough evaluation points to draw a smooth-looking curve; it does not set the axis boundaries.
Which edges can the function reach?
For a function on the interval , the curve reaches the left and right plot edges when the x-axis limits are and , and the domain includes those endpoints. Its y-values at the edges are and .
The curve touches the top edge only if there is some in the domain for which . It touches the bottom edge only if for some x. Setting a y limit does not force the function to reach it; the function and the chosen limit must agree.
For example, to make a sine curve meet the left and right edges of an interval from to , use xmin=0, xmax=2*pi and domain=0:2*pi. PGF’s sin function takes its argument in degrees, so plot sin(deg(x)) for a sine curve parameterized in radians. If you also set ymin=-1, ymax=1, the curve meets the bottom and top edges at its minimum and maximum. If the y limits were instead and , it would touch only the vertical edges.
When automatic limits cause a gap
If you omit axis limits, PGFPlots chooses them from the plotted data and may add space around the data. That can make the curve look as if it stops short of the frame. To control the visible rectangle, specify limits explicitly and use enlargelimits=false.
For instance, this does not ensure that the curve reaches the right edge:
\addplot[domain=-2:1] {x^2};
If the axis has xmax=2, the function stops at , leaving a gap to the right boundary. Extend the domain to -2:2 if the intended curve should reach that edge.
Common mistakes
- Changing only the axis limits: A wider axis does not extend a curve whose
domainends earlier. - Changing only the domain: A longer domain does not guarantee the visible axis includes the endpoints; set
xminandxmaxas needed. - Expecting the curve to fill the y-range: A function reaches a horizontal boundary only where its value equals that y limit.
- Using too few samples: The endpoints may be correct, but a curve with few sampled points can look angular. Increase
samplesfor smooth curves. - Confusing the plot rectangle with the outer figure: Tick labels and axis labels sit outside the plotting area. The curve should meet the inner axis rectangle, not the outer edge of the complete figure.
PGFPlots clips plotted material to the axis rectangle by default. A thick line drawn exactly to a boundary may appear slightly cut off because part of its stroke lies outside the clipping area; that is a drawing effect, not a gap in the function’s domain. Usually the curve should remain clipped so it does not spill beyond the axes. Consider clip=false only if you specifically want marks or strokes to extend outside the plot rectangle.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.