Plots and Tables

## Introduction

Plots are one of the most common tools used by scientists and engineers to communicate their findings. The definition of what makes a plot "good" or "bad" can vary from person to person but they all share characteristics at the most basic level. Here, I'll discuss these basic characteristics and demonstrate how to achieve them using MATLAB.

## Basic Plot Characteristics

### Plot Types

There are three basic plot types that you'll probably use at some point in CENG 176:

1. **Bar charts.** These are typically used in one of the following cases:
	1. You're comparing *exactly* two values.
	2. Your *x*-axis is best represented as a qualitative category instead of a number.
2. **Scatter plots.** The most common plot style, scatter plots are commonly used in the following cases:
	1. You'd like to display three or more of quantitative data pairs.
	2. Continuous functions for which you have some method of generating arbitrarily spaced data (*e.g.*, analytic functions or results from numerical calculations).
	3. Regression analyses, which are combinations of the above two cases.
3. **Surface or contour plots.** A means to show the relationship between three variables, these plots are rarely used in this lab except in the LPCVD experiment.

### Plot Characteristics

Since the goal of any plot is to communicate the results of some experiment or analysis to the reader, the following items and features are common to nearly all plots:

1. **Labeled, square axes.** The reader needs to know what data are being shown and the axis labels achieve this. The labels should always include units unless the value being plotted is dimensionless or is a qualitative metric. The axes are typically square unless the *x*-axis represents a significantly wider range than the *y*-axis (*e.g.*, DLS data are usually asymmetric with a longer *x*-axis).
2. **Data series.** Experimental measurements are represented as data points (circles, dots, etc); continuous functions such as theoretical predictions or regression analyses are represented as continuous lines.
3. **Error bars.** All experimentally determined values have error and it's critical that you communicate you degree of certainty to the reader through error bars. See the Statistics wiki for information on how to calculate error (uncertainty).
4. **A caption.** The caption to any figure should be descriptive; merely stating "Figure 3. A plot of *x* vs. *y*" tells the reader nothing. Typically a caption has two parts:
	1. A short phrase to describe what is being plotted. Example: *Mass fraction of salt in the permeate stream (\(x\_p\)) as a function of transmembrane pressure (\(\Delta P\)).*
	2. An additional one or two sentences describing the highlights of the plot. These highlights could include any regression analyses, the interpretation of the data, and any outliers or unusual data. Example: *The solid line is a linear best-fit regression of the data; error bars were calculated by propagation of error from experimental apparatuses. Eq. (4) predicts a linear relationship where the slope is equal to \(A\_wS\_a\) from which we conclude \(A\_w S\_a = 4.6 \textrm{ kg}\).*
5. **Legible features.** The method below produces a PDF file of your plot which, as a vector graphic, is one of the best ways to make a graphic because it retains its resolution regardless of how far you zoom in or zoom out. High-resolution bitmaps, also known as raster graphics, are also acceptable, but generally vector graphics are superior. Even with vector graphics, however, you must be careful to size your plots so that font sizes, data points, lines, etc are legible when they appear in the final report. The method below will ensure that this is not a problem

### What kind of plot do I need?

Follow this handy flow sheet below (click to enlarge) if you're not sure what belongs on a plot. Keep in mind that this represents a *generalized* classification of plots that are useful in this course and might not be the best choice for your application. There are several instances in this course where other plots or modifications to these guidelines are appropriate. See also Data Visualization or flowingdata.com (both by Nathan Yau) for a discussion about effective, enjoyable figures.

## Example Figure Generation

Although there are other ways to make decent plots, they often require significant effort and only rarely produce better results than the method below (and it's even harder to produce quality vector graphics). Consequently, **I strongly recommend** you follow the method below to make a high-quality plot. The resulting file can be inserted easily into your ShareLaTeX report; there is an example plot in the sample report that you can refer to if you're unclear on the commands needed to insert a graphic.

You'll need an additional MATLAB file to duplicate this example:

1. `sizeFig()`: Use this to re-size the plot axes to the desired width and height (in centimeters).

### Generating the Plot

The first thing you'll need to do is make the plot itself. For this example, I'm going to use the same calibration data from the pump calibration curve example on the Statistics page; check out the description on that page if you're curious about what's being plotted.

First, generate all the needed data series:

```
clear all
close all
knob = [0; 1; 2; 3; 4; 5];
rate = [0.0; 2.3; 4.2; 7.2; 8.4; 11.9];
err_knob = [0.2; 0.2; 0.2; 0.2; 0.2; 0.2];
err_rate = [0.0; 0.3; 0.4; 0.5; 0.6; 0.7];
fobj = fit(knob, rate, 'poly1');
fknob = linspace(min(knob), max(knob), 50);
frate = fobj(fknob);
fpi = predint(fobj, fknob);

```

Now make the actual plot. If you're not familiar with MATLAB figure generation, refer to the extensive documentation for the functions below.

```
% Make the actual plot:
figure; hold on;
h1 = errorbar(knob, rate, err_rate, err_rate, err_knob, err_knob, 'ko');
h2 = plot(fknob, frate, 'k-');
h3 = plot(fknob, fpi, 'r-');

```

Notice that the experimental data points have been represented by black circles (`'ko'`) and the continuous functions of the linear regression and prediction intervals have been represented by a black line (`'k-'`) and red lines (`'r-'`). Finally, we add the necessary information to describe what is being plotted:

```
% Add labels and make it pretty:
xlabel('Knob Setting');
ylabel('Discharge Rate (mL/s)');
legend([h1 h2 h3(1)], {'Data', 'Fit', 'Pred. Int'}, 'Location', 'NorthWest');
set(gca, 'box', 'on');
hold off;

```

The legend is critical when more than one data series is shown; don't forget it! The box around the plot area (`set(gca,'box','on')`) is helpful in the final report to distinguish between report text and figure text. A PNG file of the plot at this stage is shown in **Figure 1**.

Note the grainy, pixelated appearance (ewww); that's because I used File -> Save As... \*.png to save this image. If instead I use File -> Save As... \*.pdf then I'll get this file. Better, but notice that this produces a large plot (it's nearly 50% of the page height) in the middle of an entire 8.5" x 11" sheet, which will obviously look like crap in your report. We need to re-size!

### Resizing the Figure

(Note: You'll need the `sizeFig()` function for this part)

The next step is to re-size the figure for publication. On a printed page, most plots are about 8.5 cm wide as measured from the left-most feature (usually the *y*-axis label) to the right-most feature (usually the last tick mark label on the *x*-axis). We're less picky about getting a figure *exactly* 8.5 cm wide (some journals are quite the opposite), so we're going to instead set the size of the axes themselves and to do this we're going to use the `sizeFig()` function in MATLAB. You should note that this function is good but not perfect: if you've got a very odd kind of plot (3D, for example) then you might have to re-size the figure manually (on-screen) as best you can.

The syntax for `sizeFig()` is simply `sizeFig(width, height)` with a single, undocked figure open, where `width` and `height` are the desired width and height of the plot axes in centimeters. This makes it easy to adjust the aspect ratio of the plot, and as a general guideline the aspect ratio should be one of the following:

1. **Square.** This is the most common aspect ratio; use it in most instances with a width and height of 7 cm. With a single plot open, simply call `sizeFig(7, 7)` to re-size your plot axes to a 7 cm x 7 cm square.
2. **Golden ratio.** If you need an asymmetric plot (*e.g.*, liposome distribution plots) then a good place to start is by using the Golden Ratio, 1:1.618. A height of 7 cm is usually a good height; to achieve the "golden" aspect ratio, simply call `sizeFig(7 * 1.618, 7)`.

Since the calibration curve has no unusual features a standard square aspect ratio is appropriate, and the result of `sizeFig(7, 7)` is shown in **Figure 2**.

Note that `sizeFig` has also cropped the plot to remove excess whitespace, a feature most notable in the PDF version which you can download here.

That's all there is to it to create a decent looking plot! Happy plotting!

## Tables

Table creation is handled within LaTeX; check out the format example that I provided with your ShareLaTeX account to see an example of the appropriate syntax. Generally, there are only three requirements when you make a table:

1. The units of any numbers should be in the top row along with the variable.
2. There are only three horizontal lines, called "rules," in any table: the top rule (\toprule), the middle rule (\midrule) that separates the labels in the first row from the data in subsequent rows, and the bottom rule (\bottomrule) that "finishes" the table.
3. The table caption goes above the table (as opposed to below figures). This is handled automatically in LaTeX provided you use follow the syntax in the format example.

Try to avoid tables unless you think they're necessary. It's nearly always possible to represent your data more effectively as a plot, but there are a few times when a table might be better than a plot. Try also to keep your tables small: one thing that LaTeX is not good at is large, multi-page tables, but you're going to have to talk fast to convince me that you need such a large table for your report.

## References

1. ↑ N. Yau. Data Points - Visualization that means something. Wiley, Indianapolis, 2013.