Matlab

# Adding a Legend to Axes in MATLAB

MATLAB is a powerful programming language and environment used by engineers and scientists for numerical computation, data analysis, and visualization. One of the many features of MATLAB is the ability to add legends to axes in plots. This article covers how we can define legends in MATLAB and add them to axes in MATLAB.

## What is a Legend in MATLAB

A legend is a graphical element that helps identify different data series in a plot. It typically consists of a box containing symbols and text labels that correspond to the data series in the plot. Legends are useful for distinguishing between multiple data series and making plots easier to understand.

## How to Add a Legend to Axes in MATLAB

Adding a legend to axes in MATLAB is easy. Here’s how we can do it:

## Step 1: Create a Plot

First, we need to create a plot. We can do this using any of the plotting functions available in MATLAB. For example, we can use the plot function to create a 2D line plot:

x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
plot(x, y1)
hold on
plot(x, y2)

This code creates a plot with two data series: y1 (a sine wave) and y2 (a cosine wave). ## Step 2: Add a Legend

Once we have created a plot, the legend can be added using the legend function. This function takes as input the text labels that we want to use for each data series. For example:

% Step 1: Create a Plot
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
plot(x, y1)
hold on
plot(x, y2)

% Step 2: Add a Legend
legend('Sine', 'Cosine')

This code adds a legend with two entries: “Sine” and “Cosine”. The first entry corresponds to the first data series (y1) and the second entry corresponds to the second data series (y2). ## Step 3: Customize the Legend

We can customize the appearance of the legend using various properties such as Location, Orientation, and FontSize. For example:

% Step 1: Create a Plot
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
plot(x, y1)
hold on
plot(x, y2)

% Step 2: Add a Legend
legend('Sine', 'Cosine')

% Step 3: Customize the Legend
legend('Sine', 'Cosine', 'Location', 'northwest', 'Orientation', 'horizontal', 'FontSize', 14)

This code adds a legend with two entries “Sine” and “Cosine” and customizes its appearance by setting its location to “northwest”, its orientation to “horizontal”, and its font size to 14. ## Examples of Adding a Legend to Axes in MATLAB

Here are some examples that explain how one can add legends to axes in different types of plots:

## Example 1: Adding a Legend to a 2D Plot

Here’s an example of how we can add a legend to a 2D line plot:

x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
plot(x, y1)
hold on
plot(x, y2)
legend('Sine', 'Cosine')

This code creates a 2D line plot with two data series (y1 and y2) and adds a legend with two entries (“Sine” and “Cosine”). ## Example 2: Adding a Legend to a 3D Plot

Below, the code demonstrates how one can add a legend to a 3D surface plot:

[X, Y] = meshgrid(-5:0.5:5);
Z1 = sin(sqrt(X.^2 + Y.^2));
Z2 = cos(sqrt(X.^2 + Y.^2));
surf(X, Y, Z1)
hold on
surf(X, Y, Z2)
legend('Sine', 'Cosine')

This code creates a 3D surface plot with two data series (Z1 and Z2) and adds a legend with two entries (“Sine” and “Cosine”). ## Example 3: Adding a Legend to a Subplot

Below, the code explains the steps of adding a legend to a subplot:

x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
subplot(1, 2, 1)
plot(x, y1)
title('Sine')
subplot(1, 2, 2)
plot(x, y2)
title('Cosine')
legend('Sine', 'Cosine')

This code creates two subplots: one for the y1 data series (a sine wave) and one for the y2 data series (a cosine wave). It then adds a legend with two entries (“Sine” and “Cosine”) that applies to both subplots. ## Example 4: Adding Different Legends to Multiple Axes

Here’s an example of how we can add legends to multiple axes inside the same figure.

% Create a sample data
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
% Create the figure and the axes
figure;
ax1 = subplot(2,1,1);
ax2 = subplot(2,1,2);
% Plot the data on the first axis
plot(ax1, x, y1, 'LineWidth', 2);
hold(ax1, 'on');
plot(ax1, x, y2, 'LineWidth', 2);
% Set the title and the legend for the first axis
title(ax1, 'Trigonometric Functions');
legend(ax1, {'sin(x)', 'cos(x)'}, 'Location', 'northwest');
% Plot the data on the second axis
plot(ax2, x, y1.^2, 'LineWidth', 2);
hold(ax2, 'on');
plot(ax2, x, y2.^2, 'LineWidth', 2);
% Set the title and the legend for the second axis
title(ax2, 'Squared Trigonometric Functions');
legend(ax2, {'sin^2(x)', 'cos^2(x)'}, 'Location', 'southeast');

In this example, we created sample data x, y1, and y2. We then create a figure with two axes using the subplot function. We plot the sin(x) and cos(x) functions on the first axis, and the squared sin(x) and cos(x) functions on the second axis. Furthermore, we set the title and the legend for each axis using the title and legend functions, respectively.

Note that we use the hold function to ensure that both sin(x) and cos(x) are plotted on the same axis and that both squared functions are plotted on the other axis. ## Conclusion

This article covers different ways of adding legends to axes in MATLAB. Legends are useful for identifying different data series in plots and making them easier to understand. Adding a legend to axes in MATLAB can be done using the legend function. By default, the legend function will include a label for each plotted line, but it is also possible to customize its appearance and placement. Read more on adding legends in MATLAB axes in this article. 