当前位置:网站首页>Matlab 2012a 绘制带箭头的线段
Matlab 2012a 绘制带箭头的线段
2022-06-30 01:52:00 【Intimes】
Matlab 2012a 自带的line函数可以绘制线段,但无法绘制箭头。
此外还有一个函数叫 annotation,可以绘制箭头线段,但遗憾的是,只能在0到1的范围内绘制,和可能是出于什么特殊的考虑吧。
因此,需要自制函数来绘制带箭头的线段。在网上找了个代码,但是在Matlab 2012a环境下运行有问题,因此进行了一些改动。可以运行了。
首先来看下效果:

效果还是比较满意的,可以绘制漂亮的箭头线段,且范围不用限制在0到1的范围内,当然,运行效率等方面可以还是可以改进的。
下面直接上代码”
clc;clear;
x1 = [1 2];
y1 = [5 7];
h1 = drawarrow(x1,y1);
hold on;
axis([0 5 0 7]);
x2 = [3 1];
y2 = [3 5];
h2 = drawarrow(x2,y2);这是测试用的程序,下面是函数
function h = drawarrow(x,y,lineType,ax)
% https://blog.csdn.net/qq_41093957/article/details/115256114
% 这个程序最大的问题是 改变了plot的范围 从而使线段位置不对了
switch nargin
case 2
lineType='arrow';
ax=gca;
case 3
ax=gca;
end
% 调整坐标大小以适应箭头长度
% xlimraw=get(ax,'XLim');
% ylimraw=get(ax,'YLim');
xlim=get(ax,'XLim');
ylim=get(ax,'YLim');
xlimmin=xlim(1);xlimmax=xlim(2);
ylimmin=ylim(1);ylimmax=ylim(2);
if xlimmin>min(x(1),y(1)), xlimmin=min(x(1),y(1));end
if xlimmax<max(x(1),y(1)), xlimmax=max(x(1),y(1));end
if ylimmin>min(x(2),y(2)), ylimmin=min(x(2),y(2));end
if ylimmax<max(x(2),y(2)), ylimmax=max(x(2),y(2));end
% ax.XLim = [xlimmin,xlimmax];
% ax.YLim = [ylimmin,ylimmax];
set(ax,'XLim',[xlimmin,xlimmax]);
set(ax,'YLim',[ylimmin,ylimmax]);
xlim=get(ax,'XLim');
ylim=get(ax,'YLim');
% xlim=ax.XLim;
% ylim=ax.YLim;
pos=get(ax,'Position');
x_ratio = pos(3)/(xlim(2)-xlim(1));
y_ratio = pos(4)/(ylim(2)-ylim(1)); % 缩放比例
orig_pos=[-xlim(1)*x_ratio+pos(1),-ylim(1)*y_ratio+pos(2)]; % figure坐标系中的原点坐标
x=x.*[x_ratio,y_ratio];y=y.*[x_ratio,y_ratio];
x=x+orig_pos;y=y+orig_pos;
h = annotation(lineType,[x(1),y(1)],[x(2),y(2)]);
% set(ax,'XLim',xlimraw);
% set(ax,'YLim',ylimraw);
end
这里要鸣谢一下原作者。他的程序是没有问题的,只是可能不适应2012a的环境,改动之后,就可以用了。
边栏推荐
- 【机器学习Q&A】余弦相似度、余弦距离、欧式距离以及机器学习中距离的含义
- Kubernetes core object overview details
- (1) Basic learning - figure out the difference between pin, pad, port, IO and net
- (1)基础学习——图解pin、pad、port、IO、net 的区别
- Varnish foundation overview 3
- DTW learning (dynamic time warping) -- Thought and code implementation
- 003_ color
- C语言 害死人不偿命的(3n+1)猜想
- How to deal with occasional bugs?
- 【机器学习Q&A】数据抽样和模型验证方法、超参数调优以及过拟合和欠拟合问题
猜你喜欢
随机推荐
[pytorch actual combat] generate confrontation network Gan: generate cartoon character avatars
cookie加密8
Cookie加密13
【PyTorch实战】生成对抗网络GAN:生成动漫人物头像
【图神经网络】图分类学习研究综述[2]:基于图神经网络的图分类
一次 Keepalived 高可用的事故,让我重学了一遍它!
Ansible ad-hoc temporary command
Write this number in C
Three questions from the boss
The first technology podcast month will begin soon
cookie加密8
C语言 素数对猜想
JS returned content is encoded by Unicode
谁再用Redis过期监听实现关闭订单,立马滚蛋!
Unity2D--给动画添加关键帧并绑定事件
js逆向请求参数加密:
Repair method for win10 subsystem WSL if the passwords of root and other users are forgotten
MySQL monitoring 2
Local page floating animation is realized with the help of scroll wheel
C language irony



![【图神经网络】图分类学习研究综述[3]:图分类方法评价及未来研究方向](/img/b1/2afa73a14b2f41b7a65c4c2d261e6a.png)





