# How To Make A Legend In Matlab

How To Articles

Hey there! Today I want to share with you my personal experience and some tips on how to create a legend in MATLAB. Adding a legend to your MATLAB plots can be really helpful to provide clear and concise explanations of the data being displayed. So, let’s jump right into it!

First, let’s understand what a legend is. In MATLAB, a legend is a graphical representation that helps to identify different elements in a plot. It usually consists of labels and corresponding symbols or colors that represent the different data series or objects in the plot.

Creating a legend in MATLAB is a straightforward process that can be done in just a few steps. Here’s how:

## Step 1: Plot your data

The first step is to plot your data using MATLAB’s plotting functions, such as `plot()` or `scatter()`. This will create the visual representation of your data on the plot.

## Step 2: Define labels for your data series

Next, you need to define meaningful labels for each of the data series or objects that you want to include in the legend. These labels will help the viewers understand what each data series represents.

## Step 3: Create the legend

Now comes the fun part! To create the legend, you can use the `legend()` function in MATLAB. This function takes in the labels as input and automatically generates a legend with corresponding symbols or colors for each data series.

For example, let’s say we have plotted two data series, “Temperature” and “Pressure”, and we want to create a legend for them. We can use the following code:

``` x = 1:10; % Example x values y1 = x.^2; % Example data series 1 y2 = x.^3; % Example data series 2```

``` ```

```plot(x, y1, 'r-', x, y2, 'b--'); % Plotting the data series legend('Temperature', 'Pressure'); % Creating the legend ```

By running this code, you should see a plot with the two data series and a corresponding legend displaying the labels “Temperature” and “Pressure”. Easy, right?

Now, you can also customize your legend to match the style and appearance of your plot. MATLAB provides various options to modify the position, font size, font color, and other properties of the legend. You can refer to the MATLAB documentation to explore these customization options and make your legend stand out!

## Conclusion

Creating a legend in MATLAB is a simple yet valuable technique to enhance the clarity and understandability of your plots. By following the steps outlined above, you can easily add a legend to your MATLAB plots and customize it according to your needs. So go ahead and experiment with legends in your MATLAB code to make your visualizations more informative and visually appealing!