TExcelTAD
Home › MATLAB / Octave › Plot Formatting

Plot Formatting

A default plot is fine for a quick look, but figures for a report need colours, line styles, markers, a legend, a grid and sensible limits. As in the previous lesson, each figure shows the full plotting code, then the runnable data part with a sketch of the result.

Line specification strings

The optional third argument of plot is a short line spec combining up to three codes, in any order:

Colour Line style Marker
b blue - solid (default) o circle
r red -- dashed s square
g green : dotted * asterisk
k black -. dash-dot x cross
m / c / y magenta / cyan / yellow ^ v d . + triangles, diamond, point, plus

If you give a marker but no line style, only the markers are drawn: plot(x, y, 'ro') gives red circles with no line.

matlab
x = 0:5;
plot(x, x.^2, 'r--s')        % red, dashed, square markers
title('y = x^2')
matlab · try it yourself
x = 0:5;
disp([x; x.^2])
    0    1    2    3    4    5
    0    1    4    9   16   25

Text in titles and labels supports TeX-style markup: x^2 shows a superscript 2, x_1 a subscript and \alpha a Greek letter.

Name-value properties

For finer control, pass name-value pairs after the data (and optional line spec). The most common ones:

Property Example Meaning
'LineWidth' 2 Line thickness in points (default 0.5)
'Color' [0.1 0.6 0.3] RGB triple with values 0–1, or a letter
'MarkerSize' 8 Marker size in points
'MarkerFaceColor' 'w' Fill colour of markers
'LineStyle', 'Marker' '--', 'o' Same as in the line spec
matlab
days = 1:7;
steps = [6200 8400 7100 10300 9800 12100 5400];
plot(days, steps, '-o', 'LineWidth', 2, 'Color', [0.1 0.6 0.3], ...
     'MarkerSize', 8, 'MarkerFaceColor', 'w')
xlabel('Day of week'); ylabel('Steps')
title('Daily steps')
matlab · try it yourself
days = 1:7;
steps = [6200 8400 7100 10300 9800 12100 5400];
fprintf('Average %.0f steps; %d days above 8000\n', mean(steps), sum(steps > 8000));
Average 8471 steps; 4 days above 8000

Grid, limits and ticks

Command Effect
grid on / grid off Show / hide grid lines
axis([xmin xmax ymin ymax]) Set both axis ranges
xlim([a b]), ylim([a b]) Set one range
axis equal Same scale on both axes (circles look round)
axis tight Limits fit the data exactly
xticks(v), yticks(v) Choose tick positions
xticklabels({...}) Text labels for the ticks
matlab
t = linspace(0, 10, 200);
y = exp(-0.3*t) .* cos(2*t);
plot(t, y, 'LineWidth', 1.5)
grid on
axis([0 10 -1 1])
xlabel('t (s)'); ylabel('Displacement')
title('Damped oscillation')
matlab · try it yourself
t = linspace(0, 10, 200);
y = exp(-0.3*t) .* cos(2*t);
fprintf('max %.4f, min %.4f, final %.4f\n', max(y), min(y), y(end));
max 1.0000, min -0.6311, final 0.0203

Legends and annotations

legend labels each line in the order it was plotted. The 'Location' option moves the box ('northwest', 'southeast', 'best', …). text(x, y, 'label') writes text at a data position:

matlab
x = 0:0.1:3;
plot(x, x, 'b-', x, x.^2, 'r--', 'LineWidth', 1.5)
legend('y = x', 'y = x^2', 'Location', 'northwest')
text(1, 1, '  crossing at (1, 1)')
grid on
matlab · try it yourself
x = 0:0.1:3;
crossings = x(abs(x - x.^2) < 1e-12);
disp(crossings)
   0   1

Saving a figure

In MATLAB or Octave desktop you can save the current figure from code (this needs a graphics window, so it doesn't run here):

matlab
saveas(gcf, 'damped.png')                 % format from the file extension
print('-dpdf', 'damped.pdf')              % vector PDF
exportgraphics(gca, 'damped.png', 'Resolution', 300)   % MATLAB R2020a+ only

gcf means "get current figure" and gca "get current axes": handles you can also pass to set to change properties, for example set(gca, 'FontSize', 12).

Test yourself

What does the line spec 'g:*' draw?

  1. Green dotted line with asterisk markers
  2. Grey dashed line
  3. Green circles only
  4. A green grid

How do you set the x-axis to run from 0 to 5 without changing the y-axis?

  1. axis([0 5])
  2. xlim([0 5])
  3. xaxis(0, 5)
  4. set x 0 5

Which name-value pair makes a line thicker?

  1. 'Width', 3
  2. 'LineWidth', 3
  3. 'Thickness', 3
  4. 'Size', 3