How to Create A Subplot In MATLAB?

9 minutes read

To create a subplot in MATLAB, you can follow these steps:

  1. Open MATLAB and create a new script file.
  2. Define your data or variables that you want to plot in the subplots. For example, let's say you have two vectors x and y representing some data.
  3. Use the subplot function to divide the figure window into multiple subplots. The function has the syntax subplot(m, n, p), where m is the number of rows, n is the number of columns, and p is the position of the subplot.
  4. Specify the position of the first subplot using subplot(m, n, 1). This will set the position of the first subplot to the top-left corner.
  5. Plot your data in the first subplot using the plot function. For example, you can use plot(x, y) to create a line plot.
  6. Specify the position of the second subplot using subplot(m, n, 2). This will set the position of the second subplot to the top-right corner.
  7. Plot your data in the second subplot using the plot function.
  8. Repeat steps 6-7 for any additional subplots you want to create.
  9. Once you have created all the subplots, you can customize them further by adding labels, titles, legends, or any other desired settings.
  10. Finally, display the subplots using the show function.


This will create subplots in MATLAB, allowing you to visualize multiple plots simultaneously in 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 and Simulink In-Depth: Model-based Design with Simulink and Stateflow, User Interface, Scripting, Simulation, Visualization and Debugging

Rating is 4.9 out of 5

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

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 Engineers

Rating is 4.7 out of 5

MATLAB for Engineers

5
Matlab: A Practical Introduction to Programming and Problem Solving

Rating is 4.6 out of 5

Matlab: A Practical Introduction to Programming and Problem Solving

6
MATLAB: A Practical Introduction to Programming and Problem Solving

Rating is 4.5 out of 5

MATLAB: A Practical Introduction to Programming and Problem Solving

7
MATLAB: An Introduction with Applications

Rating is 4.4 out of 5

MATLAB: An Introduction with Applications

8
MATLAB for Engineers (4th Edition)

Rating is 4.3 out of 5

MATLAB for Engineers (4th Edition)


How to create a grid of subplots in MATLAB?

To create a grid of subplots in MATLAB, you can use the subplot function in combination with the subplot indexing syntax. Here's a step-by-step guide:

  1. Decide on the dimensions of the grid. Determine the number of rows (nrows) and columns (ncols) you would like for your grid.
  2. Use the subplot function to create a subplot grid. The syntax for subplot is subplot(nrows, ncols, index). Set the current subplot as the first one: subplot(nrows, ncols, 1).
  3. Create your first plot within the current subplot.
  4. Use a loop or index incrementation to create additional subplots and plots within each subplot. For example, using a loop: for i = 2:(nrows*ncols) subplot(nrows, ncols, i); % Create plot i within the current subplot end Alternatively, using index incrementation: index = 2; while index <= nrows*ncols subplot(nrows, ncols, index); % Create plot index within the current subplot index = index + 1; end
  5. Customize each subplot and plot as desired using various MATLAB plotting functions.


Here's an example code snippet that creates a 2x2 grid of subplots with simple sine wave plots:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
% Define the dimensions of the subplot grid
nrows = 2;
ncols = 2;

% Create the subplot grid
subplot(nrows, ncols, 1);
plot(sin(0:0.1:2*pi));
title('Plot 1');

subplot(nrows, ncols, 2);
plot(sin(0:0.1:4*pi));
title('Plot 2');

subplot(nrows, ncols, 3);
plot(sin(0:0.1:6*pi));
title('Plot 3');

subplot(nrows, ncols, 4);
plot(sin(0:0.1:8*pi));
title('Plot 4');


This will create a 2x2 grid of subplots, and each subplot will contain a sine wave plot with increasing multiples of pi in the x-axis.


How to change the background color of a subplot in MATLAB?

To change the background color of a subplot in MATLAB, you can use the set() function to modify the 'Color' property of the subplot axes. Here's the step-by-step code:

  1. Create a figure and subplot using the subplot() function.
1
2
figure;
subplot(1,1,1);


  1. Get the handle (h) of the subplot axes using the gca() function.
1
h = gca;


  1. Set the background color of the subplot axes to the desired color using the set() function.
1
set(h, 'Color', [r, g, b]);


Replace [r, g, b] with the RGB values of the color you want to set. For example, to set the background color to red, you can use set(h, 'Color', [1, 0, 0]).

  1. (Optional) You can also set the axes' 'XColor', 'YColor', or 'ZColor' properties to change the color of the axis lines, tick labels, and tick marks.


Here's an example that sets the background color of a subplot to light blue:

1
2
3
4
figure;
subplot(1,1,1);
h = gca;
set(h, 'Color', [0.7, 0.9, 1]);


Note that changing the background color of a subplot will also change the color of the plot area, including any lines, markers, or patches drawn on it.


How to remove white spaces between subplots in MATLAB?

To remove the white spaces between subplots in MATLAB and make the subplots appear closer together, you can use the subplot function in combination with adjusting the Position property of each subplot.


Here is an example code that demonstrates how to remove white spaces between subplots:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
% Define subplot grid
nRows = 2; % Number of rows of subplots
nCols = 2; % Number of columns of subplots

% Create figure and subplots
figure;
for i = 1:nRows*nCols
    subplot(nRows, nCols, i);
    plot(rand(10,1)); % Plotting random data for each subplot
end

% Adjust subplot positions
hAllAxes = findall(gcf, 'type', 'axes'); % Get handles to all subplots
set(hAllAxes, 'Position', get(hAllAxes, 'OuterPosition') - ...
    (get(hAllAxes, 'TightInset') - get(hAllAxes, 'Position')));

% Adjust figure size to accommodate subplots closely
figureWidth = 800; % Adjust figure width as desired
figureHeight = 600; % Adjust figure height as desired
set(gcf, 'Units', 'pixels', 'Position', [100, 100, figureWidth, figureHeight]);

% Remove white spaces between subplots
set(gcf, 'Renderer', 'Painters')


In this code, subplot(nRows, nCols, i) is used to create a grid of subplots. Each subplot is then adjusted using the Position property by subtracting the difference between the TightInset and Position values. Finally, the figure size is adjusted to the desired dimensions using set(gcf, 'Units', 'pixels', 'Position', [100, 100, figureWidth, figureHeight]), and the white spaces between subplots are removed by setting the renderer as 'Painters'.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

To import a C++ DLL (Dynamic Link Library) in MATLAB, you need to follow these steps:Create a C++ DLL: First, create a C++ DLL with the functions you want to use in MATLAB. You can use a compiler like Visual Studio or GCC to build the DLL. Exporting Functions:...
To create a graphical user interface (GUI) in MATLAB, you can follow these general steps:First, launch MATLAB and open the GUIDE (GUI Development Environment) by typing guide in the MATLAB Command Window. GUIDE will open a new window with a blank GUI. On the l...
To call a MATLAB script from VB.NET, you can use the MATLAB COM Automation Server. Here&#39;s how you can do it:First, make sure you have MATLAB installed on your machine. Open Visual Studio and create a new VB.NET project. Right-click on your project in the S...