Plot mathematical functions in LaTeX with pgfplots and TikZ.
You can draw clean, vector-based graphs directly in your document using the pgfplots package or raw TikZ. Define your domain, set the number of samples, and style your axes without exporting images from external tools.
The short answer: load \usepackage{pgfplots}, wrap your graph in an axis environment, and use \addplot with a mathematical expression and a domain.
Why pgfplots is better than raw TikZ for functions
The PGF manual documents how to draw plots with the basic \path operation, but doing so forces you to draw every gridline, label, and tick mark by hand. The pgfplots package builds on top of TikZ to handle axes, ticks, domains, and legends automatically.
Using pgfplots keeps your font sizes consistent with the rest of your paper, avoids scaling mismatches when your document layout changes, and gives you a familiar axis environment.
Plotting a basic function from an expression
To plot a function, load the pgfplots package in your preamble, create a tikzpicture environment, and insert an axis environment. Inside the axis, use \addplot with a domain option to specify your input range.
Remember that trigonometric functions in PGF expect degrees by default. If your variable is in radians, convert it with {deg(x)} or load radians explicitly.
Step-by-step example with customized axes and styling
Fixing rough curves with samples and smooth
By default, pgfplots evaluates 25 sample points across your domain, which makes curves look angular and jagged. Increase the sample count and add the smooth option to interpolate between points for a clean appearance.
Troubleshooting common plotting errors
Undefined control sequence in math expression
If the compiler complains about an unknown function name, check your syntax. PGF uses \sin and \cos, but trigonometric arguments need degree conversion unless you configure radians explicitly.
\addplot[domain=0:360] {sin(x)};Compile timeout on complex functions
A function with thousands of samples or heavy trigonometric calculations can hit compile limits on slower online editors. Texeno compiles most documents in 1 to 3 seconds and allows up to 8 minutes on paid plans with no compile quota.
Unexpected asymptotes or vertical spikes
Functions like \tan(x) have vertical asymptotes where values shoot to infinity, creating ugly horizontal lines across your graph. Split your domain into intervals around the singularities instead of plotting across them in one continuous pass.
Questions
Should I use TikZ or pgfplots for function graphs?
Use pgfplots. It handles axis ticks, labels, scaling, and bounding boxes automatically, whereas raw TikZ requires you to draw every gridline and coordinate by hand.
How do I fix jagged or angular curves in my plot?
Add samples=200 and the smooth option to your \addplot command. This increases the point density and interpolates a soft curve between them.
How do I plot trigonometric functions in radians instead of degrees?
Wrap your variable in deg(x) inside trigonometric functions, such as sin(deg(x)), because PGF evaluates angle arguments in degrees by default.
Try your own paper in Texeno.
Start free, upload a project as a .zip and press Recompile. Upgrade when you need longer compiles.
Start writing