Firstly, one can use quiver
, where one doesn't have to deal with unhandy normalized figure units by use of annotation
drawArrow = @(x,y) quiver( x(1),y(1),x(2)-x(1),y(2)-y(1),0 )
x1 = [10 30];
y1 = [10 30];
drawArrow(x1,y1); hold on
x2 = [25 15];
y2 = [15 25];
drawArrow(x2,y2)
Important is the 5th argument of quiver
: 0 which disables an otherwise default scaling, as this function is usually used to plot vector fields. (or use the property value pair 'AutoScale','off'
)
One can also add additional features:
drawArrow = @(x,y,varargin) quiver( x(1),y(1),x(2)-x(1),y(2)-y(1),0, varargin{:} )
drawArrow(x1,y1); hold on
drawArrow(x2,y2,'linewidth',3,'color','r')
If different arrowheads are desired, one needs to use annotations (this answer is may helpful How do I change the arrow head style in quiver plot?).
The arrow head size can be adjust with the 'MaxHeadSize'
property. It's not consistent unfortunately. The axes limits need to be set afterwards.
x1 = [10 30];
y1 = [10 30];
drawArrow(x1,y1,{'MaxHeadSize',0.8,'Color','b','LineWidth',3}); hold on
x2 = [25 15];
y2 = [15 25];
drawArrow(x2,y2,{'MaxHeadSize',10,'Color','r','LineWidth',3}); hold on
xlim([1, 100])
ylim([1, 100])
There is another tweak for adjustable arrow heads:
function [ h ] = drawArrow( x,y,xlimits,ylimits,props )
xlim(xlimits)
ylim(ylimits)
h = annotation('arrow');
set(h,'parent', gca, ...
'position', [x(1),y(1),x(2)-x(1),y(2)-y(1)], ...
'HeadLength', 10, 'HeadWidth', 10, 'HeadStyle', 'cback1', ...
props{:} );
end
which you can call from your script as follows:
drawArrow(x1,y1,[1, 100],[1, 100],{'Color','b','LineWidth',3}); hold on
drawArrow(x2,y2,[1, 100],[1, 100],{'Color','r','LineWidth',3}); hold on