We also learned to set the ‘direction’ and ‘Name’ of the label box as per our needs.
#Legend matlab how to#
We learned how to create labels in MATLAB plots and also how to achieve desired styles. Legend (,'Location','northwest')Įxplanation: As we can notice in the output, our label box is now named.
#Legend matlab code#
All we need to do is pass the pre-defined code for the direction, as an argument. Legend function in MATLAB allows us to put our label in place of our choice. Next, what if we don’t want our labels to be on the top right but in some other place on the plot. Here, we can name our functions as per our needs.Įxplanation: Notice on the top right side of the plot, we have got the names of our functions. In addition to the above code, we will add the below-mentioned line:Īs we can see, we have passed the name of the functions as an argument to our legend function. Our initial code will be the same as in the above example Now, what if instead of ‘data1’ and ‘data2’, we want to have the name of the function as the label. Since nothing was passed as an argument to legend function, MATLAB created labels as ‘data1’ and ‘data2’. This is how our input and output will look like in MATLAB console:Įxplanation: As we can see in the above output, we have plotted 2 vectors and our legend function created corresponding labels. Our inputs A, B& C are first passed as arguments to the function ‘plot’.Īnd then we simply write ‘legend’ in our code to get the labels. Now to understand how ‘Legend’ works, we will first plot our input functions and then use the function ‘legend’. Next, we will define B as the cos function of values of A and C as sine function of values of A We will define an increment of π/50 between these values. Let us understand the function with an example: Example #1įirst, we will define ‘A’ as a vector containing values between 2pi (π) and 3π. The function ‘legend’ will create labels like ‘data1’, ‘data2’, and so on.
#Legend matlab series#
, L N), where L1, L2 and so on represents the respective labels.Įxplanation: This function will create a legend for each data series used in the plot, with descriptive labels. Check this link for more information about the text() function.Hadoop, Data Science, Statistics & others You can also put multiple strings in the same box by making a cell array of the strings. Now let’s add a blue line before the text and a black box around the text and the line. For example, let’s plot a cosine wave and put some text on it with font size 18 and red color. 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. If you give the coordinates which don’t lie on the plot, you won’t be able to see 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. You need to pass the x and y coordinate on which you want to place the text. We can also use the text() function to add text to the plot. Add Custom Legends Using the text() Function in MATLAB
Check this link for more information about the legend() function.
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. In the above code, we added three legends on a cosine wave plot, and you can see they are not related to the plot. For example, let’s add custom legends to a cosine wave plot. You will not see the plots because their values are undefined, but you will see the legends. 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. If you try to add more than one legend using this function, MATLAB will give an error. But the problem is it only adds one legend on one plot. The legend() function is used to add legends to a plot in MATLAB. Add Custom Legends Using the legend() 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.