在利用matlab作图时,经常需要对图形进行标注,例如标注坐标值、箭头、主题等。利用text、legend可以实现该功能,但当拉伸图形窗口时,标注的位置会发生变化。而 annotation 是相对于坐标轴定位,使用该组件的标注不会发生变化。
类型参数 | 意义 |
'line' | 直线 |
'arrow' | 箭头 |
'doublearrow' | 双箭头 |
'textarrow' | 带有文本框的箭头 |
'textbox' | 文本框 |
'ellipse' | 椭圆 |
'rectangle' | 矩形 |
% 创建一个球,使用地质颜色映射表
cla reset;
load topo;
[x y z] = sphere(45);
s = surface(x, y, z, 'facecolor', 'texturemap', 'cdata', topo);
colormap(topomap1);
% 增强显示颜色亮度
brighten(.6)
campos([2 13 10]);
camlight;
lighting gouraud;
axis off vis3d;
x = [0.7698 0.5851];% 设置箭头文本框的位置信息
y = [0.3593 0.5492];
% 创建箭头文本对象
txtar = annotation('textarrow', x, y,'LineWidth', 2,'Color', 'r','string', '文本标记','FontSize', 12);
注意
annotation 使用的是相对坐标,如果需要使用绝对坐标,可以看了使用 quiver 画箭头,例如
x = [0.9027 1.203 1.285 1.5];% 设置箭头的位置信息
y = [2.111 2.183 2.34 2.4];
dx = x(2:end)-x(1:end-1);
dy = y(2:end)-y(1:end-1);
for i= 1:length(x)
plot(x(i),y(i),'r*'),hold on
end
quiver(x(1:end-1),y(1:end-1),dx,dy,'LineWidth', 1,'Color', 'b')