How to Create A Subplot In MATLAB?

12 minutes read

Creating a subplot in MATLAB allows you to display multiple plots or graphics side by side in a single figure window. Subplots are commonly used to compare different data sets, visualize different aspects of a dataset, or present multiple related plots together. Here's how to create a subplot in MATLAB:

  1. Begin by creating a new figure window using the figure command. This is where all the subplots will be displayed.
  2. Determine the number of rows and columns you want in your subplot grid. This represents the layout of the plots in the figure window. For example, if you want to display 4 plots in a 2x2 layout, you will have 2 rows and 2 columns.
  3. Use the subplot command to specify the location and size of each individual subplot within the grid. The syntax for the subplot command is subplot(rows, columns, plot_number). The plot_number represents the position of the subplot from left to right, top to bottom.
  4. Start plotting in the first subplot by using MATLAB's normal plotting commands, such as plot, scatter, or bar. Each subsequent plotting command will be relative to the currently active subplot.
  5. After plotting in the first subplot, move to the next subplot using the subplot command with the appropriate plot_number. Repeat the plotting process for each subplot, adjusting the plot_number accordingly.
  6. If desired, you can add titles, labels, legends, or other annotations to each subplot using the respective plotting commands like title, xlabel, and legend.
  7. Finally, customize the overall figure by adding a title, adjusting the size or position, or saving it as an image file using the available figure commands.


By following these steps and using appropriate parameters, you can create subplots in MATLAB to effectively display and compare multiple plots within a single figure window.

Best Matlab Books to Read in 2024

1
MATLAB: An Introduction with Applications

Rating is 5 out of 5

MATLAB: An Introduction with Applications

2
MATLAB for Engineers

Rating is 4.9 out of 5

MATLAB for Engineers

3
MATLAB: A Practical Introduction to Programming and Problem Solving

Rating is 4.8 out of 5

MATLAB: A Practical Introduction to Programming and Problem Solving

4
MATLAB For Dummies (For Dummies (Computer/Tech))

Rating is 4.7 out of 5

MATLAB For Dummies (For Dummies (Computer/Tech))

5
Beginning MATLAB and Simulink: From Beginner to Pro

Rating is 4.6 out of 5

Beginning MATLAB and Simulink: From Beginner to Pro

6
MATLAB and Simulink Crash Course for Engineers

Rating is 4.5 out of 5

MATLAB and Simulink Crash Course for Engineers

7
MATLAB and Simulink In-Depth: Model-based Design with Simulink and Stateflow, User Interface, Scripting, Simulation, Visualization and Debugging

Rating is 4.4 out of 5

MATLAB and Simulink In-Depth: Model-based Design with Simulink and Stateflow, User Interface, Scripting, Simulation, Visualization and Debugging

8
Matlab: A Practical Introduction to Programming and Problem Solving

Rating is 4.3 out of 5

Matlab: A Practical Introduction to Programming and Problem Solving


What is the command for creating a subplot grid with unequal columns and rows in MATLAB?

In MATLAB, you can use the subplot function to create a subplot grid with unequal columns and rows. To achieve this, you need to specify a position vector [left, bottom, width, height] for each subplot using the position argument of the subplot function.


Here is an example that creates a subplot grid with 2 columns and 3 rows, where the first subplot occupies the entire first row, and the second subplot occupies the second and third rows:

1
2
3
4
5
6
7
% Create subplot grid
figure;
subplot('Position', [0.1, 0.6, 0.8, 0.3]);
plot(x1, y1);

subplot(2, 1, 2);
plot(x2, y2);


In this example, the first subplot is specified with the position vector [0.1, 0.6, 0.8, 0.3], where 0.1 represents the left position of the subplot, 0.6 represents the bottom position, 0.8 represents the width, and 0.3 represents the height. The second subplot is created using the standard subplot function, specifying 2 as the number of rows, 1 as the number of columns, and 2 as the position.


By adjusting the position vectors of each subplot, you can create a subplot grid with unequal columns and rows according to your specific requirements.


What is the command for inserting a blank subplot in MATLAB?

The command for inserting a blank subplot in MATLAB is:

1
subplot(m, n, p, 'replace')


Here, m and n are the number of rows and columns in the subplot grid, and p is the position of the subplot. The 'replace' option ensures that the specified subplot is blank.


For example, to create a 2x3 subplot grid and insert a blank subplot in position 4, you would use:

1
subplot(2, 3, 4, 'replace')



How to remove the spacing or padding between subplots in MATLAB?

To remove the spacing or padding between subplots in MATLAB, you can use the subplot function in combination with the Position property. Here is a step-by-step guide:

  1. Create your subplots by using the subplot function. For example, if you want a 2x2 grid of subplots, you can use the following code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
subplot(2, 2, 1);
plot(x1, y1);

subplot(2, 2, 2);
plot(x2, y2);

subplot(2, 2, 3);
plot(x3, y3);

subplot(2, 2, 4);
plot(x4, y4);


  1. By default, MATLAB adds spacing between the subplots. To remove this spacing, you need to access each subplot's Position property and adjust it accordingly.


First, get the handles to the subplots using the gca function, and store them in an array:

1
axesHandles = findobj(gcf, 'type', 'axes');


  1. Next, iterate over the array and adjust the Position property of each subplot:
1
2
3
4
for i = 1:length(axesHandles)
    pos = get(axesHandles(i), 'Position');
    set(axesHandles(i), 'Position', [pos(1), pos(2), 1/2, 1/2]);
end


The above code resizes each subplot to half of its original size, effectively removing the spacing between them. You can adjust the 1/2 value to further customize the subplot sizes as per your requirement.

  1. Finally, you may need to adjust the overall figure size to accommodate the resized subplots, so they do not overlap. Use the set function to adjust the Position property of the figure:
1
set(gcf, 'Position', [100, 100, 800, 600]);


In the above code, the Position property of the figure is set to [100, 100, 800, 600], where the first two elements determine the position of the lower-left corner of the figure window, and the last two elements determine the width and height of the figure window.


By following the above steps, you can remove the spacing or padding between subplots in MATLAB.


How to add titles to subplots in MATLAB?

To add titles to subplots in MATLAB, you can use the title function. Here is a step-by-step guide:

  1. Create subplots using the subplot function. For example, to create a 2x2 subplot grid, use the following code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
subplot(2,2,1)
% Plot data for the first subplot

subplot(2,2,2)
% Plot data for the second subplot

subplot(2,2,3)
% Plot data for the third subplot

subplot(2,2,4)
% Plot data for the fourth subplot


  1. After plotting the data for each subplot, use the title function to add a title to each subplot. For example, to add titles to the subplots created above, use the following code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
subplot(2,2,1)
% Plot data for the first subplot
title('First Subplot Title')

subplot(2,2,2)
% Plot data for the second subplot
title('Second Subplot Title')

subplot(2,2,3)
% Plot data for the third subplot
title('Third Subplot Title')

subplot(2,2,4)
% Plot data for the fourth subplot
title('Fourth Subplot Title')


  1. Customize the titles by specifying additional arguments to the title function. For example, you can change the font size, font weight, or font color. Here is an example that sets the title font size to 14 and the font weight to 'bold':
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
subplot(2,2,1)
% Plot data for the first subplot
title('First Subplot Title', 'FontSize', 14, 'FontWeight', 'bold')

subplot(2,2,2)
% Plot data for the second subplot
title('Second Subplot Title', 'FontSize', 14, 'FontWeight', 'bold')

subplot(2,2,3)
% Plot data for the third subplot
title('Third Subplot Title', 'FontSize', 14, 'FontWeight', 'bold')

subplot(2,2,4)
% Plot data for the fourth subplot
title('Fourth Subplot Title', 'FontSize', 14, 'FontWeight', 'bold')


By following these steps, you can easily add titles to your subplots in MATLAB.


How to create multiple plots on a single figure in MATLAB?

To create multiple plots on a single figure in MATLAB, you can use the subplot function or the axes function. Here's how to use both methods:

  1. Using subplot: In the subplot approach, you specify the number of rows and columns in the figure grid, and then specify the current subplot position before plotting each individual plot. % Create a figure with a 2x2 grid figure; % Plot the first subplot subplot(2, 2, 1); plot(x1, y1); % Your first plot % Plot the second subplot subplot(2, 2, 2); plot(x2, y2); % Your second plot % Plot the third subplot subplot(2, 2, 3); plot(x3, y3); % Your third plot % Plot the fourth subplot subplot(2, 2, 4); plot(x4, y4); % Your fourth plot In this example, a figure with a 2x2 grid is created using subplot(2, 2, pos), where pos represents the position of the subplot. You can then plot each individual plot using plot(x, y).
  2. Using axes: In the axes approach, you manually specify the position and size of each subplot using normalized coordinates (values between 0 and 1). % Create a figure figure; % Specify the position of the first subplot ax1 = axes('Position', [0.1, 0.55, 0.35, 0.35]); plot(x1, y1); % Your first plot % Specify the position of the second subplot ax2 = axes('Position', [0.55, 0.55, 0.35, 0.35]); plot(x2, y2); % Your second plot % Specify the position of the third subplot ax3 = axes('Position', [0.1, 0.1, 0.35, 0.35]); plot(x3, y3); % Your third plot % Specify the position of the fourth subplot ax4 = axes('Position', [0.55, 0.1, 0.35, 0.35]); plot(x4, y4); % Your fourth plot In this example, you create four individual axes objects using axes('Position', [left, bottom, width, height]), where left, bottom, width, and height are the normalized coordinates. You can then specify the position and size of each subplot manually and plot each individual plot using plot(x, y).


Both methods allow you to create multiple plots on a single figure. Choose the one that suits your requirements and desired layout.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To delete an empty MATLAB structure in Python, you can follow these steps:Import the matlab package from the scipy library: from scipy import matlab Convert the MATLAB struct to a Python dictionary using the matlab.mio module: python_dict = matlab.mio.savemat(...
To pass an array from Excel to Matlab, you can use the following steps:In Excel, arrange your data in a column or row.Select and copy the data.Open Matlab.Create a new variable in Matlab that will store the array. For example, you can name it "excelData&#3...
Creating a Graphical User Interface (GUI) in MATLAB allows you to design an interactive application with buttons, menus, sliders, and other components. Here is a brief explanation of how to create a GUI in MATLAB:Open MATLAB: Launch MATLAB software on your com...