利用matlab如何在图形中绘制箭头

风起帝国

收藏于 : 2019-01-14 22:18   被转藏 : 1   

一、二维箭头
1.调用annotation函数绘制二维箭头annotation函数用来在当前图形窗口建立注释对象(annotation对象),它的调用格式如下:
(1) annotation(annotation_type)  % 以指定的对象类型,使用默认属性值建立注释对象。
(2) annotation('line',x,y)       % 建立从(x(1), y(1))到(x(2), y(2))的线注释对象。
(3) annotation('arrow',x,y)      % 建立从(x(1), y(1))到(x(2), y(2))的箭头注释对象。
(4) annotation('doublearrow',x,y)% 建立从(x(1), y(1))到(x(2), y(2))的双箭头注释对象。
(5) annotation('textarrow',x,y)  % 建立从(x(1),y(1))到(x(2),y(2))的带文本框的箭头注释对象
(6) annotation('textbox',[x y w h])  % 建立文本框注释对象,左下角坐标(x,y),宽w,高h.
(7) annotation('ellipse',[x y w h])  % 建立椭圆形注释对象。
(8) annotation('rectangle',[x y w h])% 建立矩形注释对象。
(9) annotation(figure_handle,…)     % 在句柄值为figure_handle的图形窗口建立注释对象。
(10) annotation(…,'PropertyName',PropertyValue,…)  % 建立并设置注释对象的属性。
(11) anno_obj_handle = annotation(…)  % 返回注释对象的句柄值。
注意:annotation对象的父对象是figure对象,上面提到的坐标x,y是标准化的坐标,即整个图形窗口(figure对象)左下角为(0,  0),右上角为(1,  1)。宽度w和高度h也都是标准化的,其取值在[0,  1]之间。
【例1】根据椭圆方程绘制椭圆曲线,并修饰图形。

  1. >> P = [3 1; 1 4]; 

  2. >> r = 5;

  3. >> [V, D] = eig(P);     % 求特征值,将椭圆化为标准方程

  4. >> a = sqrt(r/D(1));     % 椭圆长半轴

  5. >> b = sqrt(r/D(4));    % 椭圆短半轴

  6. >> t = linspace(0, 2*pi, 60);    % 等间隔产生一个从0到2pi的包含60个元素的向量

  7. >> xy = V*[a*cos(t); b*sin(t)];    % 根据椭圆的极坐标方程计算椭圆上点的坐标

  8. >> plot(xy(1,:),xy(2,:), 'k', 'linewidth', 3);    % 绘制椭圆曲线,线宽为3,颜色为黑色

  9. % 在当前图形窗口加入带箭头的文本标注框

  10. >> h = annotation('textarrow',[0.606 0.65],[0.55 0.65]);

  11. % 设置文本标注框中显示的字符串,并设字号为15

  12. >> set(h, 'string','3x^2+2xy+4y^2 = 5', 'fontsize', 15);

  13. >> annotation('doublearrow',[0.2 0.8],[0.85 0.85],...

  14. 'LineStyle','-','color',[1 0 0],'HeadStyle','cback3');

复制代码
【例2】绘制地球仪,并标出我们的位置

  1. % 绘制地球仪,并标出我们的位置

  2. >> cla reset;

  3. >> load topo;

  4. >> [x y z] = sphere(45);

  5. >> s = surface(x,y,z,'FaceColor','texturemap','CData',topo);

  6. >> colormap(topomap1);

  7. % Brighten the colormap for better annotation visibility:

  8. >> brighten(.6)

  9. % Create and arrange the camera and lighting for better visibility:

  10. >> campos([1.3239  -14.4250  9.4954]);

  11. >> camlight;

  12. >> lighting gouraud;

  13. >> axis off vis3d;

  14. % Set the x- and y-coordinates of the textarrow object:

  15. >> x = [0.7698 0.5851];

  16. >> y = [0.3593 0.5492];

  17. % Create the textarrow object: 

  18. >> txtar =  annotation('textarrow',x,y,'String','We are here.','FontSize',14);

复制代码
2.调用quiver函数绘制箭头
quiver函数的调用格式如下:
quiver(x,y,u,v)
quiver(u,v)
quiver(...,scale)
quiver(...,LineSpec)
quiver(...,LineSpec,'filled')
quiver(axes_handle,...)
h = quiver(...)
参数说明略,这里仅举两例。
【例3】绘制正弦曲线,并修饰图形。

  1. >> ezplot('sin(x)')

  2. >> hold on

  3. >> quiver(0,0,2,0,'r','filled','LineWidth',2);

  4. >> text(2,0,'y = sin(x)')

复制代码
【例4】绘制三维曲面Z = x*exp(-(x^2+y^2)) 的等高线图和梯度场。

  1. >> [X,Y] = meshgrid(-2:.2:2);        % 产生网格数据X和Y

  2. >> Z = X.*exp(-X.^2 - Y.^2);         % 计算网格点处曲面上的Z值

  3. >> [DX,DY] = gradient(Z,0.2,0.2);    % 计算曲面上各点处的梯度

  4. >> contour(X,Y,Z) ;                  % 绘制等高线

  5. >> hold on ;                         % 开启图形保持

  6. >> quiver(X,Y,DX,DY) ;               % 绘制梯度场

  7. >> h = get(gca,'Children');          % 获取当前axes对象的所有子对象的句柄

  8. >> set(h, 'Color','k');              % 设置当前axes对象的所有子对象的颜色为黑色

复制代码
3.调用text函数绘制箭头
通过设置图像窗口中文本对象属性也可绘制箭头,请看下例:
【例5】绘制正弦曲线,并修饰图形。

  1. >> ezplot('sin(x)')

  2. >> text(3*pi/4,sin(3*pi/4),'\leftarrowsin(t) = .707','EdgeColor','red');

复制代码
二、三维箭头
1.调用quiver3函数绘制三维箭头
quiver3函数的调用格式如下:
quiver3(x,y,z,u,v,w)
quiver3(z,u,v,w)
quiver3(...,scale)
quiver3(...,LineSpec)
quiver3(...,LineSpec,'filled')
quiver3(axes_handle,...)
h = quiver3(...)
参数说明略,这里仅举两例。
【例6】绘制三维坐标轴。

  1. >> quiver3(0,0,0,1,0,0,2,'k','filled','LineWidth',2);

  2. >> hold on

  3. >> quiver3(0,0,0,0,1,0,2,'k','filled','LineWidth',2);

  4. >> quiver3(0,0,0,0,0,1,2,'k','filled','LineWidth',2);

复制代码
【例7】绘制三维曲面Z = x*exp(-(x^2+y^2)) 的法线向量。

  1. >> [X,Y] = meshgrid(-2:0.25:2,-1:0.2:1);

  2. >> Z = X.* exp(-X.^2 - Y.^2);

  3. >> [U,V,W] = surfnorm(X,Y,Z);

  4. >> quiver3(X,Y,Z,U,V,W,0.5);

  5. >> hold on;

  6. >> surf(X,Y,Z);

  7. >> colormap hsv

  8. >> view(-35,45)

  9. >> axis ([-2 2 -1 1 -.6 .6])

  10. >> hold off

复制代码

quiver 改变箭头大小maxheadsize 缩放标记位置 scale

quiver(X(P2(i)),Y(P2(i)),-(X(P2(i))-X(P1(i))),-(Y(P2(i))-Y(P1(i))),0.5,'Color','k','LineWidth',2,'maxheadsize',1.1)

 阅读文章全部内容  
点击查看
文章点评
相关文章
风起帝国 关注

文章收藏:4836

TA的最新收藏