MATLAB Custom Legend

  1. Add Custom Legends Using the legend() Function in MATLAB
  2. Add Custom Legends Using the text() Function in MATLAB

In this tutorial, we will discuss how to add custom legends to the plot using the legend() and text() function in MATLAB.

Add Custom Legends Using the legend() Function in MATLAB

The legend() function is used to add legends to a plot in MATLAB. But the problem is it only adds one legend on one plot. If you try to add more than one legend using this function, MATLAB will give an error. So to add custom legends that do not have any relation with the graph, you have to add more plots on the figure with undefined values, and then you can add more legends. You will not see the plots because their values are undefined, but you will see the legends. For example, let’s add custom legends to a cosine wave plot. See the code below.

clc
t = 1:0.01:2;
plot(cos(2*pi*t))
hold on
p(1) = plot(NaN,NaN);
p(2) = plot(NaN,NaN);
p(3) = plot(NaN,NaN);
hold off
legend(p,'Cos1','Cos2','Cos3')

Output:

Custom Legend Using the legend() functioin in matlab

In the above code, we added three legends on a cosine wave plot, and you can see they are not related to the plot. You can add as many legends as you want on a figure; you just have to add more plots with undefined values on the same figure. Check this link for more information about the legend() function.

Add Custom Legends Using the text() Function in MATLAB

We can also use the text() function to add text to the plot. You need to pass the x and y coordinate on which you want to place the text. Simply plot the variable and then select the coordinates from the plot and then use the text() function to place the text on the selected coordinates. If you give the coordinates which don’t lie on the plot, you won’t be able to see the text. You can also change the properties of the text like the font size using the FontSize property and the color using the Color property etc. For example, let’s plot a cosine wave and put some text on it with font size 18 and red color. See the code below.

clc
t = 1:0.01:2;
plot(cos(2*pi*t))
t = text(100,0.8,'Cos1','FontSize',18,'Color','r')

Output:

Custom Legend Using the text() function in Matlab

Now let’s add a blue line before the text and a black box around the text and the line. See the code below.

clc
t = 1:0.01:2;
plot(cos(2*pi*t))
tex = text(95,0.8,'{\color{blue} ---} Cos','FontSize',18,'Color','k','EdgeColor','k')

Output:

Custom Legends Using the text() function in Matlab

You can give your desired color to the line by defining it before the line in the cell, the text using the Color property, and the box using the EdgeColor property. You can also add multiple texts at multiple positions on the plot with different text, line styles, color, size, and box. For example, let’s add one more text on the above graph with Cosine text of red color, * marker of green color, a box of yellow color, and 18 font size. See the code below.

clc
t = 1:0.01:2;
plot(cos(2*pi*t))
tex1 = text(95,0.8,'{\color{blue} ---} Cos','FontSize',18,'Color','k','EdgeColor','k')
tex2 = text(89,0.4,'{\color{red} *} Cosine','FontSize',18,'Color','g','EdgeColor','y')

Output:

Custom Legends Using the text() function in Matlab

You can also put multiple strings in the same box by making a cell array of the strings. Check this link for more information about the text() function.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - MATLAB Plot

  • MATLAB Fprintf Table
  • MATLAB Log Plot