- Simple plot
- Derivative function
- Polyline / Markers
- Multiple plots
- Logarithmic axis
- Adding positions manually
- Steep rise (polyline)
- Data points, splines (theory)
- Data points, splines (examples)
- Parametric plot
- Parametric points
- PDF+TeX
- License
- Installation
- Tutorial
- Reference
- FAQ
- Technical details
- Changes
- Related projects

3 Tutorial |

Example overview |

Source file for GNU Octave |

What did pgfplot do here? |

Process the file |

In our first example we want to plot the resulting resistance
for a parallel connection of two resistors.

R_{1} is variable in the range 0≤R_{1}≤10Ω.
R_{2}=10Ω (constant).

The formula to calculate the resulting resistance is:

We use the following source code to plot:

example01.m

`1; # Not a function file`

`# Load the package.`

`pkg load octpgfpl;`

`# Function to calculate resulting resistance of two`

`# parallel resistors. The function must be vectorized.`

`function [result] = parallelResistors(a,b)`

`result = (a .* b) ./ (a .+ b);`

`result(!(isfinite(result))) = 0;`

`endfunction`

`# Function to plot, must be a vectorized function.`

`function [y] = f(x)`

`y = parallelResistors(x, 10);`

`endfunction`

`pgfplot(`

`# The first three arguments are`

`# - output file name`

`# - Unit for width and height specification (cm or in)`

`# - A vector containing width (10cm) and height (5cm).`

`# Here we produce a *.tex file, we can run pdflatex on it.`

`'example01.tex', 'cm', [10 5],`

`# All remaining arguments are key/value pairs.`

`#`

`# The x axis is configured using the cell array list returned`

`# by the pgfplot_varargs function.`

`# This function allows us to write a one dimensional cell array`

`# list consisting of key/value pairs in a readable manner.`

`'axis.x', pgfplot_varargs(`

`# We use linear scaling for the axis, values are in the`

`# range 0 <= x <= 10.`

`'lin', [0 10],`

`# To draw the grid we divide the x range into 10 intervals.`

`'grid', 10,`

`# To draw tics we divide the x range into 10 intervals too.`

`'tics', 10,`

`# The axis label is typeset by LaTeX.`

`# For each backslash we want to pass to LaTeX we have to`

`# write two backslashes here.`

`'label', "\\(R_1\\)",`

`# The unit label is also typeset by LaTeX.`

`'unit.text', "\\(\\Omega\\)"`

`),`

`# The y axis is configured similarly.`

`'axis.y', pgfplot_varargs(`

`'lin', [0 5],`

`'grid', 5,`

`'tics', 5,`

`'label', "\\(R_{\\text{res}}\\)",`

`'unit.text', "\\(\\Omega\\)"`

`),`

`# Here we configure one item to plot.`

`'plot', pgfplot_varargs(`

`# The item to plot is the f() function. The function must be`

`# vectorized.`

`'function', @f,`

`# The line width for the plot is two times the base line width`

`# used to draw the grid.`

`'lw', 2`

`),`

`# Finally we configure options for LaTeX.`

`'latex', pgfplot_varargs(`

`# We pass the option 12pt to the document class.`

`'font-size', 12`

`)`

`);`

- As the
*x*axis is in the range 0...10 and we did not explicitly configure a range for the plot, the range 0≤x≤10 was used. - As we did not explicitly configure the number of plot intervals, the range was divided into 20 (the default) intervals.
- A
*linspace(0,10,21)'*vector (20 intervals have 21 points) was created by*pgfplot()*and passed to the*f*() function to obtain the 21 corresponding*y*values. - A spline was constructed through the 21 points. As we did not explicitly configure a spline type the default "not-a-knot" was used. Constructing the spline resulted in a piecewise polynomial.
- The piecewise polynomial was used to find the first derivative in all the points.
- A sequence of Bezier segments was constructed from the points and the derivatives.

Run

`octave -q example01.m`

`pdflatex example01`

to produce example01.pdf.

You can use your favourite PDF reader to view the file.

For more complicated plots you will probably need some iteration
steps in which you modify the *.m file, run octave and
pdflatex.

Some PDF readers like i.e. evince do not lock the PDF file and
update the view automatically on changes, so you can keep the
reader open.

Other PDF readers lock the file which causes pdflatex to fail or do
not update the view automatically.

Once the image is to your liking you can replace the file name "example01.tex" by "example01.pgf" and run octave once again. Now you have a PGF file you can include in a *.tex source.

\documentclass{article} ... \usepackage{color} \usepackage{graphicx} \usepackage{fancybox} \usepackage{pgfcore} ... \begin{figure}% {\centering% \input{example01.pgf}% \caption{My example diagram}\label{fig:example01}% }% \end{figure}

Dirk Krause

Schmalkalden, 2017-09-24