Master Beautiful LaTeX Graphics: Functions, Charts, and Geometry with TikZ
This tutorial shows how to use LaTeX's TikZ and related packages to create high‑quality function plots, statistical charts, and geometric figures that integrate seamlessly with document text, offering step‑by‑step code examples and visual results.
We can not only typeset beautiful documents with LaTeX, but also draw stunning graphics; LaTeX is a powerful drawing tool. This article explains how to create function plots, statistical charts, and geometric figures using LaTeX.
Why Choose LaTeX Drawing?
Although many drawing tools exist, LaTeX graphics ensure that figures match the surrounding text perfectly, producing results that are often more beautiful than expected.
Preparation: Import TikZ Packages
To draw graphics we need the TikZ package, which can handle simple geometry to complex 3D images. Include the following in the preamble:
<code>\usepackage{tikz}
\usepackage{pgfplots} % powerful function plotting
\usepackage{tikz-3dplot} % 3D graphics
</code>For Chinese support, also load the ctex package and compile with XeLaTeX:
<code>\usepackage{ctex}
</code>Special graphics may require additional packages such as tkz-euclide for cones.
Drawing Function Plots
Basic Function Plot
Example code for a quadratic function:
<code>\begin{figure}[h!]
\centering
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
xlabel = $x$,
ylabel = $y$,
title = {$y = x^2$},
grid = both,
]
\addplot[
domain=-2:2,
samples=100,
thick,
color=blue,
]{x^2};
\end{axis}
\end{tikzpicture}
\caption{Quadratic function $y = x^2$}
\end{figure}
</code>The code generates a clean quadratic plot with axes, grid, and custom colors.
Multiple Functions
Plotting two functions in the same coordinate system for comparison:
<code>\begin{tikzpicture}
\begin{axis}[
title={Quadratic and Cubic Functions},
xlabel={$x$},
ylabel={$y$},
legend pos=north west,
grid=major,
axis lines = middle,
]
\addplot [domain=-2:2, samples=100, color=blue] {x^2};
\addlegendentry{$y = x^2$}
\addplot [domain=-2:2, samples=100, color=red] {x^3};
\addlegendentry{$y = x^3$}
\end{axis}
\end{tikzpicture}
</code>3D Plot
For more complex three‑dimensional graphics, LaTeX can also handle them. Example of a simple 3D surface plot:
<code>\begin{tikzpicture}
\begin{axis}[
view={60}{30},
xlabel={$x$},
ylabel={$y$},
zlabel={$z$},
title={3D Surface Plot},
]
\addplot3[
surf,
mesh/ordering=y varies,
]{sin(deg(x)) * cos(deg(y))};
\end{axis}
\end{tikzpicture}
</code>Statistical Charts
LaTeX can also produce common statistical charts such as scatter plots, line charts, bar charts, and pie charts.
Scatter Plot
<code>\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
title={Scatter Plot Example},
grid=major,
]
\addplot[
only marks,
color=blue,
] coordinates {(0,0) (1,1) (2,4) (3,9) (4,16) (5,25)};
\end{axis}
\end{tikzpicture}
</code>Scatter plots are useful for showing relationships between variables.
Line Chart
Line charts effectively display trends over time.
<code>\begin{figure}[h!]
\centering
\begin{tikzpicture}
\begin{axis}[
xlabel={Time (s)},
ylabel={Amplitude},
title={Line Plot Example},
grid=major,
]
\addplot[
color=blue,
mark=square*,
mark options={scale=1.5}
] coordinates {(0,0) (1,2) (2,3) (3,5) (4,4) (5,3)};
\end{axis}
\end{tikzpicture}
\caption{A Simple Line Plot}
\label{fig:lineplot}
\end{figure}
</code>Bar Chart
Bar charts are ideal for comparing categories.
<code>\begin{tikzpicture}
\begin{axis}[
ybar,
symbolic x coords={A,B,C,D},
xtick=data,
ylabel={Values},
title={Bar Chart Example},
nodes near coords,
]
\addplot coordinates {(A,1) (B,3) (C,5) (D,2)};
\end{axis}
\end{tikzpicture}
</code>Pie Chart
Pie charts visualize proportion distribution.
<code>\begin{figure}[h!]
\centering
\begin{tikzpicture}
\pie[
text=legend,
radius=3,
color={red, blue, green, yellow}
]{
30/Red,
30/Blue,
20/Green,
20/Yellow
}
\end{tikzpicture}
\caption{A Simple Pie Chart}
\label{fig:piechart}
\end{figure}
</code>Drawing Geometric Figures
LaTeX can also produce professional geometric shapes such as cubes, cones, and spheres.
Cube
<code>\begin{figure}[h!]
\centering
\begin{tikzpicture}
\tdplotsetmaincoords{60}{120}
\begin{scope}[tdplot_main_coords]
\draw[thick] (0,0,0) -- (2,0,0) -- (2,2,0) -- (0,2,0) -- cycle;
\draw[thick] (1,1,2) -- (3,1,2) -- (3,3,2) -- (1,3,2) -- cycle;
\draw[thick] (0,0,0) -- (1,1,2);
\draw[thick] (2,0,0) -- (3,1,2);
\draw[thick] (2,2,0) -- (3,3,2);
\draw[thick] (0,2,0) -- (1,3,2);
\end{scope}
\end{tikzpicture}
\caption{A 3D Cube}
\end{figure}
</code>Cone
<code>\begin{figure}[h!]
\centering
\begin{tikzpicture}[scale=2, line width=1pt]
\draw[densely dashed] (2,0) arc [start angle=0, end angle=180, x radius=2, y radius=0.75];
\draw (2,0) arc [start angle=0, end angle=-180, x radius=2, y radius=0.75];
\draw[thick] (2,0) -- (0,4) -- (-2,0);
\end{tikzpicture}
\caption{A Cone}
\label{fig:cone}
\end{figure}
</code>Sphere
<code>\begin{figure}[h!]
\centering
\begin{tikzpicture}
\def\R{4}
\fill[ball color=white!10] (0,0) circle (\R);
\end{tikzpicture}
\caption{A 3D Sphere}
\end{figure}
</code>The biggest advantage of LaTeX drawing is the seamless integration of graphics with text and the ease of parameterized adjustments. You generate vector graphics directly in LaTeX, ensuring high resolution at any scale without distortion.
Give it a try and create your own beautiful figures with LaTeX!
Model Perspective
Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.