MATLAB 绘图函数参考

MATLAB legend 函数为图中曲线添加图例,文字顺序需与曲线绘制顺序一致。

语法

legend('名称1', '名称2', ...)
legend('文字', 'Location', 'northwest')   % 指定位置

输入参数

参数说明
名称各曲线的名字,按顺序对应
'Location'图例位置:north/south/east/west/northwest

示例

x = 0:0.01:10;
plot(x, sin(x), x, cos(x))
legend('sin(x)', 'cos(x)')

用 hold on 分步画完后加图例:

plot(x, sin(x), 'r')
hold on
plot(x, cos(x), 'b--')
hold off
legend('sin', 'cos')

指定位置:

legend('sin', 'cos', 'Location', 'northwest')

注意事项

  • 图例顺序错乱是最常见错误,务必与 plot 顺序一致;
  • 隐藏图例用 legend off