
In this example, the plot command specifies a solid, red line ( '- r') for the cosine function and a dash-dot, blue line ( '. If your mouse has more than one button, you press the left mouse button.Įxamples Add a legend to a graph showing a sine and cosine function: You move the legend by pressing the mouse button while the cursor is over the legend and dragging the legend to a new location. legend positions the legend based on a variety of factors, such as what objects the legend obscures. I want to create two legends positioned at NorthWest and NorthEast of the figure such that the first legend represents the first three curves and the second legend represents the second set of three curves. MATLAB displays only one legend per Axes. I am plotting six curves in a MATLAB figure. By default, the legend annotates the current Axes. Remarks legend associates strings with the objects in the Axes in the same order that they are listed in the Axes Children property. But the problem is it only adds one legend on one plot. Returns a handle to the legend, which is an Axes graphics object. The legend () function is used to add legends to a plot in MATLAB.

Legend matlab series#
The legend updates automatically whenever we include any new or remove data series from the current axes. For labels, it uses one of the properties of data series which is known as DisplayName. Bubble legends are useful for showing the relationship between the bubbles and the size data for. pos = 1 places the legend in the upper-right corner of the Axes (default). In Matlab, legends are used to label the plotted data for every parameter that is mentioned. bubblelegend creates a bubble legend in the current axes.pos = 0 places the legend inside the Axes boundary, obscuring as few points as possible.pos = -1 places the legend outside the Axes boundary on the right side.Uses pos to determine where to place the legend. Specifies the legend for the Axes specified by h. Removes the legend from the current Axes or the Axes specified by h. legend(h,Strings)Īssociates each row of the matrix Strings with the corresponding graphics object in the vector h. This is the same as legend(Strings(1,:),Strings(2,:).). legend(Strings)Īdds a legend containing the rows of the matrix Strings as labels. For each line plotted, the legend shows a sample of the line type, marker symbol, and color beside the text label you specify. legend(' string1',' string2'.)ĭisplays a legend in the current Axes using the specified strings to label each set of data. I am plotting six curves in a MATLAB figure. Description legend places a legend on various types of graphs (line plots, bar graphs, pie charts, etc.). The basic syntax is: legend( ‘Description 1’, ‘Description 2’, ). The simplest way to use the function is to pass in a character string for each line on the plot. After the legend appears, you can move it using the mouse. The legend() function in MATLAB/Octave allows you to add descriptive labels to your plots. When plotting filled areas, the legend contains a sample of the face color next to the text label.


For each line in the plot, the legend shows a sample of the line type, marker symbol, and color beside the text label you specify. You can also use copyobj to copy graphics elements from one figure to another if you have a lot of elements, then use set(x, 'visible', 'off') to hide them before showing the legend, but it depends on what your final application is.Legend (MATLAB Function Reference) MATLAB Function Referenceĭescription legend places a legend on a graph. This will plot the additional points, but they will not be visible on the plot itself.
Legend matlab code#
The updated code above works, but the answer below is only relevant pre-2016b: figure This will plot the additional points, but because the coordinates are at NaN they will not be visible on the plot itself:ĮDIT : My original answer results in greyed out legend entries in 2016b. This is how I have solved this problem in the past: figure
