legend1.m

Control line and text entries in a legend object, individually.
274 Downloads
Updated 26 Dec 2012

View License

In the case of a multi-line plot where individual lines are created using a loop, one can easily control each line's properties, but it is less clear how to control the properties of each legend entry. This function gives control over some basic properties of individual legend entries: line color, text, text color, text font size.

Example:

nentries = 4;
colors = [0 0 1; 1 0 0; 0 1 0; 0 0 0]; % number of lines needs to match nentries
leglabels = {'Blue';'Red';'Green';'Black'}; % number of cells needs to match nentries

% create 4 curves
t = [1:100];
for ic=1:nentries
curve(ic,:) = rand(1,length(t)) + [t(1):t(end)]*0.1*ic;
end

% plot curves using for-end loop, insert legend
figure
for ic=1:nentries
plot(curve(ic,:),'Color',colors(ic,:))
hold on
end
legend1 (leglabels,colors,12)

Cite As

Stavros Zanos (2024). legend1.m (https://www.mathworks.com/matlabcentral/fileexchange/39505-legend1-m), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2011a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.1.0.0

Fixed error with output handle.

1.0.0.0