how to make subplots in matlab

Mastering MATLAB Subplots: A Step-by-Step Guide

By William King
AD

Subplots are a powerful tool in MATLAB that allow you to create multiple figures and place them together in a single plot. This is particularly useful when you want to compare data or display complementary information side by side. Here's a comprehensive guide on how to create subplots in MATLAB.

Before we dive into the details, it's important to understand that MATLAB subplots are essentially an arrangement of one or more axes, which are the spaces where your plot data is displayed. Let's explore how to create and customize these subplots step by step.

Creating Simple Subplots

MATLAB provides several functions to create subplots easily. The most basic way is using the subplot function. It takes three inputs: the number of rows (m), columns (n), and the position in the grid (p).

AD
Save a subplot in matplotlib
Save a subplot in matplotlib
Matlab by Examples - Subplot
Matlab by Examples - Subplot
MATLAB 2D Plotting & Data Visualization Cheat Sheet
MATLAB 2D Plotting & Data Visualization Cheat Sheet

Here's an example:

subplot(2,2,1); plot(t, y1);
This command will create a 2x2 grid of subplots and plot the data in the first position ( p=1 ).

What Is a Subplot? Definition & 20+ Examples
What Is a Subplot? Definition & 20+ Examples
MATLAB Subplots
MATLAB Subplots

Using Tight Layout for Better Visualization

By default, MATLAB leaves some space between subplots, which can make them look scattered. The tight_subplot function from the File Exchange can help you create tightly packed subplots. Here's how you can use it:

AD
pos = tight_subplot(2,2,[.01 .03],[.1 .01],[.01 .01]);
This creates a grid with more compact subplots.

Creating Subplots Using a Loop

If you need to create multiple subplots with a loop, MATLAB's for loop along with the subplot function can be helpful. Here's an example:

AD
for k = 1:4, subplot(2,2,k), plot(rand(5)), end;
This will create a 2x2 grid of plots with random data.

Customizing Subplots

Once you've created your subplots, you might want to customize them to better suit your needs. MATLAB provides many properties to fine-tune your subplots.

For example, you can change the Colorbar property to control the color bar's location. You can also use the _title property to add a title to your subplot and the _label property to add labels to the X and Y axes.

AD

Setting Common Properties for Subplots

If you have multiple subplots and want to set common properties (like font size, line width, etc.), you can use the handle property to reference each subplot. Here's an example:

h = findobj(gcf,'type','axes'); set(h,'fontsize',14);
This will change the font size of all axes in the current figure.

Using Grid and Box Properties for Better Visualization

You can also use the Grid and Box properties to make your subplots more readable. The Grid property adds gridlines to your subplots, while the Box property adds a bounding box around your data. Here's how you can use them:

axl = subplot(1,2,1); set(axl, 'GridLineStyle', '-', 'GridColor', 'k');
This will create a subplot with gridlines.

Finally, remember that subplots are a powerful tool to enhance your data visualization. Don't be afraid to experiment with different layouts and styles to find the best presentation of your data. Keep learning and exploring the many features MATLAB offers to create engaging and insightful plots.

AD