加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

关于plotyy的坐标轴 设置

(2012-05-16 14:38:54)
标签:

杂谈

分类: study与matlab

Matlab 多个图像共用一个横坐标 subplot、plotyy

x=0:0.1:7;
h1=subplot(3,1,1);
plot(x,sin(x),'k');
h2=subplot(3,1,2);
plot(x,cos(x),'k');
set([h1,h2],'Xcolor','w','XTick',[]) %将前两个x坐标设为白色,且不显示xtick

% 利用plotyy双轴显示同一个图像,一副显示左y,下x,另一幅显示右y,下x
h3=subplot(3,1,3);
[ax,h4,h5]=plotyy(x,sec(x),x,sec(x));
box off
set(ax(1),'YColor','k')%k 黑色
set(ax(2),'Xaxislocation','bottom','YColor','k')
set(h5,'color','k')
set(gcf,'color','w') % 背景色设为白色,间接隐藏所有白色轴线

http://s13/middle/647aa009gc021f1d5606c&690设置" TITLE="关于plotyy的坐标轴 设置" />
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot');
ylabel('First axes1');
set(AX(1),'ylim',[-300 300]);   % 坐标轴范围
set(AX(1),'ytick',[-300:100:300]) %坐标轴刻度  划分这么多个范围 
set(AX(1),'yticklabel',[-300:100:300])
set(AX(1),'box','off')
set(get(AX(2),'ylabel'),'string','Second axes2');
set(AX(2),'ylim',[-1 1]);
set(AX(2),'ytick',[-1:.5:1])
set(AX(2),'yticklabel',[-1:.5:1])
set(AX(2),'xaxislocation','top')%%%%%%%%%%坐标轴位置

http://s13/middle/647aa009gc0221621c81c&690设置" TITLE="关于plotyy的坐标轴 设置" />

 

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

在金融数据作图中像一般的统计分析图一样在横坐标标出日期,可按您的书中提到的方法我只能把日期叠加到原来的数字上,而且用尽办法也去不掉原来的数字。比如代码如下,横坐标的1 2 3 4 5怎么也去不掉并用日期代替,请赐教

A=[1 2 3 4 5];  B=[2 4 6 8 10];
plotyy(1:5,A,1:5,B,'plot');
set(gca,'XTick',[1 2 3 4 5]);
set(gca,'XTickLabel',{'2005-01', '2005-07','2006-01', '2006-07','2007-01'});

http://s1/middle/647aa009gc0222547bb90&690设置" TITLE="关于plotyy的坐标轴 设置" />
如图 横坐标有数字与重叠   去不掉  因为有两个x轴的缘故

 

修改后:

A=[1 2 3 4 5];  B=[2 4 6 8 10];
[AX H1 H2]=plotyy(1:5,A,1:5,B,'plot');
set(AX,'XTick',[1 2 3 4 5]);  % 相当于刻度坐标轴  不是让其自带控制大小
set(AX,'XTickLabel',{'2005-01', '2005-07','2006-01', '2006-07','2007-01'});


http://s10/middle/647aa009gc02237d0c8d9&690设置" TITLE="关于plotyy的坐标轴 设置" />

 

%% 举例说明 XTick

A=[1 2 3 4 5];  B=[2 4 6 8 10];
[AX H1 H2]=plotyy(1:5,A,1:5,B,'plot');

http://s15/middle/647aa009gc0223f90a71e&690设置" TITLE="关于plotyy的坐标轴 设置" />

set(AX,'XTick',[1 2 3 4 5]);

http://s6/middle/647aa009gc02240a32a25&690设置" TITLE="关于plotyy的坐标轴 设置" />

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

matlab plotyy xtick的横坐标的设置问题
[AX,H1,H2] = plotyy(24,0,0,0,'plot');
set(AX(1),'XColor','k','YColor','b');
set(AX(2),'XColor','k','YColor','r');
set(get(AX(1),'Ylabel'),'String','Plasma Glucose (mg/dl)')
set(get(AX(2),'Ylabel'),'String','Plasma Insulin (pmol/l)')
set(AX(1),'ylim',[50 250])
set(AX(2),'ylim',[0 600])
set(AX(1),'ytick',50:50:250)
set(AX(2),'ytick',0:100:600)
set(gca,'xlim',[0 24])
set(gca,'xtick',0:2:24)
grid on
xlabel('Time (hour)')

目的是想让左边的纵坐标范围为50-250,右边的纵坐标范围为0-600,横坐标范围为0-24
现在出来的图纵坐标没问题,但横坐标很乱,不知为什么。请高人指教。

倒数第三第四句:
set(gca,'xlim',[0 24])
set(gca,'xtick',0:2:24)
改成:
set(AX,'xlim',[0 24])
set(AX,'xtick',0:2:24)
原因:
这里有两个x轴,因此需要将两个的axes的x坐标设置成一样的才行

 

 

这些关键在于设置句柄

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

双纵坐标的标注已实现
[AX]=plotyy(x1,y1,x1,y2);
set(get(AX(1),'Ylabel'),'string','left Y-axis‘);
set(get(AX(2),'Ylabel'),'string','right y-axis');

了解plotyy的返回值
[AX]=plotyy(x1,y1,x1,y2);
得到两个axes句柄,AX(1)和AX(2)
set(AX(1),'yTick',[0:10:350])   设置左边Y轴的刻度 
set(AX(2),'yTick',[0:10:350]) 设置右边Y轴的刻度

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

双y坐标实例

close all hidden
clear all 
clc
% w=boxcar(nfft);
fni1=input('请输入时间序列文件: ','s');
fid1=fopen(fni1,'r');
s=fscanf(fid1,'%s',1);
if same(s,'Curve')
    for i=1:61
         tline=fgetl(fid1);
      end
else

fid1=fopen(fni1,'r');
end
a1=fscanf(fid1,'%f');
status=fclose(fid1);
n=length(a1);
n2=n/2;
a2=reshape(a1,2,n2);
x1=a2(1,:);
y1=a2(2,:);
fni2=input('输入速度曲线文件','s');
fid2=fopen(fni2,'r');
b=fscanf(fid2,'%f');
n3=length(b);
n4=n3/2;
b2=reshape(b,2,n4);
x2=b2(1,:);
y2=b2(2,:);
p=polyfit(x2,y2,3); 
y3=polyval(p,x2);
% plot(x2,y2)
[AX,H1,H2]=plotyy(x1,y1,x2,y3);
grid on;
xlabel('时间/s');
set(get(AX(1),'Ylabel'),'string','加速度/g');
set(get(AX(2),'Ylabel'),'string','速度km/h');
set(AX(1),'yTick',[-2:0.5:2]);
% % axes1 = axes('Position',[0.08 0.73 0.38 0.25],'Parent',figure1);
% axis(axes1,[0 xtime(end) -0.5 0.5]);
% set(AX(2),'YTick',[300:5:350]);
yticks2 = linspace(300,360,9);
set(AX(2),'YLim',[300 360],'YTick',yticks2);
set(H2,'linewidth',3);

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=
 

x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot');

set(AX(1),'XColor','k','YColor','b');
set(AX(2),'XColor','k','YColor','r');

HH1=get(AX(1),'Ylabel');
set(HH1,'String','Left Y-axis');
set(HH1,'color','b');

HH2=get(AX(2),'Ylabel');
set(HH2,'String','Right Y-axis');
set(HH2,'color','r');

set(H1,'LineStyle','-');
set(H1,'color','b');
set(H2,'LineStyle',':');
set(H2,'color','r');

legend([H1,H2],{'y1 = 200*exp(-0.05*x).*sin(x)';'y2 = 0.8*exp(-0.5*x).*sin(10*x)'});
xlabel('Zero to 20 \musec.');
title('Labeling plotyy');

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
用[AX,H1,H2]=plotyy(x,y1,x,y2);命令。AX(1),AX(2)分别为左右Y轴的句柄



%%You can use the handles returned by plotyy to label the axes and set the line styles used for plotting.
%%With the axes handles you can specify the YLabel properties of the left- and right-side y-axis:
set(get(AX(1),'Ylabel'),'String','Slow Decay')
set(get(AX(2),'Ylabel'),'String','Fast Decay')

%%Use the xlabel and title commands to label the x-axis and add a title:
xlabel('Time (\musec)')
title('Multiple Decay Rates')

%%Use the line handles to set the LineStyle properties of the left- and right-side plots:
set(H1,'LineStyle','--')
set(H2,'LineStyle',':')

、++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
linspace(0,2*pi,40)     % 0到2pi之间布 40个点   生成1*40的行向量
 
legend([h1,h2,h3],'a','b','c');
这块用曲线的句柄给曲线进行标注  。这种方法允许挑选任意几条标注,而不用按着画图的顺序来。

 

 


Plotyy(x1,y1,x2,y2, 'function1','function2')------功能同上,function是指那些
绘图函数如:plot,semilogx,
loglog等. ...

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有