Skip to main content
Planned maintenance impacting Stack Overflow and all Stack Exchange sites is scheduled for Monday, September 16, 2024, 5:00 PM-10:00 PM EDT (Monday, September 16, 21:00 UTC- Tuesday, September 17, 2:00 UTC). The email/password authentication method will be unavailable for logging in and registering. Read more here

{pgfplots} is a package for creating 2D and 3D plots of mathematical functions and numerical data, using the PGF graphics framework. For questions about the inbuilt plotting functionality of TikZ/PGF, use {tikz-pgf} instead.

Questions tagged with are about the plotting package of the same name, which uses the PGF graphics framework to create 2D and 3D plots of mathematical functions or numerical data.

The package supports the creation of standard plot types such as scatter plots, line plots, bar/column plots, area plots, and mesh/surface plots, but also of more exotic ones such as comb plots or piecewise constant plots.

The coordinates for the plots can be supplied in the form of mathematical functions using the mathematical engine provided by the PGF framework, by providing coordinates directly in the document, or by reading datafiles from the file system.

Plots created with pgfplots are vector graphics and can thus be scaled without loss of quality. Titles and labels are set as normal text and thus use the same font as the rest of the document, leading to a homogeneous appearance.

A simple example, plotting the function f(x)=x3:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
  \begin{axis}
  \addplot {x^3}; 
\end{axis}
\end{tikzpicture}
\end{document}

plot of f(x)=x^3 using pgfplots

A more advanced example of a bar chart with varying bar widths (taken from the pgfplots manual):

\documentclass{article}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar interval=0.9,
    x tick label as interval,
    xmin=2003,xmax=2030,
    ymin=0,ymax=140,
    xticklabel={
       $\pgfmathprintnumber{\tick}$
    -- $\pgfmathprintnumber{\nexttick}$},
    xtick=data,
    x tick label style={
        rotate=90,anchor=east,
        /pgf/number format/1000 sep=}
]

    \addplot[draw=blue,fill=blue!40!white]
        coordinates
        {(2003,40) (2005,100) (2006,15) 
         (2010,90) (2020,120) (2030,3)};
\end{axis}
\end{tikzpicture}
\end{document}

pgfplots ybar interval

An example of a 3D plot of the function f(x,y)=sin(x)*sin(y) (taken from the pgfplots manual). Note that trigonometric functions in PGF use degrees instead of radians.

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}
  \addplot3[surf,domain=0:360,samples=40] {sin(x)*sin(y)};
\end{axis}
\end{tikzpicture}
\end{document}

pgfplots 3D plot