Matlab绘图时使用latex风格的符号和字体,往往能够使你的图形增色不少。在Matlab中,title、text、xlabel、ylabel和legend均可使用latex风格的符号和字体。多说无益,直接上例子。
E = 2;
t = -5:0.01:5;
y = E*exp(-(t/2).^2);
plot(t,y,'r','LineWidth',2);
axis([-5,5,-0.1,2.2])
set(gca,'XAxisLocation','origin');
set(gca,'YAxisLocation','origin');
title(['$f(t)=2e^{-(\frac{t}{2})^2}$'],'Interpreter','latex','FontSize',20)
set(gca,'FontSize',32);
set(gca, 'XTick',[-4:2:4]);
set(gca, 'YTick',[0:1:2]);
set(gca, 'Box','off')
其中,把title语句中的 [] 去掉,有的Matlab版本也正确。
K = 0.2;
a1 = -0.3;
a2 = 0;
a3 = 0.3;
t=-5:0.1:5;
f1 = K*exp(a1*t);
f2 = K*exp(a2*t);
f3 = K*exp(a3*t);
plot(t,f1,'b','LineWidth',4);
hold on;
plot(t,f2,'r','LineWidth',4);
hold on;
plot(t,f3,'k','LineWidth',4);
axis([-6,6,-0.2,1.2])
set(gca,'XAxisLocation','origin');
set(gca,'YAxisLocation','origin');
text(-5.5,1,'\it{K}e^{\alpha\it{t}}(\alpha < 0)','FontSize',32)
text(3.3,1,'\it{K}e^{\alpha\it{t}}(\alpha > 0)','FontSize',32)
text(4.5,0.25,'\it{K}','FontSize',32)
set(gca,'FontSize',32);
xlabel('t','position',[5.5,0]);
text(-0.8,1.15,'\it{f}(t)','FontSize',32);
alpha = 0:0.01:4*pi;
y1 = sin(alpha )+2;
plot(alpha,y1,'b','LineWidth',2);
axis([0,4*pi,-4,4])
set(gca,'FontSize',32);
ylabel(['$f_{1}(\alpha)=sin(\alpha)+2$'],'Interpreter','latex','FontSize',32);
%set(ylabel(['$f_{1}(\alpha)=sin(\alpha)+2$'],'Interpreter','latex','FontSize',32),'rotation',0,'position',[2.2,3])
box off
也可以将ylabel语句替换成%后的语句得到下图:
xlabel与ylabel类似,不再赘述。
K = 0.2;
a1 = -0.3;
a2 = 0;
a3 = 0.3;
t=-5:0.1:5;
f1 = K*exp(a1*t);
f2 = K*exp(a2*t);
f3 = K*exp(a3*t);
plot(t,f1,'b','LineWidth',4);
hold on;
plot(t,f2,'r','LineWidth',4);
hold on;
plot(t,f3,'k','LineWidth',4);
axis([-6,6,-0.2,1.2])
set(gca,'XAxisLocation','origin');
set(gca,'YAxisLocation','origin');
legend({'$\it{K}e^{\alpha\it{t}}(\alpha < 0)$','$\it{K}e^{\alpha\it{t}}(\alpha > 0)$','$\it{K}$'},'Interpreter','latex')
legend语句中的 {} 不可缺少,且不可替换为 [] 。