Starting in R2017a, if a legend is present, it will auto-update with new plots added to the axes.
The legend has been changed so that by default it updates when data is added to or removed from a plot automatically. To prevent this behavior, set the legends "AutoUpdate" property.
x = linspace(0,2*pi);
y = sin(x);
To affect all new legends, set the value on the root level. To affect all legends in a figure:
fig = figure;
To exclude an individual plot from the legend, set the "Annotation" property':
p = plot(1:10,'DisplayName','Line Chart');
s = stem(1:10,'DisplayName','Stem Chart');
s.Annotation.LegendInformation.IconDisplayStyle = 'off';
For more information about this change, refer to the release notes:
Also note that the "default" properties system inherits down the entire graphics hierarchy, as documented here:
One can set the same property on the graphics root and it will apply to all figures in that MATLAB session:
The above line can also be added to the MATLAB startup script if the user wants it to apply it to every MATLAB session.