关于MATLAB中legend命令的一些操作

本文介绍了如何在MATLAB中使用legend命令实现横排显示、选择性显示曲线以及在legend中添加带下标和希腊字母。通过设置'Interpreter'属性为'latex',可以利用LaTeX语法美化legend,使得下标效果更佳。使用Mathpix snipping tool工具能方便地将公式转换为LaTeX代码,提升图表的专业性。

1、横排

hl = legend('string1', 'string2');
set(hl,'Orientation','horizon')

2、只显示部分曲线的legend

h1 = plot(data1);
plot(data2);
h2 = plot(data3);
legend([h1,h2],'string1','string2');  % only show the legend of data1 and data3

另外一种方式是直接在plot中指定legend中的内容:

x = linspace(0,pi);
y1 = cos(x);
plot(x,y1,'DisplayName','cos(x)')

hold on 
y2 = cos(2*x);
plot(x,y2,'DisplayName','cos(2x)')
hold off

legend

得到:

3、在legend中使用带下标 希腊字母

最简单的方法:

legend('\tau_{1}')

得到的效果: 

下标的符号如上图所示,希腊字母表间legend的帮助文件。

但是得到的字体明显不好看,特别是那个下标,简直丑爆了。但是,非常好的是MATLAB支持Latex啊,嘿嘿,这真是个好东西。先看下legend帮助文件里面怎么使用Latex的:

By default, MATLAB supports a subset of TeX markup. Use TeX markup to add superscripts and subscripts, modify the font type and color, and include special characters in the text. 

 the Interpreter property set to 'tex'

就是我们需要改下legend的‘Interpreter ’属性就行了,如下:

legend({'$\tau_{1}$'},'Interpreter','latex');

啥,你的legend里面有多个标注,这样来:

legend({'$\tau_{1}$','$\tau_{2}$'},'Interpreter','latex');

怎么生成Latex代码?我的方法是现在公式编辑器里面打入需要的公式,然后用一个叫“Mathpix sniping tool ”的强大公式识别软件(用到就爱上了,再也离不开了),复制到上面的大括号里面就完事了。上结果:

这个下标是不是美滋滋。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值