matlab三维绘图注释,[学习笔记]matlab绘图的一些技巧

matlab绘图的一些技巧

1.在坐标轴上任意标上感兴趣的刻度。

用XTick、YTick、ZTick。如图1.

如:x=0:0.1:10;y=x.^2;h=plot(x,y,'o',x,y);set(gca,'YTick',[0,10,25,50,80,99],'XTick',[0.5,8,10]);

用XTickLabel、YTickLabel、ZTickLabel属性把标记标签从数值改为字符串。如图2.

如将y轴上的值80用字符串代替:x=0:0.1:10;y=x.^2;h=plot(x,y,'o',x,y);

set(gca,'YTickLabel','0|10|25|50|cutoff|99');

92886275_1

图1

92886275_2

图2

2.使用多个x轴和y轴

XAxisLocation和YAxisLocation属性指定在图形的哪一侧放置x轴和y轴。如图3.

x1=0:0.01:10;y1=sin(x1);

h1=line(x1,y1,'Color','r');

ax1=gca;set(ax1,'XColor','r','YColor','r');

ax2=axes('Position',get(ax1,'Position'),'XAxisLocation','top','YAxisLocation','right','Color','none','XColor','k','YColor','k');

x2=x1;y2=cos(x2);

h2=line(x2,y2,'Color','k','Parent',ax2);

92886275_3

图3

3.连接图形与变量(更新自变量或因变量的值)

用数据源属性XDataSource、YDataSource、ZDataSource及refreshdata.可以做动画。

t=0:0.01:2*pi;

y=exp(sin(t));

h=plot(t,y,'YDataSource','y');

for k=1:0.1:20

y=exp(sin(t.*k));

refreshdata(h,'caller');

drawnow;

pause(0.1);

end

4.创建组(Hggroup)对象

将每个Hggroup子对象的HitTest属性值设置为off,使得单击任何子对象时,可以选择所有子对象。

将每个Hggroup子对象的HitTest属性值设置为off,使得单击任何子对象时,该Hggroup对象成为当前对象。

设置Hggroup对象的Vision属性会同时将每个子对象的Vision属性值为相同值。

鼠标点击图4中任意一条线,变为图5.

hg=hggroup('ButtonDownFcn',@set_lines);

hl=line(randn(5),randn(5),'HitTest','off','Parent',hg);

function set_lines(cb,eventdata)

hl=get(cb,'Children');

lw=get(hl,'Linewidth');

set(hl,{'Linewidth'},num2cell([lw{:}]+1,[5,1])');

92886275_4

图4

92886275_5

图5

5.创建Hgtransform对象

将Hgtransform对象作为父对象的优点:该对象提供了对其子对象进行变换的能力,包括平移、比例化、旋转。

Hgtransform对象可以包括其他对象,从而在处理可见性、大小、方向等属性时,可以将Hgtransform对象及其子对象作为单个实体处理。操作时,将对象的Parent属性设置为Hgtransform对象的句柄。

用一组父对象为单个Hgtransform对象的表面对象创建3维星形图。将Hgtransform对象绕z轴旋转,同时缩放。

(1)创建一个axes对象。

ax=axes('XLim',[-1.5,1.5],'YLim',[-1.5,1.5],'ZLim',[-1.5,1.5]);

view(3);grid on; axis equal;

(2)创建希望作为Hgtransform对象子对象的对象。

[x,y,z]=cylinder([0.2,0]);

h(1)=surface(x,y,z,'FaceColor','r');

h(2)=surface(x,y,-z,'FaceColor','g');

h(3)=surface(z,x,y,'FaceColor','b');

h(4)=surface(-z,x,y,'FaceColor','c');

h(5)=surface(y,z,x,'FaceColor','m');

h(6)=surface(y,-z,x,'FaceColor','y');

(3)创建一个Hgtransform对象,并作为刚刚创建的一系列表面对象的父对象。

t=hgtransform('Parent',ax);

set(h,'Parent',t);

(4)选择一个渲染器并显示对象。

set(gcf,'Renderer','opengl');

drawnow;

(5)将旋转和比例化矩阵初始化

Rz=eye(4);

Sxy=Rz;

(6)组成z轴旋转矩阵和比例化矩阵

for r=1:0.1:2*pi

Rz=makehgtform('zrotate',r);

Sxy=makehgtform('scale',r/4);

set(t,'Matrix',Rz*Sxy);

drawnow;

end

pause(1);

(7) 用单位矩阵重新设置原始方位和大小

set(t,'Matrix',eye(4));

 92886275_6

图6

6.三维图形绕x、y、z轴旋转或平移

h=surface(peaks(40));

view(-20,30);

t=hgtransform;

set(h,'Parent',t);

ry_angle=-30*pi/180;

rx_angle=30*pi/180;

Ry=makehgtform('yrotate',ry_angle);

Rx=makehgtform('xrotate',rx_angle);

set(t,'Matrix',Ry);

pause(2);

set(t,'Matrix',Rx);

pause(2);

Tx1=makehgtform('translate',[-5,0,0]);

Tx2=makehgtform('translate',[5,0,0]);

set(t,'Matrix',Rx*Tx2*Ry*Tx1);

7.用属性值查找对象——findobj函数以及使用逻辑操作符和正则表达式

(1)Find all line objects in the current axes:

h = findobj(gca,'Type','line')

(2)Find all objects having a Label set to 'foo' and a String set to

'bar':

h = findobj('Label','foo','-and','String','bar');

(3)Find all objects whose String is not 'foo' and is not

'bar':

h = findobj('-not','String','foo','-not','String','bar');

(4)Find all objects having a String set to 'foo' and a Tag set to

'button one' and whose Color is not 'red' or 'blue':

h = findobj('String','foo','-and','Tag','button

one',...

'-and','-not',{'Color','red','-or','Color','blue'})

(5)Find all objects for which you have assigned a value to the Tag

property (that is, the value is not the empty string ''):

h = findobj('-regexp','Tag','[^'']')

(6)Find all children of the current figure that

have their BackgroundColor property set to a certain shade of gray

([.7 .7 .7]). This statement also searches the current figure for

the matching property value pair.

h = findobj(gcf,'-depth',1,'BackgroundColor',[.7 .7 .7])

8.复制和删除对象

复制:new_handle = copyobj(h,p)

h = surf(peaks);

colormap hot;

figure;

new_handle1 = copyobj(h,gca);

colormap cool;

view(3);

grid on;

92886275_7 92886275_8

图7

删除:delete(h),delete(handle_array)

8、直方图的标注

clear;

x = -2.9:0.5:2.9;

y=exp(-x.*x);

bar(x,y,'g','EdgeColor',[1,0.5,0.5]);

set(gcf,'color','w');

set(gca,'FontSize',12,'XTick',[-4:0.5:3]);

legend('直方图示例');

for i=1:length(x)

str=sprintf('%4.2f',y(i));

text(x(i)-0.1,y(i)+0.02,str);

end

92886275_9

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值