TikZ and PGFPlots¶
Prerequisites: mathematics and tables and figures. Goal: create an original vector diagram and a labelled function plot.
Choose the right tool¶
TikZ describes shapes, nodes, and paths. PGFPlots builds on TikZ to provide axes, coordinates, legends, and data plotting. A diagram communicates relationships; a plot communicates quantitative relationships. Both need an explanation in ordinary text.
Complete example¶
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows.meta}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\section{A process diagram}
\begin{center}
\begin{tikzpicture}[
process node/.style={draw,rounded corners,minimum width=24mm,minimum height=10mm},
node distance=12mm,>=Stealth]
\node[process node] (source) {Source};
\node[process node,right=of source] (build) {Build};
\node[process node,right=of build] (pdf) {PDF};
\draw[->] (source) -- (build);
\draw[->] (build) -- (pdf);
\end{tikzpicture}
\end{center}
The arrows show a left-to-right transformation, not a feedback loop.
\section{A function plot}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
width=0.85\linewidth,
xlabel={$x$},ylabel={$f(x)$},
domain=0:2,samples=41,grid=major,
legend pos=north west]
\addplot[blue,thick,mark=none] {x^2};
\addlegendentry{$x^2$}
\addplot[black,dashed,thick] {x};
\addlegendentry{$x$}
\end{axis}
\end{tikzpicture}
\end{center}
The curves intersect at \(x=0\) and \(x=1\). Line styles distinguish
the curves even without color.
\end{document}
The positioning library enables right=of source.
arrows.meta provides configurable arrow tips.
A named style such as process node/.style={...} keeps related nodes consistent
without reusing TikZ's existing step key.
For the plot, compat=1.18 requests a documented PGFPlots behavior level.
It is not a package version installer. Use a distribution containing that
compatibility level or consult your installed manual.
Compile and expected output¶
From the repository root, enter that folder and open build/main.pdf.
Expect a Source ? Build ? PDF diagram and a plot of \(x^2\) and \(x\) on
\(0\leq x\leq2\). They intersect at \(x=0\) and \(x=1\).
The dashed line remains distinguishable from the solid curve in grayscale.
Plot data without rewriting it¶
For measured or tabulated data, prefer a CSV file to copying points into several places. The laboratory report reads one CSV for both its table and its plotted points. Label the axes with quantities and units, and explain whether values are observed, simulated, or synthetic.
Large plots can slow compilation. Simplify a problematic example first. TikZ externalization is an advanced optimization that may require shell-escape; none of this platform's examples needs unrestricted shell execution.
Practice¶
Change the plot domain to -1:2. Predict the behavior before compiling:
\(x^2\) stays nonnegative while \(x\) becomes negative to the left of zero.
Move the PDF node below the Build node and reroute the arrow.
Check: axis labels, legend entries, and explanatory text still match the new graph. The diagram does not rely on arrow color alone.
Next: laboratory report project.