mar*_*sei 19
对于Matlab版本> R2014b
自R2014b版本以来,Matlab已经修改了其图形组件的结构.这是使用Matlab 注释的最新代码.
由...产生
headWidth = 8;
headLength = 8;
LineLength = 0.08;
%some data
[x,y] = meshgrid(0:0.2:2,0:0.2:2);
u = cos(x).*y;
v = sin(x).*y;
%quiver plots
figure('Position',[10 10 1000 600],'Color','w');
hax_1 = subplot(1,2,1);
hq = quiver(x,y,u,v); %get the handle of quiver
title('Regular Quiver plot','FontSize',16);
%get the data from regular quiver
U = hq.UData;
V = hq.VData;
X = hq.XData;
Y = hq.YData;
%right version (with annotation)
hax_2 = subplot(1,2,2);
%hold on;
for ii = 1:length(X)
for ij = 1:length(X)
headWidth = 5;
ah = annotation('arrow',...
'headStyle','cback1','HeadLength',headLength,'HeadWidth',headWidth);
set(ah,'parent',gca);
set(ah,'position',[X(ii,ij) Y(ii,ij) LineLength*U(ii,ij) LineLength*V(ii,ij)]);
end
end
%axis off;
title('Quiver - annotations ','FontSize',16);
linkaxes([hax_1 hax_2],'xy');
Run Code Online (Sandbox Code Playgroud)
请注意,这段代码改变了头部样式并控制了线条的长度(在左侧面板中,您可以看到左侧子图的左上部分重叠箭头,而右侧子图则没有).箭头的长度和宽度不会被修改.
对于这个编辑,我没有保留为角度编码的颜色方案,并丢弃动态头部大小.它让事情变得更加清晰.
对于Matlab版本<R2014b
箭头图很难修改.正如@Luis Mendo所说,你可以在matlab安装中修改箭头功能.但是,您仍然会受到以编程方式绘制具有漂亮补丁/线条的箭头的复杂性的限制.可能有一个更容易使用的路线annotation
- 请参阅设置headStyle
属性的"箭头 - 注释"子图cback1
.
注释是图形对象(线条,文本框,箭头......),一旦绘图完成,您可以手动插入.例如,它们显示附加文本或指向特定区域.您也可以通过定义它们的位置以编程方式插入它们 - 这是我们将采取的选项.我们首先绘制常规quiver
图(左侧面板),获取蓝线X
和Y
数据,然后使用这些坐标插入注释箭头,每个箭头都显示在完全相同的位置(相同位置,相同角度,相同尺寸;右侧面板) ).
注释箭头有一些很不错的属性,你可以很容易地修改,例如Color
,HeadWidth
,HeadLength
,和HeadStyle
.在下图中,我根据角度与x轴的角度修改了每个箭头的颜色,headWidth
这取决于长度.
如下图
由...产生
%some data
[x,y] = meshgrid(0:0.2:2,0:0.2:2);
u = cos(x).*y;
v = sin(x).*y;
%quiver plots
figure('Position',[10 10 1000 600],'Color','w');
hax_1 = subplot(1,2,1);
%left version (regular)
hq1 = quiver(x,y,u,v);
%get the line position (first handle)
hkid = get(hq1,'children');
X = get(hkid(1),'XData');
Y = get(hkid(1),'YData');
axis off;
title('Quiver - regular ','FontSize',16);
%right version (with annotation)
hax_2 = subplot(1,2,2);
cmap = jet(116); %colormap, 116 because angles goes up to 115 degrees
for ii = 1:3:length(X)-1
headWidth = 200 * sqrt((X(ii+1)-X(ii)).^2 + (Y(ii+1)-Y(ii)).^2); % set the headWidth, function of length of arrow
angled = floor(atan2(Y(ii+1)-Y(ii),X(ii+1)-X(ii))*180/pi) + 1; %get the angle
ah = annotation('arrow',...
'Color', cmap(angled,:),...
'headStyle','cback1','HeadLength',50,'HeadWidth',headWidth);
set(ah,'parent',gca);
set(ah,'position',[X(ii) Y(ii) X(ii+1)-X(ii) Y(ii+1)-Y(ii)]);
end
axis off;
title('Quiver - annotations ','FontSize',16);
linkaxes([hax_1 hax_2],'xy');
Run Code Online (Sandbox Code Playgroud)
该文件refresh.m
位于文件夹中...\MATLAB\...\toolbox\matlab\specgraph\@specgraph\@quivergroup\@quivergroup
包含以下行:
%// Arrow head parameters
alpha = .33; %// Size of arrow head relative to the length of the vector
beta = .25; %// Width of the base of the arrow head relative to the length
Run Code Online (Sandbox Code Playgroud)
改变数值alpha
并beta
达到预期效果.
但是,这需要修改Matlab的文件,因此不建议这样做.如果这样做,请保留原始refresh.m
文件的副本.
结果使用quiver
帮助中显示的示例代码:
[x,y] = meshgrid(-2:.2:2,-1:.15:1);
z = x .* exp(-x.^2 - y.^2); [px,py] = gradient(z,.2,.15);
quiver(x,y,px,py), hold off, axis image
Run Code Online (Sandbox Code Playgroud)
使用原始参数(alpha = .33;
beta = .25;
):
用alpha = .5;
beta = .5;
: