Matlab

How To Add a Title to Each Subplot in MATLAB

Adding titles to subplots in MATLAB can greatly enhance the clarity and organization of multi-panel figures. With clear titles, it becomes easier to identify and interpret the contents of each subplot. In this article, we will explore different methods to add titles to individual subplots in MATLAB, enabling you to create professional-looking visualizations with informative annotations.

How to Add a Title to Each Subplot in MATLAB

When plotting multiple data sets in MATLAB, it can be helpful to add a title to each subplot to identify the data set being plotted, and there are a few different ways to do this.

Method 1: Using the title() Function

The title() function in MATLAB is the most convenient and straightforward method for adding titles to subplots. This function allows you to specify the title text and apply it to a specific subplot. Here’s an example code snippet demonstrating this approach:

% Generate x values

x = linspace(0, 2*pi, 100);

% Generate y values for each line

y1 = sin(x);

y2 = cos(x);

y3 = tan(x);

% Create a figure with three subplots

figure;

% First subplot

subplot(3, 1, 1);

plot(x, y1);

title('Sine Function');

% Second subplot

subplot(3, 1, 2);

plot(x, y2);

title('Cosine Function');

% Third subplot

subplot(3, 1, 3);

plot(x, y3);

title('Tangent Function');

% Adjust overall figure title position

sgtitle('Plotting Three Lines in Subplots', 'FontSize', 10);

First, the code generates x-values using the linspace() function to create a vector ranging from 0 to 2π with 100 points. Next, the y-values for each line are computed by applying the sin(), cos(), and tan() trigonometric functions to the x-values.

Next, a figure with three subplots is created using the subplot function. Each subplot is specified using the parameters (3, 1, n), where n denotes the position of the subplot. In the first subplot, the sin() function is plotted, and a title of Sine Function is added using the title function. Similarly, the cosine and tangent functions are plotted in the second and third subplots, respectively, with appropriate titles assigned to each subplot.

Finally, the sgtitle() function is used to add an overall title to the figure, stating Plotting Three Lines in Subplots, with a font size of 10.

Method 2: Using Axes Annotations

Another approach to adding titles to subplots is by utilizing the annotation() function to create text annotations within each subplot. By utilizing this approach, you gain greater flexibility in customizing the placement and visual attributes of the titles. Here is a code snippet exemplifying the implementation of this method:

% Generate x values

x = linspace(0, 2*pi, 100);

% Generate y values for each line

y1 = sin(x);

y2 = cos(x);

y3 = tan(x);

% Create a figure with three subplots

figure;

% First subplot

subplot(3, 1, 1);

plot(x, y1);

annotation('textbox', [0.13, 0.8, 0.1, 0.1], 'String', 'Sine Function', 'FontSize', 12, 'FontWeight', 'bold', 'EdgeColor', 'none');

% Second subplot

subplot(3, 1, 2);

plot(x, y2);

annotation('textbox', [0.13, 0.51, 0.1, 0.1], 'String', 'Cosine Function', 'FontSize', 12, 'FontWeight', 'bold', 'EdgeColor', 'none');

% Third subplot

subplot(3, 1, 3);

plot(x, y3);

annotation('textbox', [0.13, 0.22, 0.1, 0.1], 'String', 'Tangent Function', 'FontSize', 12, 'FontWeight', 'bold', 'EdgeColor', 'none');

% Adjust overall figure title position

sgtitle('Plotting Three Lines in Subplots with Annotations', 'FontSize', 14);

First, the code generates x-values using the linspace() function to create a vector ranging from 0 to 2π with 100 points. Subsequently, the y-values for each line are computed by applying the sin(), cos(), and tan() trigonometric functions to the respective x-values.

A figure with three subplots is created using the subplot function. In each subplot, a line plot is generated with the respective x and y values. To add titles, the annotation function is used. The annotations are positioned using the textbox annotation type, specifying the coordinates for the position, the text string for the title, the font size, font weight, and edge color.

For the initial subplot, the title Sine Function is incorporated with the desired attributes to provide a clear description of the plot. Similarly, the Cosine Function title is added to the second subplot, and the Tangent Function title to the third subplot.

Finally, the sgtitle() function is used to add an overall title to the figure, stating Plotting Three Lines in Subplots with Annotations, with a font size of 14.

Conclusion

Adding titles to each subplot in MATLAB is essential for providing context and improving the readability of multi-panel figures. Whether you choose to use the title() function or utilize annotations, both methods offer flexibility and control over the appearance of the titles.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.