Creating High-Quality Plots in LaTeX with PGFPlots
PGFPlots is a powerful LaTeX package that revolutionizes the way we create plots in technical documents. Designed to integrate seamlessly with LaTeX, PGFPlots allows users to generate high-quality 2D and 3D plots directly within their documents, eliminating the need for external graphing software. This versatile tool offers a wide range of plotting capabilities, from simple line graphs to complex 3D surfaces, all while maintaining the uniform formatting of LaTeX. Whether you’re a researcher presenting data in a scientific paper, an engineer creating technical reports, or a student working on a thesis, PGFPlots provides the flexibility and precision needed to visualise your data effectively. In this blog post, we’ll explore the basics of PGFPlots, demonstrate its key features, and show you how to create professional-looking 2D and 3D plots that enhance the quality of your LaTeX documents
Here’s the stepwise guide for installing and setting up PGFPlots in LaTeX: Installation & Setup Guide for PGFPlots
TeX Live: https://tug.org/texlive/
MiKTeX: https://miktex.org/
\usepackage{pgfplots}
to check for errors.For MiKTeX:
On Windows, search for “MiKTeX Console” in the Start menu and open it.
Navigate to Packages (on the left sidebar).
In the search bar, type pgfplots and press Enter.
Select the package named pgfplots from the list.
Click on Install to download and install PGFPlots.
Wait for the installation to complete.
For TeX Live:
tlmgr install pgfplots
For Overleaf:
Add the following line in the preamble of your document
\usepackage{pgfplots}
\pgfplotsset(compact=newest)
PGFPlots offers a wide range of powerful features for creating high-quality plots directly within LaTeX documents. Here are some key capabilities:
PGFPlots excels in creating both 2D and 3D plots with ease:
2D Plots: Supports various types of 2D plots, including line graphs, scatter plots, and bar charts.
3D Plots: Enables creation of surface plots, mesh plots, and contour plots in three dimensions.
PGFPlots offers a diverse array of plot types to suit different data visualization needs:
PGFPlots provides extensive customization capabilities:
By combining these features, PGFPlots enables users to create publication quality plots that seamlessly blend with their LaTeX documents, offering both flexibility and precision in data visualization.
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
xlabel = $x$,
ylabel = {$f(x)$},
]
\addplot[samples=100, domain=-2:2, blue, thick] {x^2};
\end{axis}
\end{tikzpicture}
\end{document}
Explanation of the code:
Uses article
class.
Loads the pgfplots
package.
tikzpicture
environment, which is used for creating graphics using TikZ.
\begin(axis)
: begins axis environment for plotting.
axis lines = middle
: Places the x-axis and y-axis at the center (instead of the default bottom-left).
xlabel = $x$
: Labels the x-axis as x.
ylabel = {$f(x)$}
: Labels the y-axis as f(x).
\addplot[...] {x^2}
; plots the function f(x)=x^2.
samples=100
: Uses 100 sample points for a smooth curve.
domain=-2:2
: Plots the function from x=−2 to x=2.
blue, thick
: Colors the curve blue and makes it thicker for better visibility.
\end(axis)
and \end(tikzpicture)
closes environments.
Output: This creates a simple parabola f(x) = x^2 with labeled axes.
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
view={60}{30},
axis lines = middle,
xlabel = $x$,
ylabel = $y$,
zlabel = {$f(x,y)$}
]
\addplot3[surf, domain=-2:2, y domain=-2:2] {x^2 + y^2};
\end{axis}
\end{tikzpicture}
\end{document}
Explanation of the code:
Uses article
class.
Loads the pgfplots
package.
tikzpicture
starts a drawing environment where we define and draw the plot.
\begin{axis}
initializes the 3D coordinate system.
view={60}{30}
sets the viewing angle
axis lines = middle
places the axis lines in the middle of the graph.
xlabel = $x$, ylabel = $y$, zlabel = {$f(x,y)$}
define the labels for the axes.
\addplot3
is used for 3D plotting.
surf
specifies that the function should be drawn as a surface plot.
domain=-2:2, y domain=-2:2
defines the range of x and y from -2 to 2.
{x^2 + y^2}
is the function being plotted.Output: This generates a 3D surface plot for f(x,y)=x^2+y^2
PGFPlots is a versatile tool that finds applications in various fields where data visualization is crucial. Here are some key use cases:
Researchers can create publication-quality plots directly within their LaTeX documents. This ensures consistency in formatting and style throughout the paper. PGFPlots is particularly useful for:
Engineers and technical professionals can utilize PGFPlots to:
Graduate students can benefit from PGFPlots in their thesis by:
PGFPlots can be used with LaTeX beamer class to create:
Educators can use PGFPlots to:
By integrating seamlessly with LaTeX, PGFPlots allows users to create high-quality, customizable plots that enhance the visual appeal and clarity of their documents across various academic and professional fields.
https://youtu.be/CcOPVFqndQo
PGFPlots is an incredibly useful tool for anyone working with LaTeX, especially when it comes to creating professional, well-formatted plots. Instead of relying on external software for graphs, it allows users to generate high-quality 2D and 3D visualizations directly within their documents. This not only ensures consistency but also makes the entire workflow smoother for researchers, engineers, and students. While it takes some time to get familiar with the syntax, the level of control and customization it offers makes the effort worthwhile.PGFPlots provides a flexible and reliable solution for simple line plots as well as complex 3D surfaces. With practice, it becomes an essential skill for anyone working on scientific papers, technical reports, or academic projects.
These resources cover a wide range of topics, from basic usage to advanced techniques, ensuring you can make the most of PGFPlots in your LaTeX documents.