06 Data Visualization & Plotting

Overview: The Graphics Engine

MATLAB features a graphics rendering system based on Handle Graphics. This note covers 2D line plots, tiled layouts, 3D meshes, and handle property modifications.


1. 2D Plotting and Graph Labeling

The fundamental tool for 2D graphing is plot.

Function Signatures:

  • plot
    • Syntax: plot(Y), plot(X, Y), or plot(X, Y, lineSpec)
    • Parameters:
      • X: Independent variable data vector.
      • Y: Dependent variable data vector.
      • lineSpec (Optional): Formatting string defining color, marker, and line style (e.g. 'r--' for red dashed line).
  • axis
    • Syntax: axis([xmin, xmax, ymin, ymax]), axis equal, or axis square
    • Description: Sets axis limits or adjusts coordinate ratios.
  • legend
    • Syntax: legend(label1, label2, ...)
    • Description: Adds a labels legend to curves in the order they were plotted.

2. Dynamic Plotting Methods: hold and fplot

Plotting Multiple Curves:

By default, calling plot wipes the active figure window. To plot multiple curves, you can:

  1. Pass multiple sets of vectors: plot(x, y1, x, y2)
  2. Use hold on to keep the current plot and overlay new curves. Call hold off when finished.

Functional Plotting:

  • fplot
    • Syntax: fplot(f, limits)
    • Parameters:
      • f: A function handle (e.g. @(x) sin(x)).
      • limits: Vector setting domain bounds [xmin, xmax].
    • Description: Dynamically samples and plots a mathematical function over the specified interval.

3. Subplots (Tiled Layouts)

To display multiple plots side-by-side in a single window, partition the screen layout.

  • subplot
    • Syntax: subplot(m, n, p)
    • Parameters:
      • m: Total row divisions.
      • n: Total column divisions.
      • p: The index slot of the active tile (numbered row-by-row, left-to-right).

4. Specialized 3D Surface Plots

  • plot3
    • Syntax: plot3(X, Y, Z)
    • Description: Plots a 3D line trajectory.
  • mesh / contour / pcolor
    • Syntax: mesh(Z) (3D grid), contour(Z) (2D isolines), pcolor(Z) (2D colored matrix cells).
    • Description: Generates surface or density plots from a grid matrix Z.

5. Handle Graphics (set, get, gca)

Every graphical element (figure, axes, lines) is an object with a unique identifier handle.

Key Handle Functions:

  • gca (Get Current Axes)
    • Syntax: ax = gca
    • Description: Returns the handle of the current axes.
  • gcf (Get Current Figure)
    • Syntax: fig = gcf
    • Description: Returns the handle of the current figure window.
  • set
    • Syntax: set(handle, 'PropertyName', propertyValue)
    • Description: Modifies properties of a graphics object.
  • get
    • Syntax: val = get(handle, 'PropertyName')
    • Description: Returns the current state value of a property.

💻 Conceptual Code Demonstration

% 1. Sample data
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
 
% 2. Partition figure using subplot
subplot(1, 2, 1); % Selects first tile in 1x2 grid
plot(x, y1, 'b-');
title('Sine Wave');
 
subplot(1, 2, 2); % Selects second tile
lineHandle = plot(x, y2);
title('Cosine Wave');
 
% 3. Modify line using Handle Graphics properties
set(lineHandle, 'Color', 'r');      % Changes color to red
set(lineHandle, 'LineWidth', 2.0);  % Sets line thickness
set(lineHandle, 'LineStyle', '--');  % Sets dashed style
 
% Adjust axes direction using gca
set(gca, 'XLim', [0, 2*pi]);