make a manual legend for figure in matlab

To make a manual legend for a figure in MATLAB, you can use the legend function. First, you need to plot the data that you want to include in the legend. Then, you can create a legend with custom labels and assign them to the plotted data.

Here's an example:

main.m
% Plot some data
x = 1:10;
y1 = x;
y2 = x.^2;
plot(x, y1, 'b', x, y2, 'r');

% Create a legend with custom labels
legend('Linear', 'Quadratic');
145 chars
9 lines

This code creates a plot with two lines representing linear and quadratic data. Then, it creates a legend with custom labels "Linear" and "Quadratic" that correspond to the plotted data.

You can customize the location of the legend by adding a location specifier to the legend function. For example, legend('Linear', 'Quadratic', 'Location', 'northwest') would place the legend in the northwest corner of the figure.

Note that if you want to include data in the legend that was not plotted (e.g. a horizontal line or text), you can create a "dummy" plot with the desired attributes and then add it to the legend. For example:

main.m
% Create a horizontal line
line_x = [3, 7];
line_y = [5, 5];
line(line_x, line_y, 'Color', 'g', 'LineStyle', '--');

% Add the line to the legend
legend('Linear', 'Quadratic', 'Horizontal line', 'Location', 'northwest');
221 chars
8 lines

Here, we create a horizontal line with two points at (3,5) and (7,5) and add it to the plot. Then, we add the line to the legend by including an additional label in the legend function, with the desired location specifier.

related categories

gistlibby LogSnag