Creating multiple plots in MATLAB is an essential skill for effective data visualization. This guide will introduce you to various techniques for displaying multiple graphs in a single figure or across multiple figures.
The subplot
function is a powerful tool for creating multiple plots within a single figure window. It divides the figure into a grid of subplots.
subplot(m, n, p)
Where:
m
: number of rows in the subplot gridn
: number of columns in the subplot gridp
: position of the current subplot in the gridx = 0:0.1:2*pi;
subplot(2,2,1)
plot(x, sin(x))
title('Sine Function')
subplot(2,2,2)
plot(x, cos(x))
title('Cosine Function')
subplot(2,2,3)
plot(x, tan(x))
title('Tangent Function')
subplot(2,2,4)
plot(x, exp(x))
title('Exponential Function')
The hold
command allows you to add multiple plots to the same axes. This is useful when you want to compare different data sets in a single graph.
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x, y1)
hold on
plot(x, y2)
legend('sin(x)', 'cos(x)')
title('Sine and Cosine Functions')
hold off
The figure
command creates new figure windows, allowing you to display multiple plots in separate windows.
x = 0:0.1:2*pi;
figure(1)
plot(x, sin(x))
title('Sine Function')
figure(2)
plot(x, cos(x))
title('Cosine Function')
subplot
when comparing related data setshold
for overlaying plots with shared axesfigure
for independent, detailed plotsFor more complex visualizations, consider exploring these advanced topics:
By mastering multiple plot techniques in MATLAB, you'll be able to create compelling visualizations that effectively communicate your data analysis results.