标记不同的图形、字体和大小MATLAB

4
我正在尝试为我的期末练习基本复制这个图表,但我不知道如何更改字体、大小或标记轴。简而言之,我需要从我的代码中完全复制此图表。我需要字体为Times New Roman、大小为18,标记大小为8。我该如何将我的代码格式化为此格式?
这是我的代码:
clear
clc

x = linspace(0,2);
y1 = sin(2*pi*x);
y2 = exp(-0.5*2*pi*x).*sin(2*pi*x);


figure
subplot(2,1,1);
hPlot1 = plot(x,y1,'rs');
ylabel('f(t)')
set(gca,'YLim',[-1 2],'YTick',-1:1:2,'XTick',0:.5:2)

subplot(2,1,2);
hPlot2 = plot(x,y2,'k*');
xlabel('Time(s)')
ylabel('g(t)')
set(gca,'YLim',[-0.2,0.6],'YTick',[-0.2,0,0.2,0.4,0.6],'XTick',0:.5:2)
2个回答

4
下面是代码示例:
%// x = linspace(0,2); %// changed that to respect where the markers are on your example figure 
x = 0:0.1:2 ; 
y1 = sin(2*pi*x);
y2 = exp(-0.5*2*pi*x).*sin(2*pi*x);

figure
h.axtop = subplot(2,1,1) ;
h.plottop = plot(x,y1,'LineStyle','-','Color','r', ...
                    'Marker','s', ...
                    'MarkerEdgeColor','k', ...
                    'MarkerFaceColor','none', ...
                    'MarkerSize',8) ;
set(gca,'YLim',[-1 2],'YTick',-1:1:2,'XTick',0:.5:2)
h.ylbl(1) = ylabel('\itf(t)') ;     %// label is set in "italic" mode with the '\it' tag at the beginning

h.axbot = subplot(2,1,2);
h.plotbot = plot(x,y2,'-ks', ...
                    'Marker','*', ...
                    'MarkerEdgeColor','r', ...
                    'MarkerSize',8) ;
set(gca,'YLim',[-0.2,0.6],'YTick',[-0.2,0,0.2,0.4,0.6],'XTick',0:.5:2)
h.xlbl(1) = xlabel('Time(s)') ;
h.ylbl(2) = ylabel('\itg(t)') ;     %// label is set in "italic" mode with the '\it' tag at the beginning

%// create the "text" annotations
h.txttop = text(0.5,1.5, 'Harmonic force \itf(t)=sin(\omegat)' , 'Parent',h.axtop ) ;                   %// note the 'parent' property set to the TOP axes
h.txtbot = text(0.5,0.3, 'Forced response \itg(t)=e^{\zeta\omegat} sin(\omegat)' , 'Parent',h.axbot ) ; %// note the 'parent' property set to the BOTTOM axes

%// set the common properties for all text objects in one go
set( [h.xlbl h.ylbl h.txttop h.txtbot] , 'FontName','Times New Roman' , 'FontSize',18)

将产生以下图形: figure 请注意,图形对象的句柄被保存并重复使用以后设置属性。如果多个图形对象(即使不同)具有相同的属性,则可以一次将此属性分配给所有图形对象。
有关如何在图形上放置注释的详细信息,请参阅Matlab的text函数文档。

3

xlabel('Time(s)')替换为:

xlabel('Time(s)','FontName','TimesNewRoman','FontSize',18)

对于ylabel,也要执行同样的操作。

对于标记大小,请用hPlot1 = plot(x,y1,'rs');替换为

hPlot1 = plot(x,y1,'r-',x(1:5:end),y1(1:5:end),'ks','MarkerSize',8);

对于另一个图形也是同样的操作。

最后,你可以使用text函数在图中添加文本,例如:

text(0.5,1.5,'Harmonic force f(t) = sin(\omega t)')

同样,你可以像更改xlabelylabel一样更改字体大小和字体名称。


嘿,谢谢!图表都是正确的,但由于某种原因,在我的MATLAB R2014a上文本没有显示出来,但在使用运行Linux和matlab 2013的实验室计算机时却可以。顺便说一句,我正在运行Windows 8,命令显示“无法解释LaTeX”。 - Stan-Lee
你可能需要将解释器设置为“latex”:text(0.5,1.5,'Harmonic force f(t) = sin(\omega t)','interpreter','latex') - am304

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接