Introduction: Why Data Visualization Matters in Science and Engineering
If you’ve been following this course from the beginning, you’ve already encountered MATLAB’s plotting functions in passing. You know that plot() creates a 2D line plot, surf() creates a 3D surface plot, and figure() creates a new figure window. But there’s so much more to MATLAB’s visualization capabilities than these basics.
In this comprehensive guide, we’re going to dive deep into MATLAB’s plotting architecture. We’ll explore the Handle Graphics system—the object-oriented underpinnings of all MATLAB plots. We’ll master the creation of both 2D and 3D visualizations, from simple line plots to complex, multi-panel figures suitable for publication. And we’ll understand not just the “how” of creating plots, but the “why”—why MATLAB’s plotting system is designed the way it is, how it differs from other plotting libraries (like Python’s Matplotlib), and how to create visualizations that effectively communicate your data’s story.
Whether you’re preparing a figure for a journal paper, creating an interactive visualization for a presentation, or just exploring your data to gain insights, the skills you’ll learn in this article will serve you well. By the time you finish, you’ll be able to create publication-quality figures that are not just informative, but aesthetically pleasing.
MATLAB’s Plotting Architecture: The Handle Graphics System
To understand how to create effective visualizations in MATLAB, you need to understand the architecture that underlies all plotting—the Handle Graphics system. This isn’t just implementation detail; it’s a conceptual framework that, once mastered, will give you precise control over every aspect of your plots.
The Object Hierarchy of Handle Graphics
In MATLAB’s Handle Graphics system, every element of a plot is an object with properties that you can inspect and modify. The objects are organized in a hierarchy:
ROOT
└── Figure
└── Axes
├── Line
├── Text
├── Surface
├── Patch
└── (other graphics objects)
Let’s break this down:
- ROOT: The root object is at the top of the hierarchy. There’s only one root object, and it’s not something you’ll interact with directly. But it’s useful to know it exists.
- Figure: A figure is the top-level container for plots. It corresponds to a window on your screen (or a “page” if you’re saving to a file format like PDF). A figure contains one or more axes objects.
- Axes: An axes object defines a coordinate system. It’s the rectangle (or 3D volume) where your data is plotted. An axes contains graphics objects like lines, surfaces, text, etc.
- Graphics Objects: These are the actual visualizations of your data. Examples include:
Line: Created byplot(),plot3(),semilogx(), etc.Surface: Created bysurf(),mesh(),pcolor(), etc.Text: Created bytext(),xlabel(),title(), etc.Patch: Created byfill(),patch(), etc.Scatter: Created byscatter().- And many more.
Handles: Your Key to Modifying Plots
In the Handle Graphics system, every object has a handle—a unique identifier that you can use to modify the object’s properties.
When you create a plot using a function like plot(), the function returns the handle to the graphics object it created:
x = linspace(0, 2*pi, 100);
y = sin(x);
% Create a plot, and capture the handle
h_line = plot(x, y);
% h_line is now a handle to the Line object
% You can use it to modify the line's properties
set(h_line, 'Color', 'red', 'LineWidth', 2);
But you don’t always need to capture the handle. MATLAB provides several ways to get handles to existing objects:
gcf(Get Current Figure): Returns the handle to the current figure.gca(Get Current Axes): Returns the handle to the current axes.gco(Get Current Object): Returns the handle to the current object (the last one you clicked on, or the last one you created).findobj: Finds objects with specific properties.
% Create a plot
plot(x, y);
% Get the handle to the current figure
h_fig = gcf();
% Get the handle to the current axes
h_axes = gca();
% Get the handle to the line object (the children of the axes)
h_line = findobj(h_axes, 'Type', 'line');
% Now modify properties
set(h_line, 'Color', 'blue');
set(h_axes, 'FontSize', 14);
The set() and get() Functions
The set() and get() functions are your primary tools for modifying and querying object properties.
x = linspace(0, 2*pi, 100);
y = sin(x);
h = plot(x, y);
% --- GET: Query properties ---
% Get all properties and their current values
get(h);
% Get a specific property
color = get(h, 'Color');
line_width = get(h, 'LineWidth');
% --- SET: Modify properties ---
set(h, 'Color', 'red'); % Change line color to red
set(h, 'LineWidth', 2); % Change line width to 2 points
set(h, 'LineStyle', '--'); % Change to dashed line
set(h, 'Marker', 'o'); % Add circular markers
set(h, 'MarkerSize', 8); % Set marker size
set(h, 'MarkerFaceColor', 'blue'); % Fill markers with blue
This is the power of the Handle Graphics system. Every aspect of your plot is controllable via properties. Want to change the font of the x-axis label? There’s a property for that (FontSize on the XLabel object). Want to make the plot background transparent? There’s a property for that (Color on the Axes object, set to 'none').
A Concrete Example: Customizing Every Aspect of a Plot
Let me walk you through a complete example of creating a plot and customizing it using the Handle Graphics system:
% --- Step 1: Create data ---
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
% --- Step 2: Create figure and axes ---
figure('Name', 'Sine and Cosine', 'Color', 'white', 'Position', [100, 100, 800, 600]);
% --- Step 3: Plot the data ---
h1 = plot(x, y1, 'b-', 'LineWidth', 2); % Sine: blue solid line
hold on;
h2 = plot(x, y2, 'r--', 'LineWidth', 2); % Cosine: red dashed line
hold off;
% --- Step 4: Customize axes ---
h_axes = gca();
set(h_axes, 'FontSize', 12); % Axis label font size
set(h_axes, 'LineWidth', 1.5); % Axis line width
set(h_axes, 'XGrid', 'on'); % Turn on X-axis grid
set(h_axes, 'YGrid', 'on'); % Turn on Y-axis grid
set(h_axes, 'GridLineStyle', ':'); % Dotted grid lines
set(h_axes, 'Box', 'on'); % Frame the plot
% --- Step 5: Add labels and title ---
h_title = title('Sine and Cosine Waves');
set(h_title, 'FontSize', 16, 'FontWeight', 'bold');
h_xlabel = xlabel('Angle (radians)');
set(h_xlabel, 'FontSize', 14);
h_ylabel = ylabel('Amplitude');
set(h_ylabel, 'FontSize', 14);
% --- Step 6: Add legend ---
h_legend = legend('sin(x)', 'cos(x)');
set(h_legend, 'FontSize', 12, 'Location', 'best');
% --- Step 7: Adjust plot limits ---
xlim([0, 2*pi]);
ylim([-1.5, 1.5]);
This might seem like a lot of code for a simple plot. And indeed, for quick-and-dirty plotting, you’d just use:
plot(x, y1, 'b-', x, y2, 'r--');
legend('sin(x)', 'cos(x)');
title('Sine and Cosine');
xlabel('Angle (radians)');
ylabel('Amplitude');
But the power of the Handle Graphics approach becomes apparent when you need to create publication-quality figures. When you need precise control over every aspect of the plot—the exact font sizes, the exact line widths, the exact colors—the Handle Graphics system is unrivaled.



