如何在MATLAB中改变图例的显示格式 [英] How to change display format of legend in MATLAB

查看:996
本文介绍了如何在MATLAB中改变图例的显示格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种强制以特定格式输入图例的方法。我下面的代码,他们显示像





相反,我希望它像1e-1,1e-2,1e-3,1e-4,1e-5。
有没有办法做到这一点。



MWE:

  sig = [0.1 0.01 0.001 0.0001 0.00001]; 
对于j = 1:长度(sig)
对于x = 1:10
Cost(j,x)= 2 * x + j;
end
plot(1:10,Cost(j,:));
end
legend(strcat('\sigma ^ 2_n =',num2str((sig)')));
set(h,'Interpreter','latex')


解决方案当你通过传递 sig 到一个字符串时,你应该指定你想使用科学符号。



如果要删除指数中的前导 0 ,可以使用正则表达式将它们删除

  S = regexprep(cellstr(num2str(sig。','%.0e')),'(?<= e [ -  +])0 *', ); 
legend(strcat('\sigma ^ 2_n =',S))

< a href =https://i.stack.imgur.com/tY4O4.png =nofollow noreferrer>


I am looking for a way to force the legend entries in a particular format. I following code, they are displayed like

Instead I want it like 1e-1,1e-2,1e-3,1e-4, 1e-5. Is there a way to do this.

MWE:

sig=[0.1 0.01 0.001 0.0001 0.00001];
for j=1:length(sig)
    for x=1:10
       Cost(j,x) = 2*x+j;
    end 
plot(1:10,Cost(j,:));
end 
legend(strcat('\sigma^2_n=',num2str((sig)')));
set(h,'Interpreter','latex')

解决方案

You should specify that you'd like to use scientific notation when you convert sig to a string by passing a custom format specifier to num2str

legend(strcat('\sigma^2_n=',num2str(sig.', '%.0e')));

If you want to remove the leading 0 in the exponent, you can remove them with a regular expression

S = regexprep(cellstr(num2str(sig.', '%.0e')), '(?<=e[-+])0*', '');
legend(strcat('\sigma^2_n=', S))

这篇关于如何在MATLAB中改变图例的显示格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆