Skip to main content
To plot a function or dataset directly in LaTeX, load \usepackage{pgfplots}, set \pgfplotsset{compat=1.18} in the preamble, and place \addplot commands inside an axis environment wrapped in tikzpicture. pgfplots loads tikz automatically and compiles publication-ready vector plots without an external plotting tool. For general diagrams, flowcharts, and geometric drawings rather than coordinate axes, see Creating diagrams with TikZ.

Step 1: Plot functions inside an axis environment

Set \pgfplotsset{compat=1.18} in your preamble. TeX Live 2026 ships pgfplots 1.18.2, where 1.18 is the highest numbered compatibility level; pinning 1.18 enables modern axis label placement, suppresses the backward-compatibility warning, and keeps existing plots from shifting if pgfplots is upgraded later (prefer pinning a version number over compat=newest). Inside \begin{axis}[...], each \addplot[style] {expression}; draws one curve over domain=min:max using samples points (default 25; raise samples for curved functions). End every \addplot statement with a semicolon ;, and add legend labels with \addlegendentry{...} or a single \legend{a, b}.
pgfplots-function.tex
Ready to use this syntax? Continue in the browser editor when you want to adapt the example in a real project.Open LaTeX Cloud Studio

Step 2: Plot data from an inline table

You can plot measured data without external files by passing an inline table directly to \addplot table. Give the columns header names on the first line and select which columns map to x and y with table[x=epoch, y=train].
pgfplots-data.tex

Step 3: Combine a function with scatter coordinates and radians

pgfplots evaluates trigonometric functions such as sin(x) and cos(x) in degrees by default. When your xx-axis is in radians, wrap the argument in deg(x)—for example sin(deg(x)). To overlay discrete data points on top of a curve, use \addplot[only marks, mark=*] coordinates { (x1,y1) (x2,y2) ... };.
pgfplots-trig-scatter.tex

Common axis and \addplot options

Common mistakes

  • Missing semicolon after \addplot: Every \addplot command ends with ;. Omitting it triggers a confusing TikZ error on the following line.
  • Omitting \pgfplotsset{compat=1.18}: Without compat, pgfplots falls back to legacy defaults and prints a compatibility warning in the log.
  • Plotting sin(x) over 0:6.28 without deg(x): Because pgfplots expects degrees, sin(6.28) evaluates at 6.28° instead of 2π2\pi radians, producing a nearly straight line.
  • Leaving samples at the default 25 on oscillating curves: Increase samples (for example samples=100 or 120) for smooth peaks and troughs.

Try it in the editor

pgfplots is installed in LaTeX Cloud Studio’s TeX Live 2026 environment. Start from the Lab Report template in LaTeX Cloud Studio and replace main.tex with the complete example.