PGFPlots Basic Setup
Configure PGFPlots in your LaTeX document for optimal results.
Loading PGFPlots
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
% Your plots here
\end{document}The compat setting ensures consistent behavior and enables the latest features. Always use the newest version number.
Compatibility Versions
| Version | Notes |
|---|---|
1.18 | Latest stable (recommended) |
1.17 | Previous stable release |
1.16 | Older stable release |
newest | Always use latest (may change behavior) |
Loading Additional Libraries
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
% Load PGFPlots libraries
\usepgfplotslibrary{fillbetween} % Fill between curves
\usepgfplotslibrary{statistics} % Box plots, histograms
\usepgfplotslibrary{polar} % Polar plots
\usepgfplotslibrary{colorbrewer} % Color schemes
\usepgfplotslibrary{colormaps} % Additional colormaps
% Load TikZ libraries (also used by PGFPlots)
\usetikzlibrary{patterns} % Fill patterns
\usetikzlibrary{arrows.meta} % Arrow stylesGlobal Settings
Set defaults for all plots in your document:
\pgfplotsset{
compat=1.18,
% Default size
width=10cm,
height=7cm,
% Default styling
every axis/.append style={
xlabel style={font=\small},
ylabel style={font=\small},
title style={font=\bfseries},
tick label style={font=\footnotesize},
legend style={font=\footnotesize}
},
% Default grid
grid=major,
grid style={line width=.1pt, draw=gray!30},
major grid style={line width=.2pt, draw=gray!50}
}Plot Dimensions
\begin{axis}[
% Absolute size
width=8cm,
height=6cm,
% Or relative to text width
width=0.8\textwidth,
height=0.5\textwidth,
% Scale only (maintains aspect ratio)
scale=0.8,
% Or scale only one dimension
scale only axis,
width=8cm
]Axis Configuration
\begin{axis}[
% Axis limits
xmin=-5, xmax=5,
ymin=0, ymax=100,
% Axis labels
xlabel={Time ($t$)},
ylabel={Amplitude ($A$)},
% Title
title={Signal Analysis},
% Tick configuration
xtick={-4,-2,0,2,4},
ytick={0,25,50,75,100},
xticklabels={$-4$,$-2$,$0$,$2$,$4$},
% Minor ticks
minor xtick={-3,-1,1,3},
minor ytick={12.5,37.5,62.5,87.5},
% Tick label format
xticklabel style={rotate=45, anchor=east},
yticklabel={\pgfmathprintnumber{\tick}\%}
]Legend Configuration
\begin{axis}[
% Position
legend pos=north west, % or: north east, south west, south east, outer north east
% Custom position
legend style={at={(0.5,-0.15)}, anchor=north},
% Multiple columns
legend columns=2,
% Cell alignment
legend cell align=left,
% Background
legend style={
fill=white,
fill opacity=0.8,
draw=gray,
rounded corners=2pt
}
]
\addplot {x};
\addplot {x^2};
\legend{Linear, Quadratic}Custom Styles
Define reusable styles for consistent plots:
\pgfplotsset{
% Custom axis style
my axis/.style={
width=10cm,
height=6cm,
grid=major,
xlabel={$x$},
ylabel={$f(x)$},
legend pos=north west
},
% Custom plot style
my line/.style={
thick,
mark=*,
mark size=1.5pt
}
}
% Usage
\begin{tikzpicture}
\begin{axis}[my axis, title={My Plot}]
\addplot[my line, blue] {sin(deg(x))};
\addplot[my line, red] {cos(deg(x))};
\end{axis}
\end{tikzpicture}Cycle Lists
Define automatic color and style cycling:
\pgfplotsset{
cycle list={
{blue, mark=*},
{red, mark=square*},
{green!60!black, mark=triangle*},
{orange, mark=diamond*}
}
}
% Or use predefined cycle lists
\begin{axis}[cycle list name=color list]
\addplot {x}; % Uses first style
\addplot {x^2}; % Uses second style
\addplot {x^3}; % Uses third style
\end{axis}Complete Setup Template
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween, statistics}
\pgfplotsset{
compat=1.18,
every axis/.append style={
width=0.9\textwidth,
height=0.6\textwidth,
grid=both,
minor tick num=1,
xlabel style={font=\small},
ylabel style={font=\small},
legend style={
font=\footnotesize,
cells={anchor=west}
}
}
}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\begin{axis}[
title={My Publication-Ready Plot},
xlabel={Independent Variable},
ylabel={Dependent Variable}
]
\addplot[blue, thick] {x^2};
\legend{Data}
\end{axis}
\end{tikzpicture}
\caption{A properly configured plot.}
\end{figure}
\end{document}Next Steps
Start creating plots:
- Line Plots - Function and data line plots
- Scatter Plots - Point data visualization
- Axis Customization - Advanced axis options
PGFPlots Basic Setup
Configure PGFPlots in your LaTeX document for optimal results.
Loading PGFPlots
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
% Your plots here
\end{document}The compat setting ensures consistent behavior and enables the latest features. Always use the newest version number.
Compatibility Versions
| Version | Notes |
|---|---|
1.18 | Latest stable (recommended) |
1.17 | Previous stable release |
1.16 | Older stable release |
newest | Always use latest (may change behavior) |
Loading Additional Libraries
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
% Load PGFPlots libraries
\usepgfplotslibrary{fillbetween} % Fill between curves
\usepgfplotslibrary{statistics} % Box plots, histograms
\usepgfplotslibrary{polar} % Polar plots
\usepgfplotslibrary{colorbrewer} % Color schemes
\usepgfplotslibrary{colormaps} % Additional colormaps
% Load TikZ libraries (also used by PGFPlots)
\usetikzlibrary{patterns} % Fill patterns
\usetikzlibrary{arrows.meta} % Arrow stylesGlobal Settings
Set defaults for all plots in your document:
\pgfplotsset{
compat=1.18,
% Default size
width=10cm,
height=7cm,
% Default styling
every axis/.append style={
xlabel style={font=\small},
ylabel style={font=\small},
title style={font=\bfseries},
tick label style={font=\footnotesize},
legend style={font=\footnotesize}
},
% Default grid
grid=major,
grid style={line width=.1pt, draw=gray!30},
major grid style={line width=.2pt, draw=gray!50}
}Plot Dimensions
\begin{axis}[
% Absolute size
width=8cm,
height=6cm,
% Or relative to text width
width=0.8\textwidth,
height=0.5\textwidth,
% Scale only (maintains aspect ratio)
scale=0.8,
% Or scale only one dimension
scale only axis,
width=8cm
]Axis Configuration
\begin{axis}[
% Axis limits
xmin=-5, xmax=5,
ymin=0, ymax=100,
% Axis labels
xlabel={Time ($t$)},
ylabel={Amplitude ($A$)},
% Title
title={Signal Analysis},
% Tick configuration
xtick={-4,-2,0,2,4},
ytick={0,25,50,75,100},
xticklabels={$-4$,$-2$,$0$,$2$,$4$},
% Minor ticks
minor xtick={-3,-1,1,3},
minor ytick={12.5,37.5,62.5,87.5},
% Tick label format
xticklabel style={rotate=45, anchor=east},
yticklabel={\pgfmathprintnumber{\tick}\%}
]Legend Configuration
\begin{axis}[
% Position
legend pos=north west, % or: north east, south west, south east, outer north east
% Custom position
legend style={at={(0.5,-0.15)}, anchor=north},
% Multiple columns
legend columns=2,
% Cell alignment
legend cell align=left,
% Background
legend style={
fill=white,
fill opacity=0.8,
draw=gray,
rounded corners=2pt
}
]
\addplot {x};
\addplot {x^2};
\legend{Linear, Quadratic}Custom Styles
Define reusable styles for consistent plots:
\pgfplotsset{
% Custom axis style
my axis/.style={
width=10cm,
height=6cm,
grid=major,
xlabel={$x$},
ylabel={$f(x)$},
legend pos=north west
},
% Custom plot style
my line/.style={
thick,
mark=*,
mark size=1.5pt
}
}
% Usage
\begin{tikzpicture}
\begin{axis}[my axis, title={My Plot}]
\addplot[my line, blue] {sin(deg(x))};
\addplot[my line, red] {cos(deg(x))};
\end{axis}
\end{tikzpicture}Cycle Lists
Define automatic color and style cycling:
\pgfplotsset{
cycle list={
{blue, mark=*},
{red, mark=square*},
{green!60!black, mark=triangle*},
{orange, mark=diamond*}
}
}
% Or use predefined cycle lists
\begin{axis}[cycle list name=color list]
\addplot {x}; % Uses first style
\addplot {x^2}; % Uses second style
\addplot {x^3}; % Uses third style
\end{axis}Complete Setup Template
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween, statistics}
\pgfplotsset{
compat=1.18,
every axis/.append style={
width=0.9\textwidth,
height=0.6\textwidth,
grid=both,
minor tick num=1,
xlabel style={font=\small},
ylabel style={font=\small},
legend style={
font=\footnotesize,
cells={anchor=west}
}
}
}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\begin{axis}[
title={My Publication-Ready Plot},
xlabel={Independent Variable},
ylabel={Dependent Variable}
]
\addplot[blue, thick] {x^2};
\legend{Data}
\end{axis}
\end{tikzpicture}
\caption{A properly configured plot.}
\end{figure}
\end{document}Next Steps
Start creating plots:
- Line Plots - Function and data line plots
- Scatter Plots - Point data visualization
- Axis Customization - Advanced axis options