Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Labeling different figures, font,size MATLAB

I am trying to basically copy this graph for practice for my final coming up but I don't understand how to change the font,size or labeling the axis. Simply put, I need to replicate this graph exactly from my code. I need the font to be times new roman and size 18 with a marker size of 8. How would I format my code into this? enter image description here

This is my code:

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)
like image 228
Stan-Lee Avatar asked Mar 18 '23 16:03

Stan-Lee


2 Answers

The code below:

%// 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)

will produce the following figure: figure

note how the handle of the graphic object were saved and re-used to set properties later on. If multiple graphic object (even different) have the same property, it is possible to assign this property to all the graphic object in one go.

Look at the Matlab text function documentation for more detail on how to place annotations on your figure.

like image 166
Hoki Avatar answered Mar 29 '23 03:03

Hoki


Replace xlabel('Time(s)') by:

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

and do the same for ylabel.

For the marker size, replace hPlot1 = plot(x,y1,'rs'); by

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

and the same for the other plot.

Finally, you can add text to the figure using the text function, e.g.:

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

Again, you can change the font size and font name, as with xlabel and ylabel.

like image 38
am304 Avatar answered Mar 29 '23 01:03

am304