Start Coding

Topics

MATLAB Multiple Plots

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 grid
  • n: number of columns in the subplot grid
  • p: 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 subplot when comparing related data sets
  • Apply hold for overlaying plots with shared axes
  • Utilize figure for 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:

By mastering multiple plot techniques in MATLAB, you'll be able to create compelling visualizations that effectively communicate your data analysis results.