MATLAB 画图 入门

来源:互联网 发布:推荐好的网络公开课 编辑:程序博客网 时间:2024/05/16 05:22

1.基本画图

程序如下:

[python] view plaincopy
  1. x=0:pi/1000:2*pi;  
  2. y1=sin(2*x);  
  3. y2=2*cos(2*x);  
  4. %输出图像  
  5. plot(x,y1,'k-',x,y2,'b--');  
  6. title(' Plot of f(x)=sin(2x) and its derivative');  
  7. %设置X坐标和Y坐标的标签  
  8. xlabel('x');  
  9. ylabel('y');  
  10. %制作图例  
  11. legend('f(x)=sin(2x)','d/dx f(x)')  
  12. %显示网格  
  13. grid on;  


显示结果:

显示结果

 

2.利用有限个点画出平滑曲线

[python] view plaincopy
  1. x=1:1:10;  
  2. y=[1,4,8,10,11,11.5,12,7,5,1];  
  3. xx=1:0.01:10;  
  4. %使用了函数interp1  
  5. yy=interp1(x,y,xx,'cublic');  
  6. plot(xx,yy,x,y,'.');  
  7. grid on;  
 

3.绘制三维曲线

[python] view plaincopy
  1. t=0:pi/100:20*pi;  
  2. x=sin(t);  
  3. y=cos(t);  
  4. z=t.*sin(t).*cos(t);  
  5. plot3(x,y,z);  
  6. title('Line in 3-D Space');  
  7. xlabel('X');ylabel('Y');zlabel('Z');  
  8. grid on;  

4.绘制三维曲面

[python] view plaincopy
  1. [x,y]=meshgrid(-8:0.5:8);  
  2. z=sin(sqrt(x.^2+y.^2))./sqrt(x.^2+y.^2+eps);  
  3. subplot(2,2,1);  
  4. mesh(x,y,z);  
  5. title('mesh(x,y,z)')  
  6. subplot(2,2,2);  
  7. meshc(x,y,z);  
  8. title('meshc(x,y,z)')  
  9. subplot(2,2,3);  
  10. meshz(x,y,z)  
  11. title('meshz(x,y,z)')  
  12. subplot(2,2,4);  
  13. surf(x,y,z);  
  14. title('surf(x,y,z)')  

标准三维曲面:

[python] view plaincopy
  1. t=0:pi/20:2*pi;  
  2. [x,y,z]= cylinder(2+sin(t),30);  
  3. subplot(2,2,1);  
  4. surf(x,y,z);  
  5. subplot(2,2,2);  
  6. [x,y,z]=sphere;  
  7. surf(x,y,z);  
  8. subplot(2,1,2);  
  9. [x,y,z]=peaks(30);   
  10. surf(x,y,z);  

其他函数:

[c-sharp] view plaincopy
  1. subplot(2,2,1);  
  2. bar3(magic(4))  
  3. subplot(2,2,2);  
  4. y=2*sin(0:pi/10:2*pi);  
  5. stem3(y);  
  6. subplot(2,2,3);  
  7. pie3([2347,1827,2043,3025]);  
  8. subplot(2,2,4);  
  9. fill3(rand(3,5),rand(3,5),rand(3,5), 'y' )  

绘制多峰函数的瀑布图和等高线图:

[python] view plaincopy
  1. subplot(1,2,1);  
  2. [X,Y,Z]=peaks(30);  
  3. waterfall(X,Y,Z)  
  4. xlabel('X-axis'),ylabel('Y-axis'),zlabel('Z-axis');  
  5. subplot(1,2,2);  
  6. contour3(X,Y,Z,12,'k');     %其中12代表高度的等级数  
  7. xlabel('X-axis'),ylabel('Y-axis'),zlabel('Z-axis');  

5.图形修饰处理

三种图像着色方式效果显示:

[python] view plaincopy
  1. [x,y,z]=sphere(20);  
  2. colormap(copper);  
  3. subplot(1,3,1);  
  4. surf(x,y,z);  
  5. axis equal  
  6. subplot(1,3,2);  
  7. surf(x,y,z);shading flat;  
  8. axis equal  
  9. subplot(1,3,3);  
  10. surf(x,y,z);shading interp;  
  11. axis equal  

光照处理后的球面:

[python] view plaincopy
  1. [x,y,z]=sphere(20);  
  2. subplot(1,2,1);  
  3. surf(x,y,z);axis equal;  
  4. light('Posi',[0,1,1]);  
  5. shading interp;  
  6. hold on;  
  7. plot3(0,1,1,'p');text(0,1,1,' light');  
  8. subplot(1,2,2);  
  9. surf(x,y,z);axis equal;  
  10. light('Posi',[1,0,1]);  
  11. shading interp;  
  12. hold on;  
  13. plot3(1,0,1,'p');text(1,0,1,' light');  

图形的裁剪处理:

[python] view plaincopy
  1. [x,y]=meshgrid(-5:0.1:5);  
  2. z=cos(x).*cos(y).*exp(-sqrt(x.^2+y.^2)/4);  
  3. subplot(1,2,1);  
  4. surf(x,y,z);shading interp;  
  5. title('裁剪之前');  
  6. i=find(x<=0&y<=0);  
  7. z1=z;z1(i)=NaN;  
  8. subplot(1,2,2);  
  9. surf(x,y,z1);shading interp;  
  10. title('裁剪之后');  

从文件载入图像:

[python] view plaincopy
  1. [x,cmap]=imread('flower.jpg');  %读取图像的数据阵和色图阵  
  2. image(x);colormap(cmap);  
  3. axis image off    %保持宽高比并取消坐标轴   

制作动画:

[python] view plaincopy
  1. [X,Y,Z]=peaks(30);   
  2. surf(X,Y,Z)  
  3. axis([-3,3,-3,3,-10,10])  
  4. axis off;  
  5. shading interp;  
  6. colormap(hot);  
  7. m=moviein(20);            %建立一个20列大矩阵  
  8. for i=1:20  
  9. view(-37.5+24*(i-1),30)      %改变视点  
  10. m(:,i)=getframe;            %将图形保存到m矩阵  
  11. end   
  12. movie(m,2);                 %播放画面2次  

0 0