
#MATLAB PLOT LEGEND CODE#
Let us plot the simple function y = x for the range of values for x from 0 to 100, with an increment of 5.Ĭreate a script file and type the following code − There are many other properties that are associated with the customization of the legend in Matlab.To plot the graph of a function, you need to take the following steps −ĭefine x, by specifying the range of values for the variable x, for which the function is to be plottedįollowing example would demonstrate the concept. Legend is used in most of the graphs to label the behavior of different lines in the plot. We can also decide the visibility, orientation of the legend which can be horizontal and vertical depending on the requirements.There are many other locations apart from this which are also used in Matlab.westoutside: This is used to plot the legend in the left of the axes and it is outside.west: This is used to plot the legend in the left of the axes or chart and is placed inside of the chart.eastoutside: This is used to plot the legend in the right of the axes and it is outside.east: This is used to plot the legend in the right of the axes or chart and is placed inside of the chart.southoutside: This is used to plot the legend at the bottom of the axes and it is outside.southwest: This is used to plot the legend in the bottom left of the axes or chart.southeast: This is used to plot the legend in the bottom right of the axes or chart.south: This is used to plot the legend at the bottom of the axes or chart and is placed inside of the chart.northoutside: This is used to plot the legend on top of the axes and it is outside.northwest: This is used to plot the legend in the top left of the axes or chart.northeast: This is used to plot the legend in the top right of the axes or chart.north: This is used to plot the legend in top of the axes or chart and is placed inside of the chart.There are various values that can be assigned to the property like: Location is one of the main property of the legend which decides the location of the legend. There are various properties of the legend in Matlab which are discussed below. Here sin(3a) is the value and it is added as legend label to the plot. This can be done by using the legend property ‘DisplayName’ and the label of the legend as its value. In the above example, another line is added to an existing plot using the legend label in the plot command as shown in the highlighted part. In this example, we have added another line to the existing line as mentioned in the above example. To plot another line and specify the labels of the legend using DisplayName property of the legend in the plot command. In the above example, the legend for the two lines is shown in the top right corner of the plot, one line depicting the values for the function sin(a) and the other line depicting the values for the function sin(2a). To plot two lines and display the legend in the present chart. Please find the below examples that explain the use of legends in Matlab: Example #1 L=legend(off): This is used to delete the legend and its customization from the plot.L=legend(visibility): This is used to set the visibility of the legend which has different values like the show, hide and toggle.L=legend(bkgd): This is used to set the background appearance and its outline.

There are various properties of the legends which are discussed below. We can give the property name in ‘Name of the property’ argument and the required value after that. L=legend (_,’Name of the property’, ‘value of the property’): This is used when we want to customize the appearance of legends in the plot.

The default value of the orientation is vertical.

