【Matlab学习手记】绘制带箭头直线

Matlab提供了绘制箭头的函数,如以下函数及效果图,可见,此函数绘制的范围限定在[0, 1]之间,如果要在指定位置绘制箭头,则此方法不可取,需要重新定义绘图函数,将实际坐标换算到[0, 1]之间。

annotation('arrow', [0, 1], [0, 1]);

 源代码

function PlotLineArrow(obj, x, y, markerColor, lineColor)
% 绘制带箭头的曲线
% 绘制散点图
plot(x, y, 'o', 'Color', markerColor, 'MarkerFaceColor', markerColor);
% 获取 Axes 位置
posAxes = get(obj, 'Position');
posX = posAxes(1);
posY = posAxes(2);
width = posAxes(3);
height = posAxes(4);
% 获取 Axes 范围
limX = get(obj, 'Xlim');
limY = get(obj, 'Ylim');
minX = limX(1);
maxX = limX(2);
minY = limY(1);
maxY = limY(2);
% 转换坐标
xNew = posX + (x - minX) / (maxX - minX) * width;
yNew = posY + (y - minY) / (maxY - minY) * height;
% 画箭头
annotation('arrow', xNew, yNew, 'color', lineColor);

示例

clear; clc;
x = 1 : 10;
y = sin(x);
plot(x, y, '.')
hold on
for i = 1 : 9
    PlotLineArrow(gca, [x(i), x(i + 1)], [y(i), y(i + 1)], 'b', 'r')
end
hold off

效果图

说明: 先绘制原来的图,建议用.表示,后面会被覆盖,确定好Axes,得到相关的信息,然后逐点绘制箭头。

  • 16
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值