matlab plotyy 横坐标,[转载]关于plotyy的坐标轴 设置

本文详细介绍了如何在 MATLAB 中使用 plotyy 函数创建双纵坐标图表,并着重讲解了如何设置横坐标,包括颜色、刻度、标签等。通过示例代码展示了如何调整坐标轴的位置、范围以及去除多余的横坐标标记,同时提供了金融数据作图中横坐标日期替换的解决方案。此外,还探讨了双纵坐标图的标注、线型设置以及曲线句柄的使用,以便灵活控制图表的各个细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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') % 背景色设为白色,间接隐藏所有白色轴线

a4c26d1e5885305701be709a3d33442f.png

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

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')%%%%%%%%%%坐标轴位置

a4c26d1e5885305701be709a3d33442f.png

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

在金融数据作图中像一般的统计分析图一样在横坐标标出日期,可按您的书中提到的方法我只能把日期叠加到原来的数字上,而且用尽办法也去不掉原来的数字。比如代码如下,横坐标的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'});

a4c26d1e5885305701be709a3d33442f.png

如图 横坐标有数字与重叠 去不掉 因为有两个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'});

a4c26d1e5885305701be709a3d33442f.png

%% 举例说明 XTick

A=[1 2 3 4 5]; B=[2 4 6 8 10];

[AX H1 H2]=plotyy(1:5,A,1:5,B,'plot');

a4c26d1e5885305701be709a3d33442f.png

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

a4c26d1e5885305701be709a3d33442f.png

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

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等. ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值