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)
%是个心跳