Learn PGFPlots
PGFPlots is a powerful package for creating publication-quality plots and charts directly in your LaTeX documents.
What is PGFPlots?
PGFPlots is built on top of TikZ and provides a high-level interface for creating 2D and 3D plots. It's widely used in academic papers because it produces consistent, professional-looking visualizations that match your document's typography.
Why Use PGFPlots?
- Publication Quality – Generate plots that meet journal standards out of the box.
- Consistent Styling – Fonts and colors match your document automatically.
- Vector Graphics – Plots scale perfectly at any size.
- Data Integration – Import data from CSV files or compute functions directly.
- Reproducibility – Plots are defined in code, not generated externally.
Getting Started
\usepackage{pgfplots}
\pgfplotsset{compat=1.18} % Use latest featuresYour First Plot
\begin{tikzpicture}
\begin{axis}[
title={Simple Function Plot},
xlabel={$x$},
ylabel={$y$},
]
\addplot[blue, thick, domain=-2:2, samples=100] {x^2};
\end{axis}
\end{tikzpicture}Line Plots
\begin{tikzpicture}
\begin{axis}[
title={Multiple Functions},
xlabel={$x$},
ylabel={$f(x)$},
legend pos=north west,
grid=major,
]
\addplot[blue, thick, domain=0:6] {sin(deg(x))};
\addplot[red, thick, domain=0:6] {cos(deg(x))};
\addplot[green!60!black, thick, domain=0:6] {sin(deg(x))*cos(deg(x))};
\legend{$\sin(x)$, $\cos(x)$, $\sin(x)\cos(x)$}
\end{axis}
\end{tikzpicture}Scatter Plots
\begin{tikzpicture}
\begin{axis}[
title={Data Points},
xlabel={Time (s)},
ylabel={Value},
only marks,
]
\addplot coordinates {
(0, 0.5)
(1, 2.1)
(2, 3.8)
(3, 6.2)
(4, 7.9)
(5, 10.1)
};
\end{axis}
\end{tikzpicture}Bar Charts
\begin{tikzpicture}
\begin{axis}[
ybar,
title={Quarterly Sales},
xlabel={Quarter},
ylabel={Sales (\$1000)},
symbolic x coords={Q1, Q2, Q3, Q4},
xtick=data,
nodes near coords,
bar width=20pt,
]
\addplot coordinates {(Q1, 45) (Q2, 62) (Q3, 78) (Q4, 91)};
\end{axis}
\end{tikzpicture}Grouped Bar Charts
\begin{tikzpicture}
\begin{axis}[
ybar,
title={Product Comparison},
xlabel={Category},
ylabel={Score},
symbolic x coords={Speed, Quality, Price},
xtick=data,
legend style={at={(0.5,-0.15)}, anchor=north},
bar width=15pt,
]
\addplot coordinates {(Speed, 85) (Quality, 92) (Price, 78)};
\addplot coordinates {(Speed, 72) (Quality, 88) (Price, 95)};
\legend{Product A, Product B}
\end{axis}
\end{tikzpicture}Importing Data from CSV
% data.csv:
% x,y
% 0,0
% 1,1
% 2,4
% 3,9
\begin{tikzpicture}
\begin{axis}[
title={Data from CSV},
xlabel={$x$},
ylabel={$y$},
]
\addplot table[col sep=comma]{data.csv};
\end{axis}
\end{tikzpicture}Error Bars
\begin{tikzpicture}
\begin{axis}[
title={Measurement with Errors},
xlabel={Sample},
ylabel={Value},
]
\addplot+[
only marks,
error bars/.cd,
y dir=both,
y explicit,
] coordinates {
(1, 2.5) +- (0, 0.3)
(2, 3.1) +- (0, 0.4)
(3, 4.2) +- (0, 0.2)
(4, 3.8) +- (0, 0.5)
};
\end{axis}
\end{tikzpicture}3D Plots
\begin{tikzpicture}
\begin{axis}[
title={3D Surface},
view={60}{30},
colormap/viridis,
]
\addplot3[
surf,
domain=-2:2,
domain y=-2:2,
samples=30,
] {exp(-x^2-y^2)};
\end{axis}
\end{tikzpicture}Logarithmic Axes
\begin{tikzpicture}
\begin{semilogyaxis}[ % or loglogaxis, semilogxaxis
title={Exponential Growth},
xlabel={Time},
ylabel={Population},
grid=major,
]
\addplot[blue, thick, domain=0:10] {exp(x)};
\end{semilogyaxis}
\end{tikzpicture}Customizing Appearance
\begin{tikzpicture}
\begin{axis}[
title style={font=\bfseries\large},
xlabel style={font=\itshape},
ylabel style={font=\itshape},
tick label style={font=\small},
legend style={font=\footnotesize},
grid=both,
grid style={line width=.1pt, draw=gray!20},
major grid style={line width=.2pt, draw=gray!50},
width=10cm,
height=7cm,
]
\addplot[blue, thick] {x^2};
\end{axis}
\end{tikzpicture}Useful Options Reference
| Option | Description |
|---|---|
domain=a:b | Set x range for functions |
samples=n | Number of sample points |
grid=major|minor|both | Show grid lines |
legend pos= | Legend position |
width, height | Plot dimensions |
xmin, xmax, ymin, ymax | Axis limits |
Next Steps
- Learn TikZ – Create custom diagrams
- Learn LaTeX Basics – Review fundamentals
Generate plots with AI
Describe your data visualization and let Octree's AI generate the PGFPlots code for you.
Try Octree Free →