Plotting data for reports is often done in programmes such as R, matplotlib or MATLAB. However, sometimes one wants to have a consistent layout for all plots and this is far from easy when you need to combine some of these tools. One way to tackle this, is to store all plot data in a CSV-file and then import that into LaTeX. We can plot it using a special library if the plot does not contain too many points (more on that later).

Plotting Graphics From File

The easiest way is by using the pgfplots package, as illustrated in this example:

First, we need to load the pgfplots package, since it makes loading data from file very easy. Next, we state that we want to use the newest version (this removes a warning). Now it is time to define the plot. Use the axis environment inside a tikzpicture environment (pgfplots uses tikz internally).

We pass a lot of options to the axis environment. Their meaning is very straightforward: labels, grids, ticks etc. Personally, I like a light grid (minor grid style={gray!25} and major grid style={gray!25}). Furthermore, we limit the width of the plot and remove any markers. Sometimes markers are needed (for small sets of data). In this case, you can use mark=* to mark the data points with a small dot. If we need a normal (linear) axis, use the default normal instead of log mode for x- or y-axis.

By now we have defined the axes-system and it’s time to add a plot. \addplot does exactly that. We want a solid blue line of 1 point thick. The data is loaded from a table, with x-data in the f column and y-data in the T-column. These names are derived from the first line in the CSV-file. This line is not required (an index-based system is also possible), but their use is advised since these names give meaning to the data. The actual data is loaded from latex-import-data.csv and separated by commas. Notice that the syntax is very similar to the syntax to plot a function with pgfplots, see also this post.

An extract from the CSV-file used in this example is:

We also notice the \addlegendentry command. I personally prefer adding the legend entry directly with the plot addition, but you can opt to add it later too. Finally, we have the inclusion of the SIunits package. This might seem a bit over the top for just the Hz from \hertz. But this package also provides the \unit command which is really nice to use for units and numbers! Notice the difference (after typesetting) between \unit{15}{\kilo\hertz} and 15 kHz in a multi-line paragraph and be convinced of this package’s need. Hint: the space between the number and unit is a little smaller with the used of \unit, and they will never be on separate lines.

The example is generated by a relatively small number of data points (about 30). Sometimes it can happen that a plot has thousands of points. In that case, you might run out of buffer space. One way to solve this, is to plot only a small fraction of all points with the following additional options:

Apart from generating plots based on CSV-files, we can also create entire tables from a file. This is done with the pgfplotstable package. A basic example loads the data previously plotted (and more):

As always, we need to load appropriate packages. To create the table, we just have to use the \pgfplotstabletypeset with the required a required argument that indicates the file. In this case, this is the filename: latex-import-data.csv. We can also use a command-line system when we need the same table multiple times:

Take note that we should wrap this table inside a table environment, but because these snippets are typeset to the svg-images below using the standalone package, this is not possible. The omission of the table environment also explains why the caption (and rule break) have been commented out.

The options are fairly straightforward to use. First, we specify which columns we want to display. Note that the CSV-file must contain a header that is readable by LaTeX. Best practice is to use a single word column name that has clear semantics. If we want to plot all columns, we can also omit this option. Afterward, we add some styling to our header and define our caption (if needed). We can also specify the way individual columns are displayed (from the head name to the decimal precision).

At the end, we also have two special options: col sep=comma and dec sep align. col sep=comma is required for CSV-files, because by default the separation symbol is a space. We thus need to change this to a comma. Finally, dec sep align is an optional option to align decimal points. This is standard in scientific literature and makes it easy to compare numbers.

We can also include individual numbers inside a text with my own dynamicnumber package. This package allows the inclusion of individual numbers inside a text. An example is depicted below.
The content of the measurement.dnlist file is automatically created by the scripting side of the dynamicnumber package:
First, we load the dynamic number source file (measurement), afterwards we use the variables stored inside with the \dnget command.