失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > MATLAB箭头绘制 arrow3 函数与 quiver3 函数的实用教程

MATLAB箭头绘制 arrow3 函数与 quiver3 函数的实用教程

时间:2022-02-05 21:03:16

相关推荐

MATLAB箭头绘制 arrow3 函数与 quiver3 函数的实用教程

MATLAB 的 arrow3 函数可以方便地在 figure 窗口中绘制箭头,效率也还可以,这里简单介绍其基本使用方法以便读者参考。对于一般的使用场景而言arrow3的使用十分简单方便。但是,arrow3 是R13版本中的函数,后续已不再更新,对于需要绘制较多矢量箭头的场景,推荐使用 quiver 和 quiver3 函数。对于采用箭头作为注释的场景,推荐使用 annotation 函数。

文章目录

arrow3基础绘图方式进阶绘图方式颜色设定S 参数的使用全局变量 ColorOrderarrow3 官方帮助文档quiver3基础绘图方式进阶绘图方式

arrow3

基础绘图方式

arrow3 的基础绘图方式只需要指定箭头的起点(N x 3)、终点(N x 3)。如下:

figure;arrow3([0 0 0],[1 1 1]);grid on;

绘图效果如下:

如果给出多个起点、终点,便可以绘制多段箭头图形,注意起点、终点都是行向量。

figure;arrow3([0 0 0;1 1 1],[1 1 1;2 1 1]);grid on;

arrow3 可以用来绘制2D或3D的箭头。

进阶绘图方式

下面看看更复杂的绘图,使用 arrow3 可以指定箭头的颜色、尺寸、线形等等信息。

arrow3 完整的参数表为:

arrow3(P1,P2,S,W,H,IP,ALPHA,BETA)

其中最常用的 ‘S’ 参数可以指定颜色。

figure;P1 = [0 0 0; 1 0 0; 1 0 1; 1 1 1; 0 1 0];P2 = [P1(2:end,:); [0 1 1]];S = 'b';arrow3(P1,P2,S);grid on;

颜色的设置代码可以参考官方帮助文档。颜色的设置可以十分灵活,例如可以通过前缀 ‘_’ 和 ‘^’ 改变颜色的深浅,深浅度由 BETA 参数指定;可以将颜色设置成随机的 ‘x’、按顺序的 ‘o’ 以及按幅度的 ‘|’;

figure;P1 = [0 0 0; 1 0 0; 1 0 1; 1 1 1; 0 1 0];P2 = [P1(2:end,:); [0 1 1]];S = '_o';arrow3(P1,P2,S);grid on;

利用 S 参数,我们还可以指定箭头的其它特征,如线形和线的宽度,具体参考官方帮助文档。

figure;S = 'x2.5';arrow3(zeros(20,3),50*rand(20,3),S,2)grid on;

为了效率,请尽量使用一种颜色。在这里有个疑问:箭头的颜色和线的颜色为什么有时候一样有时候不同?这个问题将在下一节讨论。

W 和 H 参数分别指定箭头的宽度和高度:

figure;P1 = [0 0 0; 1 0 0; 1 0 1; 1 1 1; 0 1 0];P2 = [P1(2:end,:); [0 1 1]];S = '_o';arrow3(P1,P2,S, 2.0, 6.0);grid on;

IP 参数指定箭头起点的宽度:

figure;P1 = [0 0 0; 1 0 0; 1 0 1; 1 1 1; 0 1 0];P2 = [P1(2:end,:); [0 1 1]];S = 'o';arrow3(P1,P2,S, 2.0, 6.0, 2);grid on;

ALPHA 参数用来指定箭头图形的透明度,取值[0,1]。BETA 参数用来指定颜色变深浅的成都,取值[0,1]。

figure;P1 = [0 0 0; 1 0 0; 1 0 1; 1 1 1; 0 1 0];P2 = [P1(2:end,:); [0 1 1]];S = 'o';arrow3(P1,P2,S, 2.0, 6.0, 2, 0.2);grid on;

注意:对于不需要指定的参数,可以采用 ‘[]’ 跳过

颜色设定

颜色的设定是 arrow3 使用中较为复杂的环节。有三种方式可以指定箭头颜色:使用 ‘S’ 参数、使用全局变量 ‘ColorOrder’ 和设定 ‘gca’。这里仅介绍前两种方式,第3种请参考官方帮助文档。

S 参数的使用

事实上,arrow3 中 ‘S’ 参数若仅包含对箭头部分的颜色设定,而线部分则取决于MATLAB中的绘图颜色计数,如:

figure;theta=[0:pi/22:pi/2]';arrow3(zeros(12,2),[cos(theta),sin(theta)],'c',1.5,[],[],[],0.5);hold on;arrow3(zeros(12,2),[cos(theta+pi/2),sin(theta+pi/2)],'j',1.5,[],[],[],0.5)arrow3(zeros(12,2),[cos(theta+pi),sin(theta+pi)],'z',1.5,[],[],[],0.5)axis([-Inf,Inf, -Inf, Inf]);grid on;

三个 ‘S’ 值指定了箭头的颜色,而线的三种颜色事实上是因为绘制了三次,采用了MATLAB绘图的颜色计数,这个计数顺序实际上等同于将 ‘S’ 值设定为 ‘o’ 时的顺序。我们同样可以在一次绘图中让线的颜色也按顺序出现,只需在 ‘S’ 参数中加入 ‘*’ ,如:

figure;theta=[0:pi/22:pi/2]';arrow3(zeros(12,2),[cos(theta),sin(theta)],'c*',1.5,[],[],[],0.5);hold on;axis([-Inf,Inf, -Inf, Inf]);grid on;

如果将 ‘S’ 参数设定为按顺序 ‘o’ 或者随机 ‘x’,那么箭头部分与线部分的颜色会被同时影响,如:

figure;theta=[0:pi/22:pi/2]';arrow3(zeros(12,2),[cos(theta),sin(theta)],'o',1.5,[],[],[],0.5);hold on;arrow3(zeros(12,2),[cos(theta+pi/2),sin(theta+pi/2)],'j',1.5,[],[],[],0.5)arrow3(zeros(12,2),[cos(theta+pi),sin(theta+pi)],'z',1.5,[],[],[],0.5)axis([-Inf,Inf, -Inf, Inf]);grid on;

我们采用 ‘S’ 参数并不能直接控制线部分的颜色。

全局变量 ColorOrder

另外一种设置颜色的方式是重定义 ‘ColorOrder’ 全局变量。此时需要将 ‘S’ 参数设定为 ‘o’。全局变量 ‘ColorOrder’ 的默认值是 ‘bersvpd’。我们可以在其中加入 ‘_’ 或 ‘^’ 前缀来改变颜色的深浅。例如:

figure;global ColorOrder, ColorOrder='^ss_s';theta=[0:pi/22:pi/2]';arrow3(zeros(12,2),[cos(theta),sin(theta)],'o',1.5,[],[],[],0.5);hold on;arrow3(zeros(12,2),[cos(theta+pi/2),sin(theta+pi/2)],'k',1.5,[],[],[],0.5)axis([-Inf,Inf, -Inf, Inf]);grid on;

可以看出,设置 ‘S’ 参数为 ‘o’ 同时影响了线部分的颜色计数。同上,我们只要指定线形标志 ‘*’ 即可让线部分颜色按 ‘ColorOrder’ 顺序变化:

figure;global ColorOrder, ColorOrder='^ss_s_zz^z';theta=[0:pi/22:pi/2]';arrow3(zeros(12,2),[cos(theta),sin(theta)],'o',1.5,[],[],[],0.5);hold on;arrow3(zeros(12,2),[cos(theta+pi/2),sin(theta+pi/2)],'*k',1.5,[],[],[],0.5)axis([-Inf,Inf, -Inf, Inf]);grid on;

arrow3 官方帮助文档

arrow3 (R13)

arrow3(P1,P2) draws lines from P1 to P2 with directional arrowheads. P1 and P2 are either nx2 or nx3 matrices. Each row of P1 is an initial point, and each row of P2 is a terminal point.

arrow3(P1,P2,S,W,H,IP,ALPHA,BETA) can be used to specify properties of the line, initial point marker, and arrowhead. S is a character string made with one element from any or all of the following 3 columns:

Color SwitchesLineStyle LineWidth------------------ ------------------- --------------------k blacK (default) - solid (default) 0.5 points (default)y Yellow : dotted 0 no linesm Magenta-. dashdot / LineWidthOrderc Cyan -- dashedr Red * LineStyleOrder _______ __ g Green ^ | b Blue / \ | w White Arrowhead / \ Height a Asparagus /\| d Dark gray / \ | e Evergreen/___ ___\ __|__ f Firebrick | | | | h Hot pink|-- Width --| i Indigo | | | | j Jade | | l Light gray | | n Nutbrown| | p Pear | | q kumQuat Line -->| |<--LineWidths Sky blue| | t Tawny | | u bUrgundy| | v Violet | | z aZure | | x random Initial/ \ o colorOrder Point -->()<--IP| magnitudeMarker \_ _/ -------------------------------------------------------------Color Equivalencies-------------------------------------------------------------ColorOrderArrow3 |Simulink Arrow3--------------------|--------------------Color1 Blue |LightBlueaZureColor2 Evergreen|DarkGreenAsparagusColor3 Red |Orange kumQuatColor4 Sky blue |Gray Light grayColor5 Violet |Color6 Pear |Color7 Dark gray|-------------------------------------------------------------

The components of S may be specified in any order. Invalid characters in S will be ignored and replaced by default settings.

Prefixing the color code with ‘_’ produces a darker shade, e.g. ‘_t’ is dark tawny; prefixing the color code with ‘^’ produces a

lighter shade, e.g. ‘^q’ is light kumquat. The relative brightness of light and dark color shades is controlled by the scalar parameter BETA. Color code prefixes do not affect black (k), white (w), or the special color switches (xo|).

ColorOrder may be achieved in two fashions: The user may either set the ColorOrder property (using RGB triples) or define the

global variable ColorOrder (using a string of valid color codes). If the color switch is specified with ‘o’, and the global variable ColorOrder is a string of color codes (color switches less ‘xo|’, optionally prefixed with ‘_’ or ‘^’), then the ColorOrder property will be set to the sequence of colors indicated by the ColorOrder variable. The color sequence ‘bersvpd’ matches the default ColorOrder property. If the color switch is specified with ‘o’, and the global variable ColorOrder is empty or invalid, then the current ColorOrder property will be used. Note that the ColorOrder variable takes precedence over the ColorOrder property.

The magnitude color switch is used to visualize vector magnitudes in conjunction with a colorbar. If the color switch is specified

with ‘|’, colors are linearly interpolated from the current ColorMap according to the length of the associated line. This option sets

CLim to [MinM,MaxM], where MinM and MaxM are the minimum and maximum magnitudes, respectively.

The current LineStyleOrder property will be used if LineStyle is specified with ‘*’. MATLAB cycles through the line styles defined

by the LineStyleOrder property only after using all colors defined by the ColorOrder property. If however, the global variable

LineWidthOrder is defined, and LineWidth is specified with ‘/’, then each line will be drawn with sequential color, linestyle, and linewidth.

W (default = 1) is a vector of arrowhead widths; use W = 0 for no arrowheads. H (default = 3W) is a vector of arrowhead heights. If vector IP is neither empty nor negative, initial point markers will be plotted with diameter IP; for default diameter W, use IP = 0. The units of W, H and IP are 1/72 of the PlotBox diagonal.

ALPHA (default = 1) is a vector of FaceAlpha values ranging between 0 (clear) and 1 (opaque). FaceAlpha is a surface arrowhead and initial point marker) property and does not affect lines. FaceAlpha is not supported for 2D rendering.

BETA (default = 0.4) is a scalar that controls the relative brightness of light and dark color shades, ranging between 0 (no contrast) and 1 (maximum contrast).

Plotting lines with a single color, linestyle, and linewidth is faster than plotting lines with multiple colors and/or linestyles. Plotting lines with multiple linewidths is slower still. arrow3 chooses renderers that produce the best screen images; exported or printed plots may benefit from different choices.

arrow3(P1,P2,S,W,H,‘cone’,…) will plot cones with bases centered on P1 in the direction given by P2. In this instance, P2 is interpreted as a direction vector instead of a terminal point. Neither initial point markers nor lines are plotted with the ‘cone’ option.

HN = arrow3(P1,P2,…) returns a vector of handles to line and surface objects created by arrow3.

arrow3 COLORS will plot a table of named colors with default brightness. arrow3(‘colors’,BETA) will plot a table of named colors with brightness BETA.

arrow3 attempts to preserve the appearance of existing axes. In particular, arrow3 will not change XYZLim, View, or CameraViewAngle. arrow3 does not, however, support stretch-to-fill scaling. AXIS NORMAL will restore the current axis box to full size and remove any restrictions on the scaling of units, but will likely result in distorted arrowheads and initial point markers. See (arrow3_messes_up_my_plots.html).

If a particular aspect ratio or variable limit is required, use DASPECT, PBASPECT, AXIS, or XYZLIM commands before calling arrow3. Changing limits or aspect ratios after calling arrow3 may alter the appearance of arrowheads and initial point markers. arrow3 sets XYZCLimMode to manual for all plots, sets DataAspectRatioMode to manual for linear plots, and sets PlotBoxAspectRatioMode to manual for log plots and 3D plots. CameraViewAngleMode is also set to manual for 3D plots.

arrow3 UPDATE will restore the appearance of arrowheads and initial point markers that have become corrupted by changes to limits or aspect ratios. arrow3(‘update’,SF) will redraw initial point markers and arrowheads with scale factor SF. If SF has one element, SF scales W, H and IP. If SF has two elements, SF(1) scales W and IP, and SF(2) scales H. If SF has three elements, SF(1) scales W, SF(2) scales H, and SF(3) scales IP. All sizes are relative to the current PlotBox diagonal.

arrow3 UPDATE COLORS will update the magnitude coloring of arrowheads, initial point markers, and lines to conform to the current ColorMap.

HN = arrow3(‘update’,…) returns a vector of handles to updated objects.

EXAMPLES:

% 2D vectorsarrow3([0 0],[1 3])arrow3([0 0],[1 2],'-.e')arrow3([0 0],[10 10],'--x2',1)arrow3(zeros(10,2),50*rand(10,2),'x',1,3)arrow3(zeros(10,2),[10*rand(10,1),500*rand(10,1)],'u')arrow3(10*rand(10,2),50*rand(10,2),'x',1,[],1)% 3D vectorsarrow3([0 0 0],[1 1 1])arrow3(zeros(20,3),50*rand(20,3),'--x1.5',2)arrow3(zeros(100,3),50*rand(100,3),'x',1,3)arrow3(zeros(10,3),[10*rand(10,1),500*rand(10,1),50*rand(10,1)],'a')arrow3(10*rand(10,3),50*rand(10,3),'x',[],[],0)% Cone plott=(pi/8:pi/8:2*pi)'; p1=[cos(t) sin(t) t]; p2=repmat([0 0 1],16,1);arrow3(p1,p2,'x',2,4,'cone'), hold onplot3(p1(:,1),p1(:,2),p1(:,3)), hold offpause % change cone sizearrow3('update',[1,2])% Just for funarrow3(zeros(100,3),50*rand(100,3),'x',8,4,[],0.95)light('position',[-10 -10 -10],'style','local')light('position',[60,60,60]), lighting gouraud% ColorOrder variable, color code prefixes, and Betaglobal ColorOrder, ColorOrder='^ui^e_hq^v';theta=[0:pi/22:pi/2]';arrow3(zeros(12,2),[cos(theta),sin(theta)],'1.5o',1.5,[],[],[],0.5)% ColorOrder property, LineStyleOrder, and LineWidthOrderglobal ColorOrder, ColorOrder=[];set(gca,'ColorOrder',[1,0,0;0,0,1;0.25,0.75,0.25;0,0,0])set(gca,'LineStyleOrder',{'-','--','-.',':'})global LineWidthOrder, LineWidthOrder=[1,2,4,8];w=[1,2,3,4]; h=[4,6,4,2];arrow3(zeros(4,2),[10*rand(4,1),500*rand(4,1)],'o*/',w,h,0)% Magnitude coloringcolormap springarrow3(20*randn(20,3),50*randn(20,3),'|',[],[],0)set(gca,'color',0.7*[1,1,1])set(gcf,'color',0.5*[1,1,1]), grid on, colorbarpause % change the ColorMap and update colorscolormap hotarrow3('update','colors')% LogLog plotset(gca,'xscale','log','yscale','log');axis([1e2,1e8,1e-2,1e-1]); hold onp1=repmat([1e3,2e-2],15,1);q1=[1e7,1e6,1e5,1e4,1e3,1e7,1e7,1e7,1e7,1e7,1e7,1e6,1e5,1e4,1e3];q2=1e-2*[9,9,9,9,9,7,5,4,3,2,1,1,1,1,1]; p2=[q1',q2'];global ColorOrder, ColorOrder=[];set(gca,'ColorOrder',rand(15,3))arrow3(p1,p2,'o'), grid on, hold off% SemiLogX plotset(gca,'xscale','log','yscale','linear');axis([1e2,1e8,1e-2,1e-1]); hold onp1=repmat([1e3,0.05],15,1);q1=[1e7,1e6,1e5,1e4,1e3,1e7,1e7,1e7,1e7,1e7,1e7,1e6,1e5,1e4,1e3];q2=1e-2*[9,9,9,9,9,7,5,4,3,2,1,1,1,1,1]; p2=[q1',q2'];arrow3(p1,p2,'x'), grid on, hold off% SemiLogY plotset(gca,'xscale','linear','yscale','log');axis([2,8,1e-2,1e-1]); hold onp1=repmat([3,2e-2],17,1);q1=[7,6,5,4,3,7,7,7,7,7,7,7,7,6,5,4,3];q2=1e-2*[9,9,9,9,9,8,7,6,5,4,3,2,1,1,1,1,1]; p2=[q1',q2'];set(gca,'LineStyleOrder',{'-','--','-.',':'})arrow3(p1,p2,'*',1,[],0), grid on, hold off% Color tablesarrow3('colors') % default color tablearrow3('colors',0.3) % low contrast color tablearrow3('colors',0.5) % high contrast color table% Update initial point markers and arrowheads% relative to the current PlotBox diagonalarrow3('update') % redraw same sizearrow3('update',2) % redraw double sizearrow3('update',0.5) % redraw half sizearrow3('update',[0.5,2,1]) % redraw W half size,% H double size, and% IP same size

See also (arrow3_examples.html), (arrow3_messes_up_my_plots.html).

quiver3

quiver3 通常用来绘制矢量图中的箭头。quiver3 的用法与 plot、scatter等非常相似,参数指定完全可以参考。

基础绘图方式

quiver3 函数的基本格式如下:

quiver3(X,Y,Z,U,V,W)

‘X’, ‘Y’, ‘Z’ 分别指定箭头起点,即位置;‘U’, ‘V’, ‘W’ 分别指定箭头的长度和方向,即速度。

figure;quiver3(0,0,0,1,1,1);axis([-Inf, Inf, -Inf, Inf]);axis equal; grid on;

可以看出,quiver3 所绘制的箭头十分简洁,与 arrow3 的形式完全不同。其中颜色、线宽等参数可以采用plot的方式指定。

figure;quiver3(0,0,0,1,1,1,'Color',[1.0,0.0,0.0],'LineWidth',2.0);axis([-Inf, Inf, -Inf, Inf]);axis equal; grid on;

我们也可以通过循环来分别制定颜色:

注意,此时我们无法分别指定箭头的颜色和线的颜色,二者只能相同。箭头的大小与线的长度有关,会自动根据线调整,不可精确指定。我们可以通过 ‘W’ 之后的一个输入参数 'scale‘ 来相对地调整箭头大小,将其设定为 ‘0’ 或者 ’ ‘off’ ’ 即可取消自动调整。也可以修改 'AutoScale’ 属性和’AutoScaleFactor‘属性来进行调整。

进阶绘图方式

这里仅介绍线形调整,用法如下:

quiver3(___,LineSpec)

通过指定 'LinSpec’ 参数来调整线形,线形标志与 arrow3 的多数相同。

figure;quiver3(0,0,0,1,1,1, '--r','LineWidth',1.0);axis([-Inf, Inf, -Inf, Inf]);axis equal; grid on;

指定方式与 plot 基本相同,其余标志请参考官方帮助文档。

如果觉得《MATLAB箭头绘制 arrow3 函数与 quiver3 函数的实用教程》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。