Categories

# Matlab plot title

In MATLAB, the ‘title’ command is used to add a title to any plot or a visual. We can add a single title or a title with a subtitle to a plot. The formatting of the title can be controlled by using pre-defined name-value pairs present in MATLAB. These name-value pairs are passed as arguments to the ‘title’ command to control the formatting of the plot title.

This title helps us in identifying what the plot is about or what it tries to convey.

## Syntax:

1. title(text) will add the title specified as ‘text’ to the plot. Please note that, if the plot already has a title, then using this command will replace the existing title
2. title(text 1, text 2) is used if we need a title and a subtitle. The first argument ‘text 1’ will come as the main title and the second argument ‘text 2’ will come as the subtitle and this will be displayed below the main title
3. title(text ,Name, Value) is used to control the format of the title by using the name value pair argument

## Examples of Matlab plot title

Let us now understand how to give a title to a plot in MATLAB with the help of different examples

## Example 1:

In the first example, we will plot a sine wave in MATLAB and will give it the title “Sine Wave”. The steps to be followed for this example are:

1. Initialize the x-axis
2. Initialize the y-axis
3. Use the ‘plot’ command the plot the sine wave
4. Use the ‘title’ command to give title to this plot

## Code:

``x = linspace(0, 3*pi, 50);``

[Initializing the x axis]

``y = sin(x);``

[Initializing the y axis]

``plot(x, y)``

[Code to plot the sine wave]

``title(‘Sine Wave’)``

[Giving title to the plot created in above step]

This is how our input and output will look like in Matlab command window:

## Input:

``````x = linspace(0, 3*pi, 50);
y = sin(x);
plot(x, y)
title('Sine Wave')``````

## Output:

As we can see in the Output, we have obtained a plot of a sine wave with the required title. Next, we will learn how to give a title and a sub-title to the plot.

## Example 2:

For this example, we will plot a cos wave in MATLAB and will give it the title “Trigonometric Wave” and sub-title “Cos Wave”. The steps to be followed for this example are:

1. Initialize the x-axis
2. Initialize the x-axis
3. Use the ‘plot’ command the plot the cos wave
4. Use the ‘title’ command with 2 text arguments to give a title and a subtitle to this plot

## Code:

``x = linspace(0, 3*pi, 50);``

[Initializing the x axis]

``y = cos(x);``

[Initializing the y axis]

``plot(x, y)``

[Code to plot the cos wave]

``title(‘Trigonometric Wave’, ‘Cos Wave’)``

[Giving title and sub-title to the plot created in above step]

This is how our input and output will look like in MATLAB command window:

## Input:

``````x = linspace(0, 3*pi, 50);
y = cos(x);
plot(x, y)
title('Trigonometric Wave', 'Cos Wave')``````

## Output:

As we can see in the OUTPUT, we have obtained a plot of a cos wave with the required title and a sub-title.

## Example 3:

For this example, we will plot a sine wave and a cos wave in the same graph and will give it the title “Trigonometric Wave” and sub-title “Sine & Cos wave”. The steps to be followed for this example are:

1. Initialize the x-axis
2. Initialize the y-axis
3. Use the ‘plot’ command the plot the sine and cos waves
4. Use the ‘title’ command with 2 text arguments to give a title and a sub-title to this plot

## Code:

``x = linspace(0, 3*pi, 50);``

[Initializing the x axis]

``y = sin(x);``

[Initializing the sine wave]

``plot(x,y)``

[Code to plot the sine wave]

``````hold on
y1 = cos(x);``````

[Initializing the cos wave]

``plot(x, y1)``

[Code to plot the cos wave]

``````hold off
title('Trigonometric waves', 'Sine & Cos wave')``````

[Giving title and sub-title to the plot created in above step]

This is how our input and output will look like in MATLAB command window:

## Input:

``````x = linspace(0, 3*pi, 50);
y = sin(x);
plot(x,y)
hold on
y1 = cos(x);
plot(x, y1)
hold off
title('Trigonometric waves', 'Sine & Cos wave')``````

## Output:

As we can see in the Output, we have obtained a plot of a sine and cos wave with the required title and a sub-title. Next, we will learn how to format the title of the plot.

## Example 4:

For this example, we will plot a sine wave and a cos wave in the same graph as in the above example but will give the title and sub-title in ‘Blue’ color. The steps to be followed for this example are:

1. Initialize the x-axis
2. Initialize the y-axis
3. Use the ‘plot’ command the plot the sine and cos waves
4. Use the ‘title’ command with 2 text arguments and an additional name-value pair argument to specify the blue color

## Code:

``x = linspace(0, 3*pi, 50);``

[Initializing the x axis]

``y = sin(x);``

[Initializing the sine wave]

``plot(x,y)``

[Code to plot the sine wave]

``````hold on
y1 = cos(x);``````

[Initializing the cos wave]

``plot(x, y1)``

[Code to plot the cos wave]

``````hold off
title('Trigonometric waves', 'Sine & Cos wave', 'Color', 'blue')``````

[Giving an extra argument to get the title and sub-title in ‘Blue’ color]

This is how our input and output will look like in MATLAB command window:

## Input:

``````x = linspace(0, 3*pi, 50);
y = sin(x);
plot(x,y)
hold on
y1 = cos(x);
plot(x, y1)
hold off
title('Trigonometric waves', 'Sine & Cos wave', 'Color', 'blue')``````

## Output:

As we can see in the Output, we have obtained a trplot of a sine and cos wave with the required title in blue color.