PGFPlots for Beginners
A step-by-step guide to creating your first plots with PGFPlots in LaTeX.
The Basic Structure
Every PGFPlots graphic follows this structure:
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{tikzpicture}
\begin{axis}[options]
\addplot {expression or data};
\end{axis}
\end{tikzpicture}Your First Plot
Let's plot a simple function:
\begin{tikzpicture}
\begin{axis}
\addplot {x^2};
\end{axis}
\end{tikzpicture}This creates a basic plot of x² with automatic axis scaling.
Adding Labels and Title
\begin{tikzpicture}
\begin{axis}[
title={Quadratic Function},
xlabel={$x$},
ylabel={$y = x^2$}
]
\addplot {x^2};
\end{axis}
\end{tikzpicture}Styling Your Plot
\begin{tikzpicture}
\begin{axis}[
title={Styled Plot},
xlabel={$x$},
ylabel={$f(x)$},
grid=major, % Add grid lines
legend pos=north west % Legend position
]
\addplot[blue, thick] {x^2};
\addplot[red, dashed] {x^3};
\legend{$x^2$, $x^3$}
\end{axis}
\end{tikzpicture}Plotting Data Points
Plot specific coordinates instead of functions:
\begin{tikzpicture}
\begin{axis}[
title={Data Points},
xlabel={Time (s)},
ylabel={Value}
]
\addplot coordinates {
(0, 0)
(1, 2)
(2, 4)
(3, 8)
(4, 16)
};
\end{axis}
\end{tikzpicture}Line Styles and Markers
\begin{tikzpicture}
\begin{axis}
% Line with circle markers
\addplot[blue, mark=o] coordinates {(0,0) (1,1) (2,4)};
% Dashed line with square markers
\addplot[red, dashed, mark=square] coordinates {(0,0) (1,2) (2,3)};
% Only markers, no line
\addplot[only marks, mark=*] coordinates {(0,1) (1,3) (2,2)};
% Thick smooth line
\addplot[green, thick, smooth] {sin(deg(x))};
\end{axis}
\end{tikzpicture}Setting Axis Limits
\begin{tikzpicture}
\begin{axis}[
xmin=-5, xmax=5, % X axis range
ymin=0, ymax=25, % Y axis range
xtick={-4,-2,0,2,4}, % Custom tick marks
ytick={0,5,10,15,20,25}
]
\addplot {x^2};
\end{axis}
\end{tikzpicture}Multiple Plots
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$f(x)$},
legend pos=south east
]
\addplot[blue, thick] {sin(deg(x))};
\addplot[red, thick] {cos(deg(x))};
\addplot[green, thick, dashed] {sin(deg(x)) + cos(deg(x))};
\legend{$\sin(x)$, $\cos(x)$, $\sin(x)+\cos(x)$}
\end{axis}
\end{tikzpicture}domain=-5:5 in the axis options or addplot to specify the x-range for function plots.Complete Beginner Example
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture}
\begin{axis}[
title={My First PGFPlots Graph},
xlabel={Time (hours)},
ylabel={Temperature ($^\circ$C)},
xmin=0, xmax=24,
ymin=10, ymax=30,
grid=both,
legend pos=north west,
width=10cm,
height=6cm
]
\addplot[blue, thick, mark=*] coordinates {
(0, 15) (4, 12) (8, 14) (12, 25)
(16, 28) (20, 22) (24, 16)
};
\legend{Temperature}
\end{axis}
\end{tikzpicture}
\caption{Daily temperature variation}
\end{figure}
\end{document}Common Beginner Mistakes
- Missing compat setting: Always include
\pgfplotsset{compat=1.18} - Forgetting tikzpicture: The axis environment must be inside tikzpicture.
- Incorrect function syntax: Use
sin(deg(x))notsin(x)for trig functions. - Missing semicolons: Each
\addplotcommand ends with a semicolon.
Next Steps
Continue learning PGFPlots:
- Basic Setup - Configuration options
- Line Plots - Detailed line plot tutorial
- Scatter Plots - Visualize data points
PGFPlots for Beginners
A step-by-step guide to creating your first plots with PGFPlots in LaTeX.
The Basic Structure
Every PGFPlots graphic follows this structure:
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{tikzpicture}
\begin{axis}[options]
\addplot {expression or data};
\end{axis}
\end{tikzpicture}Your First Plot
Let's plot a simple function:
\begin{tikzpicture}
\begin{axis}
\addplot {x^2};
\end{axis}
\end{tikzpicture}This creates a basic plot of x² with automatic axis scaling.
Adding Labels and Title
\begin{tikzpicture}
\begin{axis}[
title={Quadratic Function},
xlabel={$x$},
ylabel={$y = x^2$}
]
\addplot {x^2};
\end{axis}
\end{tikzpicture}Styling Your Plot
\begin{tikzpicture}
\begin{axis}[
title={Styled Plot},
xlabel={$x$},
ylabel={$f(x)$},
grid=major, % Add grid lines
legend pos=north west % Legend position
]
\addplot[blue, thick] {x^2};
\addplot[red, dashed] {x^3};
\legend{$x^2$, $x^3$}
\end{axis}
\end{tikzpicture}Plotting Data Points
Plot specific coordinates instead of functions:
\begin{tikzpicture}
\begin{axis}[
title={Data Points},
xlabel={Time (s)},
ylabel={Value}
]
\addplot coordinates {
(0, 0)
(1, 2)
(2, 4)
(3, 8)
(4, 16)
};
\end{axis}
\end{tikzpicture}Line Styles and Markers
\begin{tikzpicture}
\begin{axis}
% Line with circle markers
\addplot[blue, mark=o] coordinates {(0,0) (1,1) (2,4)};
% Dashed line with square markers
\addplot[red, dashed, mark=square] coordinates {(0,0) (1,2) (2,3)};
% Only markers, no line
\addplot[only marks, mark=*] coordinates {(0,1) (1,3) (2,2)};
% Thick smooth line
\addplot[green, thick, smooth] {sin(deg(x))};
\end{axis}
\end{tikzpicture}Setting Axis Limits
\begin{tikzpicture}
\begin{axis}[
xmin=-5, xmax=5, % X axis range
ymin=0, ymax=25, % Y axis range
xtick={-4,-2,0,2,4}, % Custom tick marks
ytick={0,5,10,15,20,25}
]
\addplot {x^2};
\end{axis}
\end{tikzpicture}Multiple Plots
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$f(x)$},
legend pos=south east
]
\addplot[blue, thick] {sin(deg(x))};
\addplot[red, thick] {cos(deg(x))};
\addplot[green, thick, dashed] {sin(deg(x)) + cos(deg(x))};
\legend{$\sin(x)$, $\cos(x)$, $\sin(x)+\cos(x)$}
\end{axis}
\end{tikzpicture}domain=-5:5 in the axis options or addplot to specify the x-range for function plots.Complete Beginner Example
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture}
\begin{axis}[
title={My First PGFPlots Graph},
xlabel={Time (hours)},
ylabel={Temperature ($^\circ$C)},
xmin=0, xmax=24,
ymin=10, ymax=30,
grid=both,
legend pos=north west,
width=10cm,
height=6cm
]
\addplot[blue, thick, mark=*] coordinates {
(0, 15) (4, 12) (8, 14) (12, 25)
(16, 28) (20, 22) (24, 16)
};
\legend{Temperature}
\end{axis}
\end{tikzpicture}
\caption{Daily temperature variation}
\end{figure}
\end{document}Common Beginner Mistakes
- Missing compat setting: Always include
\pgfplotsset{compat=1.18} - Forgetting tikzpicture: The axis environment must be inside tikzpicture.
- Incorrect function syntax: Use
sin(deg(x))notsin(x)for trig functions. - Missing semicolons: Each
\addplotcommand ends with a semicolon.
Next Steps
Continue learning PGFPlots:
- Basic Setup - Configuration options
- Line Plots - Detailed line plot tutorial
- Scatter Plots - Visualize data points