MATLAB Multiple Plots
Take your programming skills to the next level with interactive lessons and real-world projects.
Explore Coddy →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.
Subplot Command
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.
Basic Syntax
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 grid
Example: Creating a 2x2 Grid of Plots
x = 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')
Hold Command
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.
Example: Plotting Multiple Functions on the Same Axes
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
Figure Command
The figure command creates new figure windows, allowing you to display multiple plots in separate windows.
Example: Creating Multiple Figure 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')
Best Practices
- Use
subplotwhen comparing related data sets - Apply
holdfor overlaying plots with shared axes - Utilize
figurefor independent, detailed plots - Always label axes and include legends for clarity
- Consider using MATLAB Plot Customization techniques to enhance readability
Advanced Techniques
For more complex visualizations, consider exploring these advanced topics:
- MATLAB 3D Plots for multi-dimensional data
- MATLAB Specialized Plots for specific data types
- MATLAB App Designer for interactive plot layouts
By mastering multiple plot techniques in MATLAB, you'll be able to create compelling visualizations that effectively communicate your data analysis results.