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');
图1
图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);
图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])');
图4
图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));
图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','buttonone',...
'-and','-not',{'Color','red','-or','Color','blue'})
(5)Find all objects for which you have assigned a value to the Tagproperty (that is, the value is not the empty string ''):
h = findobj('-regexp','Tag','[^'']')
(6)Find all children of the current figure thathave their BackgroundColor property set to a certain shade of gray([.7 .7 .7]). This statement also searches the current figure forthe 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;
图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