MATLAB: Legend with Error Bars

MATLAB

I have a plot that includes several lines with error bars and a legend. I want the legend to only show the different lines, but when I create the legend it also includes a label for the error bars. How do I remove the error bars from the legend?

Best Answer

If you add the "errorbar" after _y_ou add the “legend”, the “legend” will automatically update to include the new “errorbar” handle. There are two ways around this.
You can add the “errorbar” before you add the “legend”:
% code for Line_1 and Line_2

errorbar(x, y, y_lower, y_upper, '.')
legend({'Line_1','Line_2'}) % The legend will only annotate the two labels you provided
Alternatively, if you need to add the “errorbar” after you add the “legend”, you can set the “errorbar” handle to be invisible. This will prevent the “legend” from updating with the new “errorbar” handle:
% code for Line_1 and Line_2
legend({'Line_1','Line_2'})
errorbar(x, y, y_lower, y_upper, '.', 'HandleVisibility','off') % The errorbar handle is not visible, preventing the legend from updating