

Rather than providing character strings for all of the lines on the plot, it is possible to specify an array of specific line objects that should be included in the legend. Image by author Adding Specific Lines to the Legend 'style', 'pushbutton', 'string', 'Click Here' ) 'style', 'pushbutton', 'string', 'Press Me' ) % Set up the subplotĪ = subplot( 'position', ) Building on the previous example, we can mock up an example GUI that includes our plot, a couple of buttons, and a legend that is manually placed on the figure. This ensures that your legend does overlap with other elements or create weird spacing. Whenever you are working with subplots or GUIs, it is often necessary to specify an exact location for the legend. If you are not familiar with setting the position property on graphics object, you can see a brief description here. The position property allows us to set the exact position of the legend by specifying its horizontal origin (h0), vertical origin (v0), width (w) and height (h).

One of the most convenient of these properties is the ‘position’ property. Simply call get(hl) in the command window to display them all. There are number of other properties that can be set on the legend object. Set( hl, 'box', 'off' ) % turn off the box lines Set( hl, 'fontsize', 16 ) % set the font size

Set( hl, 'location', 'northeast' ) % set the location Set( hl, 'orientation', 'vertical' ) % set the orientation hl = legend( legStr ) % save the legend handle In this particular example, the location is set to northeast, the orientation is set to vertical, the font size is set to 16, and the box outline is removed.
#FIND A VALUE ON A PLOT MATLAB 2012 CODE#
The following code will capture the legend handle and then set a few of the legend properties. Similar to all of the other graphics objects, the legend properties can be adjusted using the set() and get() functions. The resulting plot would be the same as the first example shown earlier in the tutorial. The legend would then be toggled on by calling legend() without any input arguments. H1 = plot( x, sin( x ), 'rx', 'linewidth', 2, 'DisplayName', 'Line1' ) The syntax for the set() should like this. plot( x, sin( x ), 'rx', 'linewidth', 2, 'DisplayName', 'Line1' ) The syntax for the plot() should look like this. In both cases, after you have set the properties, you need to all legend() without any arguments to toggle the legend on. This can be done during the plot() call or using set() on the handle. Image by author Using the “DisplayName” PropertyĪnother convenient way to add the legend labels is to set the “DisplayName property on the lines as they are plotted. legStr = įor convenience, this method will be used for the rest of the examples. This is most useful when you are programmatically creating the legend string. In addition to specifying the labels as individual character strings, it is often convenient to collect the strings in a cell array. Plot( x, cos( 4x )/3, '^g', 'linewidth', 2 ) Ī legend can be added with the following command. Plot( x, sin( x ), 'rx', 'linewidth', 2 ) The basic syntax is: legend( ‘Description 1’, ‘Description 2’, … ).įor the examples in this section, we will generate a sample figure using the following code. The simplest way to use the function is to pass in a character string for each line on the plot. The legend() function in MATLAB/Octave allows you to add descriptive labels to your plots.

The source code for the included examples can be found in the GitHub repository. In this article, I cover the basic use of the legend() function, as well as some special cases that I tend to use regularly. Luckily, MATLAB/Octave include the legend() function which provide some flexible and easy-to-use options for generating legends. Plot legends are essential for properly annotating your figures.
