matlab繪圖與可視化


 1、設置圖形對象屬性值

set(h,'屬性名稱','屬性值')

>> subplot(1,2,1); h1=line([0 1],[0 1]); text(0,0.5,'unchange'); subplot(1,2,2); h2=line([0 1],[0 1]); set(h2,'linewidth',4); text(0,0.5,'change');

2、基本二維繪圖函數

  1. plot

  

x=0:0.25:2*pi; y=sin(x); plot(x,y,'-ro','linewidth',2,'markeredgecolor','b','markerfacecolor','0.49,1,0.65','markersize',14); >> xlabel('x'); >> ylabel('y')

 

  

  2.  loglog:雙對數坐標繪圖

>> x=logspace(-1,2); loglog(x,exp(x),'-s'); grid on xlabel('x') ylabel('y')

  

  3.  semilogx,semilogy:半對數坐標繪圖

>> x=-10:0.001:10; semilogy(x,2.^x);

  

  4.  polar:極坐標繪圖

>> t=0:0.01:10; polar(t,sin(2*t).*cos(2*t),'--.g')

  

  5.  plotyy:雙縱坐標繪圖

>> x=0:0.1:20; >> y1=200*exp(-0.05*x).*sin(x); >> y2=0.8*exp(-0.5*x).*sin(10*x); >> ax=plotyy(x,y1,x,y2,'plot');xlabel('x'); >> set(get(ax(1),'ylabel'),'string','left y'); >> set(get(ax(2),'ylabel'),'string','right y');

  

3、二維圖形修飾和添加注釋(help+...)

  1.  hold:圖形保持
  2. axis:設置坐標系的刻度及顯示方式
  3. box:顯示或隱藏坐標框
  4. grid:為當前坐標系添加或消除網格
  5. title:添加標題
  6. xlabel、ylabel:為當前坐標軸添加標簽
  7. text:在當前坐標系中添加文本對象
  8. gtext:在當前坐標系中交互式添加文本對象
  9. legend:在當前坐標系中添加 line 對象和 patch 對象的圖形標注框
  10. annotation:在當前圖形窗口建立注釋對象(annotation對象)
  11. subplot:繪制子圖,即在當前圖形窗口以平鋪的方式創建多個坐標系

 

>> t=linspace(0,2*pi,60); >> x=cos(t); >> y=sin(t); >> plot(t,x,'b:','linewidth',3); >> hold on; >> plot(t,y,'r-.','linewidth',1); >> plot(x,y,'g-','linewidth',2.5); >> axis equal; >> xlabel('X'); >> ylabel('Y'); >> legend('x=cos(t)','y=sin(t)','x^2+y^2=1','location','northeast');

   

 

>> p=[3 1;1 4]; r=5; [v,d]=eig(p);%求特征值,化為標准方程 a=sqrt(r/d(1));%橢圓長半軸 b=sqrt(r/d(4)); t=linspace(0,2*pi,60); xy=v*[a*cos(t);b*sin(t)]; plot(xy(1,:),xy(2,:),'k','linewidth',3); h=annotation('textarrow',[0.606 0.65],[0.55 0.65]); set(h,'String','3x^2+2xy+4y^2=5','FontSize',15); h=title('tuo yuan qu xian','FontSize',18,'FontWeight','bold');%加粗 set(h,'Position',[-0.00345622 1.35769 1.00011]); axis([-1.5 1.5 -1.2 1.7]); xlabel('X'); ylabel('Y');

  

 

>> a=[-19.6749 22.2118 5.0905]; x=0:0.01:1; y=a(1)+a(2)/2*(x-0.17).^2+a(3)/4*(x-0.17).^4; plot(x,y); text('interpreter','latex','string',['$$ -19.6749+\frac{22.2118}{2}(x-0.17)^2'...]) '+\frac{5.0905}{4}(x-0.17)^4 $$'],'position',[0.05,-12],'fontsize',12);

  

  • \frac{22.2118}{2}:22.2118 / 2
  • \alpha:α
  • \beta:beta

  help text properties

4、修飾圖形 

>> x=linspace(0,2*pi,60); >> y=sin(x); >> h=plot(x,y); >> grid on; >> set(h,'color','k','LineWidth',2); >> xticklabel={'0','pi/2','pi','3pi/2','2pi'}; >> set(gca,'xtick',[0:pi/2:2*pi],'XTickLabel',xticklabel,'TickDir','out'); >> xlabel('0\leq\theta\leq2\pi'); >> ylabel('sin(\theta)'); >> text(8*pi/9,sin(8*pi/9),'\leftarrow sin(8\pi\div 9)','HorizontalAlignment','left'); >> axis([0 2*pi -1 1]);

   

5、常用統計繪圖函數

 

>> x=normrnd(0,1,1000,1);  %產生1000個標准正態分布隨機數 hist(x,20);%繪制直方圖 xlabel('樣本數據'); ylabel('頻數'); figure;      %新建一個圖形窗口 cdfplot(x)

>> subplot(3,3,1); >> f=@(x)200*sin(x)./x;%定義匿名函數 >> fplot(f,[-20,20]); >> title('y=200*sin(x)/x'); >> 
>> subplot(3,3,2); >> ezplot('x^2+y^2=1',[-1.1 1.1]); >> axis equal; >> 
>> subplot(3,3,3); >> ezpolar('1+cos(t)'); >> 
>> subplot(3,3,4); >> x=[10 10 25 35 20]; >> name={'a','b','c','d','e'}; >> explode=[0 0 0 0 1]; >> pie(x,explode,name); >> 
>> subplot(3,3,6); >> stem(-2*pi:0.5:2*pi,sin(-2*pi:0.5:2*pi)); >> 
>> subplot(3,3,5); >> stairs(-2*pi:0.5:2*pi,sin(-2*pi:0.5:2*pi));
>>
>> subplot(3,3,7); >> z=eig(randn(20,20)); >> compass(z); >> >> subplot(3,3,8); >> theta=(-90:10:90)*pi/180; >> r=2*ones(size(theta)); >> [u,v]=pol2cart(theta,r); >> feather(u,v); >> >> subplot(3,3,9); >> t=(1/16:1/8:1)'*2*pi; >> fill(sin(t),cos(t),'r'); >> axis square;

5、三維圖形繪制

 

利用 mesh 和 surf 之前,應先產生圖形對象的網格數據 --> meshgrid

>> t=linspace(0,10*pi,300); >> plot3(20*sin(t),20*cos(t),t,'b','linewidth',2); >> hold on; >> quiver3(0,0,0,1,0,0,25,'k','filled','linewidth',2); >> quiver3(0,0,0,0,1,0,25,'k','filled','linewidth',2); >> quiver3(0,0,0,0,0,1,40,'k','filled','linewidth',2); >> grid on; >> xlabel('x');ylabel('y');zlabel('z'); >> axis([-25 25 -25 25 0 40]); >> view(-210,30);

>> [x,y]=meshgrid(1:4 , 2:5) x =

     1     2     3     4
     1     2     3     4
     1     2     3     4
     1     2     3     4 y =

     2     2     2     2
     3     3     3     3
     4     4     4     4
     5     5     5     5

>> plot(x,y,'r',x',y','b',x,y,'k.','MarkerSize',18); >> axis([0 5 1 6]);

eg:繪制三維曲面  z=xe-(x2+y2)  的等高線圖和梯度場

>> [x,y]=meshgrid(-2:.2:2); z=x.*exp(-x.^2-y.^2); [dx,dy]=gradient(z,0.2,0.2); contour(x,y,z); hold on; quiver(x,y,dx,dy); h=get(gca,'children'); 

>> ezsurf('u*sin(v)','u*cos(v)','4*v',[-2*pi,2*pi,-2*pi,2*pi]);

 

>> subplot(1,2,1); >> [x,y]=meshgrid(0:0.25:4,-2:.25:2); >> z=sin(x).*cos(y); >> [nx,ny,nz]=surfnorm(x,y,z);%計算法線方向 >> surf(x,y,z); >> hold on; >> quiver3(x,y,z,nx,ny,nz,0.5); >> axis(0 4 -2 2 -1 1); >> axis([0 4 -2 2 -1 1]); >> subplot(1,2,2); >> t=linspace(-2,2,20); >> [x,y,z]=meshgrid(t); >> [x,y,z]=meshgrid(t,t,t); >> v=x.*exp(-x.^2-y.^2-z.^2); >> xslice=2; >> yslice=2; >> xslice=[-1.2,.8,2]; >> zslice=[-2,0]; >> slice(x,y,z,v,xslice,yslice,zslice);

 6、三維圖形的修飾

colormap:繪圖色彩調整

shading:着色效果

alpha:透明度

light:光源

lighting:光照模式

material:對光反射效果

view:調整視點

>>  %立方體頂點坐標 >> vert=[0 0 0;0 200 0;200 200 0;200 0 0;0 0 100;...] 0 200 100;200 200 100;200 0 100]; >> fac=[1 2 3 4;2 6 7 3;4 3 7 8;1 5 8 4;1 2 6 5;5 6 7 8]; >> view(3); h=patch('faces',fac,'vertices',vert,'facecolor','g'); set(h,'facealpha',0.25); hold on; [x0,y0,z0]=sphere; >> x=30+30*x0;y=50+30*y0;z=50+30*z0; >> h1=surf(x,y,z,'linestyle','none','facecolor','b','edgecolor','none'); >> lightangle(45,30);%建立光源並設置光源視角 >> lighting phong; >> axis equal;

7、圖形導出到文件

print

hgexport

saveas:saveas(h,'filename.ext')

    saveas(h,'filename','format')   ------------  format:擴展名(字符串)

8、動畫制作

(1)彗星運行軌跡動畫

  comet,  comet3

>> t=linspace(0,10*pi,2000); >> x=t.*cos(t); >> y=t.*sin(t); >> comet(x,y)

(2霓虹閃爍動畫

  spinmap

>> sphere >> axis equal >> axis off >> spinmap(10,1)

(3)電影動畫

  getframe   movie 

>> x=linspace(-2,2,100); >> [x,y,z]=meshgrid(x,x,x); >> v=(x.^2+9/4*y.^2+z.^2-1).^3-x.^2.*z.^3-9/80*y.^2.*z.^3; >> p=patch(isosurface(x,y,z,v,0)); >> set(p,'facecolor','red','edgecolor','none'); >> view(3) >> axis equal; >> axis off >> light('posi',[0 -2 3]) >> lighting phong >> set(gca,'nextplot','replacechildren') >> xx=get(p,'XData'); >> yy=get(p,'yData'); zz=get(p,'ZData'); for j=1:20 bibi=sin(pi*j/20); set(p,'XData',bibi*xx,'YData',bibi*yy,'ZData',bibi*zz) f(j)=getframe; end >> movie(f,10)
%是個心跳

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM