%% Matlab Graphics % This m-file contain the code used in Session 3 of the course "Matlab for % Research" run for PhD students and postdocs in the faculty of Science at % the University of East Anglia. % % This file, and the accompanying course notes, are available at % http://researchpages.net/people/colin-goldblatt/teaching/matlab/ % % Colin Goldblatt, 21/10/2007 %% Line plots figure x = [-20:0.01:20]; y = sin(x); z = cos(x); plot(x,y,'r',x,z,'g') xlabel('x') ylabel('sin(x) and cos(x)') title('A simple graph') legend('sin(x)','cos(x)') print -dpdf graph2.pdf %% figure h1 = plot(x,y,'r') hold on h2 = plot(x,z,'g') set(h1,'color',[ 1 0.5 0],'linewidth',4) set(gca,'xlim',[-pi pi],... 'xtick',[-pi:pi/2:pi],... 'xticklabel',{'-pi','-pi/2','0','pi/2','pi'}) xlabel('-\pi \leq \Theta \leq \pi') width = 12; height = 10; set(gcf,'paperunits','centimeters',... 'PaperSize',[width height],... 'PaperPosition',[0 0 width height]) print -dpdf graph2.pdf %% some more plots figure a = [1:1:10] b = a.^2; plot(a,b,'go--') hold on % annotaion text(2,20,'hello!','color','b') h5 = text(2,40,'hello!'); set(h5,'color','m') % add a filled shape c1 = [4 4 5 5]; c2 = [10 30 30 10]; h3 = fill(c1,c2,'c'); % add another shape and make it transparent with no line d1 = [7 7 8 8]; d2 = [50 60 60 50]; h4 = fill(d1,d2,'r','linestyle','none'); alpha(h4,0.3) %% Subplots (1) a = [0:1:100]; b = randn(size(a)); figure subplot(2,2,1) plot(a,b,'r-') subplot(2,2,2) stem(a,b) subplot(2,2,3:4) scatter(a,b,40,abs(b),'filled') %% Subplots (2) % this produces a 5 x 2 set of subplots which looked right for my thesis sleftoffset = 0.10; srightoffset = 0.04; sbottomoffset = 0.05; stopoffset = 0.023; swidth = 0.5 - sleftoffset - srightoffset; sheight = 0.2 - sbottomoffset - stopoffset; vpos = [0.8 0.6 0.4 0.2 0]; hpos = [0 0.5]; a = [0:1:100]; figure for ih = 1:length(hpos) for iv = 1:length(vpos) subplot('Position',... [hpos(ih)+sleftoffset vpos(iv)+sbottomoffset swidth sheight]) plot(a,randn(size(a)),'r-') xlabel('Time') ylabel('Made up data') end end % add labels to the plot % this uses another axes overlaid on the figure h = axes('Position',[0 0 1 1],'Visible','off'); set(gcf,'CurrentAxes',h) set(h,... 'DefaultTextVerticalAlignment','Cap',... 'DefaultTextHorizontalAlignment','left',... 'DefaultTextFontSize',12,... 'DefaultTextFontWeight','bold'); hold on tposv = [1 0.8 0.6 0.4 0.2]; tposh = [0 0.5]; lab = ['ab';'cd';'ef';'gh';'ij']; for vv = 1:length(tposv) for hh=1:2 text(tposh(hh),tposv(vv),lab(vv,hh)) end end % setup the page size and print the graph width = 12.6; height = 20; set(gcf,'paperunits','centimeters',... 'PaperSize',[width height],... 'PaperPosition',[0 0 width height]) print -dpdf graph3.pdf %% Regression % example of fitting a line to a curve a = [0:0.1:10]; b = 3*a+2 + randn(size(a)); figure plot(a,b,'rx') hold on p=polyfit(a,b,1); plot(a,polyval(p,a),'k-') text(3,5,['y = ',num2str(p(1)),'x + ',num2str(p(2))]) %% Plotting matrix data - caution (1) A = magic(3) figure pcolor(A) shading flat B = NaN*ones(4,4) B(1:3,1:3) = A figure pcolor(B) shading flat figure imagesc(A) axis xy %% Plotting matrix data - caution (2) x = 1:10; y = 1:20; Z = x'*y; figure pcolor(x,y,Z') figure %pcolor(y,x,Z') figure imagesc(x,y,Z') figure imagesc(y,x,Z') figure imagesc(1:1:3,50:-10:-250,Z') %% Plotting matrix data - caution (3) figure pcolor(A) shading interp figure contourf(A) shading flat %% plotting matricies - pretty pictures A = peaks(50); figure imagesc(A) axis xy figure contourf(A) shading flat figure contourf(A,50) shading flat colorbar('southoutside') caxis([-2 2]) colormap gray figure surf(A)