home

Scientific Computing (Psychology 9040a)

Fall, 2021


Graphical Displays of Data

MATLAB comes with lots of built-in functionality for generating both 2-D and 3-D graphics. You will learn a collection of commands—mainly plot()—that will enable you do generate plots and configure their appearance with just about any degree of precision you want. The great advantage of generating graphics purely based on scripted commands (as opposed for example to graphical, pointy-clicky approaches based on graphical user interfaces (GUIs) is that you can easily and instantly re-generate graphics simply by re-running the script(s). In addition making changes to your graphics becomes a simple matter of tweaking commands and scripts.

Rather than writing notes that essentially duplicate the extensive online documentation included with MATLAB, I will instead point you to some intial starting locations where you can learn about generating graphics in MATLAB.

Graphics in MATLAB

Common Graphics Functions in MATLAB (a graphical menu showing different kinds of plots and how to generate them)

2-D and 3-D Plots

The plot() command

Creating 2-D Graphs and Customizing Lines

Add Titles, Axis Labels and Legends to Graphs

Combine Multiple Plots

3-D Surface and Mesh Plots

Shaded Polygons

Customizing graphics by setting properties of the underlying objects

Saving figures as bitmaps vs vector graphics

Bitmap formats such as .png, .jpg, .tif, etc, are resolution-specific, meaning when you export a figure in a bitmap format, you have to specify the resolution to save to. The higher the resolution, the larger the figure.

figure
plot(randn(1,1000),'b.');
xlabel('X'); ylabel('Y'); title('My Great Figure');
print myfig -dpng -r300 % save to a file called myfig.png at 300 dpi resolution

Vector graphics formats such as .pdf and .eps are resolution-independent. Instead of saving a grid of pixels, they save instructions to draw the elements of the figure. You will get much nicer looking figures using vector graphics formats. The possible exception is photographs or other photograph-like images which may be better suited to bitmap formats.

figure
plot(randn(1,1000),'b.');
xlabel('X'); ylabel('Y'); title('My Great Figure');
print myfig -dpdf % save to a file called myfig.pdf

Whitespace around .pdf figures

You might notice that when you save a figure in .pdf format, MATLAB saves an entire ‘page’ and places the figure in the middle of the page. As a result you will see lots of whitespace around the figure.

Saving a figure to .eps (encapsulated postscript) format avoids this.

To solve this problem for .pdf figures you can use the following little function:

function out = mypdfprint(h, f)

% Given a figure handle h and a filename f,
% prints the figure to a .pdf file and strips away
% the bounding box whitespace around the figure

set(h,'Units','Inches');
pos = get(h,'Position');
set(h,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3),pos(4)]);
print(h,f,'-dpdf','-r0');

end

So for example:

f1 = figure;
plot(randn(1,1000),'b.');
xlabel('X'); ylabel('Y'); title('My Great Figure');
mypdfprint(f1, 'myfig')