9 Plots

Laura Greenstreet

When including equations, it is often helpful to include plots and figures as a visual learning aid. The Tikz package provides a variety of tools for creating figures. The PGFplots package can be combined with Tikz to create simple to code yet elegant plots.

2D Plots

To create a plot, invoke the plot environment and then specify a few parameters including the axes and function to plot.

[latex]\begin{tikzpicture} \begin{axis}[ axis lines = left, xlabel = $x$, ylabel = {$f(x)$}] \addplot [ domain=-10:10, samples=30, color=blue] {x^2 + 2*x + 1}; \end{axis} \end{tikzpicture}[/latex] [‌latex]
\begin{tikzpicture}
\begin{axis}[
axis lines = left,
xlabel = $x$, ylabel = {$f(x)$}]
\addplot [ domain=-10:10, samples=30, color=blue]
{x^2 + 2*x + 1};
\end{axis}
\end{tikzpicture}[/latex]

Code like this can seem overwhelming, but it becomes easier to read if you parse it one line at a time.

  • \begin{tikzpicture} starts the tikzpicture environment. Tikzpicture is an environment often used for drawing figures with geometric shapes or plots.
  • \begin{axis} starts configuring the axes. The ‘[‘ at the end of the line indicates that we are going to enter some parameters for the axes.
  • axis lines = left, means that the axes will be along the bottom and on the lefthand side.
  • xlabel = $x$ and ylabel = {$f(x)$} specify that the x-axis should be labeled x and the y-axis should be labelled f(x). The brackets around $f(x)$ ensure that the whole group is made into the label. The ‘$’s means that the text will formatted like it is in math mode.
  • The ‘]’ ends the specifications for the axes.
  • \addplot adds a new plot to the figure.
  • domain=-10:10, samples=30, color=blue configures the line on the figure, specifying it should range from x=-10 to x=10, that the plot should use 30 line segments, and that the color of the line should be blue. As you increase the number of line segments, the line becomes smoother. However, using more line segments uses more memory and results in the page loading slower.
  • {x^2 + 2*x + 1}; is the equation that will be plotted.
  • \end{axis} and \end{tikzpicture} end the environments that we started earlier.

Understanding the code, we can modify the plot to show a wider range of values, modify the function, or plot an additional function on the same figure.

[latex]\begin{tikzpicture} \begin{axis}[axis lines=middle, xlabel = $x$, ylabel = {$f(x)$},] \addplot [ domain=-100:100, samples=30, color=blue] {x^2 + 2*x + 1}; \end{axis} \end{tikzpicture}[/latex] [‌latex]
\begin{tikzpicture}
\begin{axis}[axis lines=middle, xlabel = $x$, ylabel = {$f(x)$}]
\addplot [ domain=-100:100, samples=30, color=blue]
{x^2 + 2*x + 1};
\end{axis}
\end{tikzpicture}[/latex]
[latex]\begin{tikzpicture} \begin{axis}[ axis lines = left, xlabel = $x$, ylabel = {$f(x)$},] \addplot [ domain=-10:10, samples=30, color=red] {x^2 - 3*x + 4}; \end{axis} \end{tikzpicture}[/latex] [‌latex]
\begin{tikzpicture}<
\begin{axis}[
axis lines = left,
xlabel = $x$,
ylabel = {$f(x)$},]
\addplot [ domain=-10:10,
samples=30,
color=red]
{x^2 - 3*x + 4};
\end{axis}
\end{tikzpicture}[/latex]
[latex]\begin{tikzpicture} \begin{axis}[ axis lines = left, xlabel = $x$, ylabel = {$f(x)$},] \addplot [ domain=-10:10, samples=30, color=blue] {x^2 + 2*x + 1}; \addplot [ domain=-10:10, samples=40, color=red] {20*sin(pi*x^2)}; \end{axis} \end{tikzpicture}[/latex] [‌latex]
\begin{tikzpicture}
\begin{axis}[
axis lines = left,
xlabel = $x$,
ylabel = {$f(x)$}]
\addplot [ domain=-10:10,
samples=30,
color=blue]
{x^2 + 2*x + 1};
\addplot [ domain=-10:10,
samples=40,
color=red]
{20*sin(pi*x^2)};
\end{axis}
\end{tikzpicture}[/latex]

3D Plots

PGFPlots can also create 3D plots. The code for a 3D plot is similar to the 2D plot. Note that in this case we are setting the bounds for the axes in the parameters for addplot3 with the argument ‘domain=0:360’.

[latex]\begin{tikzpicture} \begin{axis} \addplot3[surf,domain=0:360,samples=40] {cos(y) + sin(x)}; \end{axis} \end{tikzpicture}[/latex] [‌latex]
\begin{tikzpicture}
\begin{axis}
\addplot3[surf,domain=0:360,
samples=40] {cos(y) + sin(x)};
\end{axis}
\end{tikzpicture}
[/latex]

Here is another example of a 3D plot where we use all three dimensions to create a spiral.

[latex]\begin{tikzpicture} \begin{axis}[view={60}{10}] \addplot3[mesh, domain=0:20*pi, samples=150, samples y=0, line width=2pt] ({x*sin(deg(x))}, {x*cos(deg(x))}, {x}); \end{axis} \end{tikzpicture}[/latex] [‌latex]
\begin{tikzpicture}
\begin{axis}[view={60}{10}]
\addplot3[mesh, domain=0:20*pi, samples=150,
samples y=0, line width=2pt]
({x*sin(deg(x))},
{x*cos(deg(x))},
{x});
\end{axis}
\end{tikzpicture}
[/latex]
  • The two numbers given as parameters to the axes specify what angle you will see that plot at. The first argument specifies the rotation of the x-y plane and the second argument specifies the rotation in z.
  • The mesh argument in addplot creates the color gradient.
  • The samples = 200 argument says tells the plot how many line segments to generate. Note that in this case we had to use 150 samples in comparison to the 30 samples used for the other examples. The plot below shows what the spiral looks like with 50 and 100 samples.
    [latex]\begin{tikzpicture} \begin{axis}[view={60}{10}, title={Spiral Using 50 Line Segments}] \addplot3[mesh, domain=0:20*pi, samples=50, samples y=0, line width=2pt] ({x*sin(deg(x))}, {x*cos(deg(x))}, {x}); \end{axis} \end{tikzpicture}[/latex] [latex]\begin{tikzpicture} \begin{axis}[view={60}{10}, title={Spiral Using 100 Line Segments}] \addplot3[mesh, domain=0:20*pi, samples=100, samples y=0, line width=2pt] ({x*sin(deg(x))}, {x*cos(deg(x))}, {x}); \end{axis} \end{tikzpicture}[/latex]
  • The function specifies the spiral with a function for each coordinate in terms of a single variable. This type of function is called a parametric function.

The y samples = 0 specifies that we are defining the line in terms of a single parameter. If you do not include this, Latex will generate an error. It is not obvious that you need this parameter, and the error doesn’t make it obvious what to change:

When doing something new with a package, such as creating a new type of plot, it is often easiest to learn from examples. The package documentation is often a good source of examples and will also help you figure out what arguments can be used. Here is the package documentation for PGFPlot. You can usually find the documentation of a package just by Googling the name of that package and documentation.

License

Icon for the Creative Commons Attribution-ShareAlike 4.0 International License

Plots by Laura Greenstreet is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License, except where otherwise noted.

Share This Book